xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 5b3a5a14)
1df828598SMugunthan V N /*
2df828598SMugunthan V N  * Texas Instruments Ethernet Switch Driver
3df828598SMugunthan V N  *
4df828598SMugunthan V N  * Copyright (C) 2012 Texas Instruments
5df828598SMugunthan V N  *
6df828598SMugunthan V N  * This program is free software; you can redistribute it and/or
7df828598SMugunthan V N  * modify it under the terms of the GNU General Public License as
8df828598SMugunthan V N  * published by the Free Software Foundation version 2.
9df828598SMugunthan V N  *
10df828598SMugunthan V N  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11df828598SMugunthan V N  * kind, whether express or implied; without even the implied warranty
12df828598SMugunthan V N  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13df828598SMugunthan V N  * GNU General Public License for more details.
14df828598SMugunthan V N  */
15df828598SMugunthan V N 
16df828598SMugunthan V N #include <linux/kernel.h>
17df828598SMugunthan V N #include <linux/io.h>
18df828598SMugunthan V N #include <linux/clk.h>
19df828598SMugunthan V N #include <linux/timer.h>
20df828598SMugunthan V N #include <linux/module.h>
21df828598SMugunthan V N #include <linux/platform_device.h>
22df828598SMugunthan V N #include <linux/irqreturn.h>
23df828598SMugunthan V N #include <linux/interrupt.h>
24df828598SMugunthan V N #include <linux/if_ether.h>
25df828598SMugunthan V N #include <linux/etherdevice.h>
26df828598SMugunthan V N #include <linux/netdevice.h>
272e5b38abSRichard Cochran #include <linux/net_tstamp.h>
28df828598SMugunthan V N #include <linux/phy.h>
29df828598SMugunthan V N #include <linux/workqueue.h>
30df828598SMugunthan V N #include <linux/delay.h>
31f150bd7fSMugunthan V N #include <linux/pm_runtime.h>
32e2b3e493SArnd Bergmann #include <linux/gpio/consumer.h>
332eb32b0aSMugunthan V N #include <linux/of.h>
349e42f715SHeiko Schocher #include <linux/of_mdio.h>
352eb32b0aSMugunthan V N #include <linux/of_net.h>
362eb32b0aSMugunthan V N #include <linux/of_device.h>
373b72c2feSMugunthan V N #include <linux/if_vlan.h>
38514c6032SRandy Dunlap #include <linux/kmemleak.h>
399611d6d6SIvan Khoronzhuk #include <linux/sys_soc.h>
40df828598SMugunthan V N 
41739683b4SMugunthan V N #include <linux/pinctrl/consumer.h>
427929a668SIvan Khoronzhuk #include <net/pkt_cls.h>
43df828598SMugunthan V N 
44dbe34724SMugunthan V N #include "cpsw.h"
45df828598SMugunthan V N #include "cpsw_ale.h"
462e5b38abSRichard Cochran #include "cpts.h"
47df828598SMugunthan V N #include "davinci_cpdma.h"
48df828598SMugunthan V N 
4957d90148SIvan Khoronzhuk #include <net/pkt_sched.h>
5057d90148SIvan Khoronzhuk 
51df828598SMugunthan V N #define CPSW_DEBUG	(NETIF_MSG_HW		| NETIF_MSG_WOL		| \
52df828598SMugunthan V N 			 NETIF_MSG_DRV		| NETIF_MSG_LINK	| \
53df828598SMugunthan V N 			 NETIF_MSG_IFUP		| NETIF_MSG_INTR	| \
54df828598SMugunthan V N 			 NETIF_MSG_PROBE	| NETIF_MSG_TIMER	| \
55df828598SMugunthan V N 			 NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	| \
56df828598SMugunthan V N 			 NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	| \
57df828598SMugunthan V N 			 NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	| \
58df828598SMugunthan V N 			 NETIF_MSG_RX_STATUS)
59df828598SMugunthan V N 
60df828598SMugunthan V N #define cpsw_info(priv, type, format, ...)		\
61df828598SMugunthan V N do {								\
62df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
63df828598SMugunthan V N 		dev_info(priv->dev, format, ## __VA_ARGS__);	\
64df828598SMugunthan V N } while (0)
65df828598SMugunthan V N 
66df828598SMugunthan V N #define cpsw_err(priv, type, format, ...)		\
67df828598SMugunthan V N do {								\
68df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
69df828598SMugunthan V N 		dev_err(priv->dev, format, ## __VA_ARGS__);	\
70df828598SMugunthan V N } while (0)
71df828598SMugunthan V N 
72df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...)		\
73df828598SMugunthan V N do {								\
74df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
75df828598SMugunthan V N 		dev_dbg(priv->dev, format, ## __VA_ARGS__);	\
76df828598SMugunthan V N } while (0)
77df828598SMugunthan V N 
78df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...)		\
79df828598SMugunthan V N do {								\
80df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
81df828598SMugunthan V N 		dev_notice(priv->dev, format, ## __VA_ARGS__);	\
82df828598SMugunthan V N } while (0)
83df828598SMugunthan V N 
845c50a856SMugunthan V N #define ALE_ALL_PORTS		0x7
855c50a856SMugunthan V N 
86df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg)		(reg >> 8 & 0x7)
87df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg)		(reg & 0xff)
88df828598SMugunthan V N #define CPSW_RTL_VERSION(reg)		((reg >> 11) & 0x1f)
89df828598SMugunthan V N 
90e90cfac6SRichard Cochran #define CPSW_VERSION_1		0x19010a
91e90cfac6SRichard Cochran #define CPSW_VERSION_2		0x19010c
92c193f365SMugunthan V N #define CPSW_VERSION_3		0x19010f
93926489beSMugunthan V N #define CPSW_VERSION_4		0x190112
94549985eeSRichard Cochran 
95549985eeSRichard Cochran #define HOST_PORT_NUM		0
96c6395f12SGrygorii Strashko #define CPSW_ALE_PORTS_NUM	3
97549985eeSRichard Cochran #define SLIVER_SIZE		0x40
98549985eeSRichard Cochran 
99549985eeSRichard Cochran #define CPSW1_HOST_PORT_OFFSET	0x028
100549985eeSRichard Cochran #define CPSW1_SLAVE_OFFSET	0x050
101549985eeSRichard Cochran #define CPSW1_SLAVE_SIZE	0x040
102549985eeSRichard Cochran #define CPSW1_CPDMA_OFFSET	0x100
103549985eeSRichard Cochran #define CPSW1_STATERAM_OFFSET	0x200
104d9718546SMugunthan V N #define CPSW1_HW_STATS		0x400
105549985eeSRichard Cochran #define CPSW1_CPTS_OFFSET	0x500
106549985eeSRichard Cochran #define CPSW1_ALE_OFFSET	0x600
107549985eeSRichard Cochran #define CPSW1_SLIVER_OFFSET	0x700
108549985eeSRichard Cochran 
109549985eeSRichard Cochran #define CPSW2_HOST_PORT_OFFSET	0x108
110549985eeSRichard Cochran #define CPSW2_SLAVE_OFFSET	0x200
111549985eeSRichard Cochran #define CPSW2_SLAVE_SIZE	0x100
112549985eeSRichard Cochran #define CPSW2_CPDMA_OFFSET	0x800
113d9718546SMugunthan V N #define CPSW2_HW_STATS		0x900
114549985eeSRichard Cochran #define CPSW2_STATERAM_OFFSET	0xa00
115549985eeSRichard Cochran #define CPSW2_CPTS_OFFSET	0xc00
116549985eeSRichard Cochran #define CPSW2_ALE_OFFSET	0xd00
117549985eeSRichard Cochran #define CPSW2_SLIVER_OFFSET	0xd80
118549985eeSRichard Cochran #define CPSW2_BD_OFFSET		0x2000
119549985eeSRichard Cochran 
120df828598SMugunthan V N #define CPDMA_RXTHRESH		0x0c0
121df828598SMugunthan V N #define CPDMA_RXFREE		0x0e0
122df828598SMugunthan V N #define CPDMA_TXHDP		0x00
123df828598SMugunthan V N #define CPDMA_RXHDP		0x20
124df828598SMugunthan V N #define CPDMA_TXCP		0x40
125df828598SMugunthan V N #define CPDMA_RXCP		0x60
126df828598SMugunthan V N 
127df828598SMugunthan V N #define CPSW_POLL_WEIGHT	64
128a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_SIZE		4
1299421c901SGrygorii Strashko #define CPSW_MIN_PACKET_SIZE	(VLAN_ETH_ZLEN)
130a3a41d2fSGrygorii Strashko #define CPSW_MAX_PACKET_SIZE	(VLAN_ETH_FRAME_LEN +\
131a3a41d2fSGrygorii Strashko 				 ETH_FCS_LEN +\
132a3a41d2fSGrygorii Strashko 				 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
133df828598SMugunthan V N 
134df828598SMugunthan V N #define RX_PRIORITY_MAPPING	0x76543210
135df828598SMugunthan V N #define TX_PRIORITY_MAPPING	0x33221100
1365e391dc5SIvan Khoronzhuk #define CPDMA_TX_PRIORITY_MAP	0x76543210
137df828598SMugunthan V N 
1383b72c2feSMugunthan V N #define CPSW_VLAN_AWARE		BIT(1)
139a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP	BIT(2)
1403b72c2feSMugunthan V N #define CPSW_ALE_VLAN_AWARE	1
1413b72c2feSMugunthan V N 
14235717d8dSJohn Ogness #define CPSW_FIFO_NORMAL_MODE		(0 << 16)
14335717d8dSJohn Ogness #define CPSW_FIFO_DUAL_MAC_MODE		(1 << 16)
14435717d8dSJohn Ogness #define CPSW_FIFO_RATE_LIMIT_MODE	(2 << 16)
145d9ba8f9eSMugunthan V N 
146ff5b8ef2SMugunthan V N #define CPSW_INTPACEEN		(0x3f << 16)
147ff5b8ef2SMugunthan V N #define CPSW_INTPRESCALE_MASK	(0x7FF << 0)
148ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_CNT	63
149ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_CNT	2
150ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_INTVL	(1000 / CPSW_CMINTMIN_CNT)
151ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_INTVL	((1000 / CPSW_CMINTMAX_CNT) + 1)
152ff5b8ef2SMugunthan V N 
153606f3993SIvan Khoronzhuk #define cpsw_slave_index(cpsw, priv)				\
154606f3993SIvan Khoronzhuk 		((cpsw->data.dual_emac) ? priv->emac_port :	\
155606f3993SIvan Khoronzhuk 		cpsw->data.active_slave)
156e38b5a3dSIvan Khoronzhuk #define IRQ_NUM			2
157e05107e6SIvan Khoronzhuk #define CPSW_MAX_QUEUES		8
15890225bf0SGrygorii Strashko #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
15957d90148SIvan Khoronzhuk #define CPSW_FIFO_QUEUE_TYPE_SHIFT	16
16057d90148SIvan Khoronzhuk #define CPSW_FIFO_SHAPE_EN_SHIFT	16
16157d90148SIvan Khoronzhuk #define CPSW_FIFO_RATE_EN_SHIFT		20
1627929a668SIvan Khoronzhuk #define CPSW_TC_NUM			4
1637929a668SIvan Khoronzhuk #define CPSW_FIFO_SHAPERS_NUM		(CPSW_TC_NUM - 1)
16457d90148SIvan Khoronzhuk #define CPSW_PCT_MASK			0x7f
165d3bb9c58SMugunthan V N 
166a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT	29
167a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK		GENMASK(2, 0)
168a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT	16
169a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT	8
170a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK	GENMASK(1, 0)
171a3a41d2fSGrygorii Strashko enum {
172a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
173a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
174a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
175a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
176a3a41d2fSGrygorii Strashko };
177a3a41d2fSGrygorii Strashko 
178df828598SMugunthan V N static int debug_level;
179df828598SMugunthan V N module_param(debug_level, int, 0);
180df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
181df828598SMugunthan V N 
182df828598SMugunthan V N static int ale_ageout = 10;
183df828598SMugunthan V N module_param(ale_ageout, int, 0);
184df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
185df828598SMugunthan V N 
186df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
187df828598SMugunthan V N module_param(rx_packet_max, int, 0);
188df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
189df828598SMugunthan V N 
19090225bf0SGrygorii Strashko static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
19190225bf0SGrygorii Strashko module_param(descs_pool_size, int, 0444);
19290225bf0SGrygorii Strashko MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
19390225bf0SGrygorii Strashko 
194996a5c27SRichard Cochran struct cpsw_wr_regs {
195df828598SMugunthan V N 	u32	id_ver;
196df828598SMugunthan V N 	u32	soft_reset;
197df828598SMugunthan V N 	u32	control;
198df828598SMugunthan V N 	u32	int_control;
199df828598SMugunthan V N 	u32	rx_thresh_en;
200df828598SMugunthan V N 	u32	rx_en;
201df828598SMugunthan V N 	u32	tx_en;
202df828598SMugunthan V N 	u32	misc_en;
203ff5b8ef2SMugunthan V N 	u32	mem_allign1[8];
204ff5b8ef2SMugunthan V N 	u32	rx_thresh_stat;
205ff5b8ef2SMugunthan V N 	u32	rx_stat;
206ff5b8ef2SMugunthan V N 	u32	tx_stat;
207ff5b8ef2SMugunthan V N 	u32	misc_stat;
208ff5b8ef2SMugunthan V N 	u32	mem_allign2[8];
209ff5b8ef2SMugunthan V N 	u32	rx_imax;
210ff5b8ef2SMugunthan V N 	u32	tx_imax;
211ff5b8ef2SMugunthan V N 
212df828598SMugunthan V N };
213df828598SMugunthan V N 
214996a5c27SRichard Cochran struct cpsw_ss_regs {
215df828598SMugunthan V N 	u32	id_ver;
216df828598SMugunthan V N 	u32	control;
217df828598SMugunthan V N 	u32	soft_reset;
218df828598SMugunthan V N 	u32	stat_port_en;
219df828598SMugunthan V N 	u32	ptype;
220bd357af2SRichard Cochran 	u32	soft_idle;
221bd357af2SRichard Cochran 	u32	thru_rate;
222bd357af2SRichard Cochran 	u32	gap_thresh;
223bd357af2SRichard Cochran 	u32	tx_start_wds;
224bd357af2SRichard Cochran 	u32	flow_control;
225bd357af2SRichard Cochran 	u32	vlan_ltype;
226bd357af2SRichard Cochran 	u32	ts_ltype;
227bd357af2SRichard Cochran 	u32	dlr_ltype;
228df828598SMugunthan V N };
229df828598SMugunthan V N 
2309750a3adSRichard Cochran /* CPSW_PORT_V1 */
2319750a3adSRichard Cochran #define CPSW1_MAX_BLKS      0x00 /* Maximum FIFO Blocks */
2329750a3adSRichard Cochran #define CPSW1_BLK_CNT       0x04 /* FIFO Block Usage Count (Read Only) */
2339750a3adSRichard Cochran #define CPSW1_TX_IN_CTL     0x08 /* Transmit FIFO Control */
2349750a3adSRichard Cochran #define CPSW1_PORT_VLAN     0x0c /* VLAN Register */
2359750a3adSRichard Cochran #define CPSW1_TX_PRI_MAP    0x10 /* Tx Header Priority to Switch Pri Mapping */
2369750a3adSRichard Cochran #define CPSW1_TS_CTL        0x14 /* Time Sync Control */
2379750a3adSRichard Cochran #define CPSW1_TS_SEQ_LTYPE  0x18 /* Time Sync Sequence ID Offset and Msg Type */
2389750a3adSRichard Cochran #define CPSW1_TS_VLAN       0x1c /* Time Sync VLAN1 and VLAN2 */
2399750a3adSRichard Cochran 
2409750a3adSRichard Cochran /* CPSW_PORT_V2 */
2419750a3adSRichard Cochran #define CPSW2_CONTROL       0x00 /* Control Register */
2429750a3adSRichard Cochran #define CPSW2_MAX_BLKS      0x08 /* Maximum FIFO Blocks */
2439750a3adSRichard Cochran #define CPSW2_BLK_CNT       0x0c /* FIFO Block Usage Count (Read Only) */
2449750a3adSRichard Cochran #define CPSW2_TX_IN_CTL     0x10 /* Transmit FIFO Control */
2459750a3adSRichard Cochran #define CPSW2_PORT_VLAN     0x14 /* VLAN Register */
2469750a3adSRichard Cochran #define CPSW2_TX_PRI_MAP    0x18 /* Tx Header Priority to Switch Pri Mapping */
2479750a3adSRichard Cochran #define CPSW2_TS_SEQ_MTYPE  0x1c /* Time Sync Sequence ID Offset and Msg Type */
2489750a3adSRichard Cochran 
2499750a3adSRichard Cochran /* CPSW_PORT_V1 and V2 */
2509750a3adSRichard Cochran #define SA_LO               0x20 /* CPGMAC_SL Source Address Low */
2519750a3adSRichard Cochran #define SA_HI               0x24 /* CPGMAC_SL Source Address High */
2529750a3adSRichard Cochran #define SEND_PERCENT        0x28 /* Transmit Queue Send Percentages */
2539750a3adSRichard Cochran 
2549750a3adSRichard Cochran /* CPSW_PORT_V2 only */
2559750a3adSRichard Cochran #define RX_DSCP_PRI_MAP0    0x30 /* Rx DSCP Priority to Rx Packet Mapping */
2569750a3adSRichard Cochran #define RX_DSCP_PRI_MAP1    0x34 /* Rx DSCP Priority to Rx Packet Mapping */
2579750a3adSRichard Cochran #define RX_DSCP_PRI_MAP2    0x38 /* Rx DSCP Priority to Rx Packet Mapping */
2589750a3adSRichard Cochran #define RX_DSCP_PRI_MAP3    0x3c /* Rx DSCP Priority to Rx Packet Mapping */
2599750a3adSRichard Cochran #define RX_DSCP_PRI_MAP4    0x40 /* Rx DSCP Priority to Rx Packet Mapping */
2609750a3adSRichard Cochran #define RX_DSCP_PRI_MAP5    0x44 /* Rx DSCP Priority to Rx Packet Mapping */
2619750a3adSRichard Cochran #define RX_DSCP_PRI_MAP6    0x48 /* Rx DSCP Priority to Rx Packet Mapping */
2629750a3adSRichard Cochran #define RX_DSCP_PRI_MAP7    0x4c /* Rx DSCP Priority to Rx Packet Mapping */
2639750a3adSRichard Cochran 
2649750a3adSRichard Cochran /* Bit definitions for the CPSW2_CONTROL register */
2651239a96aSIvan Khoronzhuk #define PASS_PRI_TAGGED     BIT(24) /* Pass Priority Tagged */
2661239a96aSIvan Khoronzhuk #define VLAN_LTYPE2_EN      BIT(21) /* VLAN LTYPE 2 enable */
2671239a96aSIvan Khoronzhuk #define VLAN_LTYPE1_EN      BIT(20) /* VLAN LTYPE 1 enable */
2681239a96aSIvan Khoronzhuk #define DSCP_PRI_EN         BIT(16) /* DSCP Priority Enable */
2691c0e8123SIvan Khoronzhuk #define TS_107              BIT(15) /* Tyme Sync Dest IP Address 107 */
2701239a96aSIvan Khoronzhuk #define TS_320              BIT(14) /* Time Sync Dest Port 320 enable */
2711239a96aSIvan Khoronzhuk #define TS_319              BIT(13) /* Time Sync Dest Port 319 enable */
2721239a96aSIvan Khoronzhuk #define TS_132              BIT(12) /* Time Sync Dest IP Addr 132 enable */
2731239a96aSIvan Khoronzhuk #define TS_131              BIT(11) /* Time Sync Dest IP Addr 131 enable */
2741239a96aSIvan Khoronzhuk #define TS_130              BIT(10) /* Time Sync Dest IP Addr 130 enable */
2751239a96aSIvan Khoronzhuk #define TS_129              BIT(9)  /* Time Sync Dest IP Addr 129 enable */
2761239a96aSIvan Khoronzhuk #define TS_TTL_NONZERO      BIT(8)  /* Time Sync Time To Live Non-zero enable */
2771239a96aSIvan Khoronzhuk #define TS_ANNEX_F_EN       BIT(6)  /* Time Sync Annex F enable */
2781239a96aSIvan Khoronzhuk #define TS_ANNEX_D_EN       BIT(4)  /* Time Sync Annex D enable */
2791239a96aSIvan Khoronzhuk #define TS_LTYPE2_EN        BIT(3)  /* Time Sync LTYPE 2 enable */
2801239a96aSIvan Khoronzhuk #define TS_LTYPE1_EN        BIT(2)  /* Time Sync LTYPE 1 enable */
2811239a96aSIvan Khoronzhuk #define TS_TX_EN            BIT(1)  /* Time Sync Transmit Enable */
2821239a96aSIvan Khoronzhuk #define TS_RX_EN            BIT(0)  /* Time Sync Receive Enable */
2839750a3adSRichard Cochran 
28409c55372SGeorge Cherian #define CTRL_V2_TS_BITS \
28509c55372SGeorge Cherian 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
28609c55372SGeorge Cherian 	 TS_TTL_NONZERO  | TS_ANNEX_D_EN | TS_LTYPE1_EN)
2879750a3adSRichard Cochran 
28809c55372SGeorge Cherian #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
28909c55372SGeorge Cherian #define CTRL_V2_TX_TS_BITS  (CTRL_V2_TS_BITS | TS_TX_EN)
29009c55372SGeorge Cherian #define CTRL_V2_RX_TS_BITS  (CTRL_V2_TS_BITS | TS_RX_EN)
29109c55372SGeorge Cherian 
29209c55372SGeorge Cherian 
29309c55372SGeorge Cherian #define CTRL_V3_TS_BITS \
2941c0e8123SIvan Khoronzhuk 	(TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
29509c55372SGeorge Cherian 	 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
29609c55372SGeorge Cherian 	 TS_LTYPE1_EN)
29709c55372SGeorge Cherian 
29809c55372SGeorge Cherian #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
29909c55372SGeorge Cherian #define CTRL_V3_TX_TS_BITS  (CTRL_V3_TS_BITS | TS_TX_EN)
30009c55372SGeorge Cherian #define CTRL_V3_RX_TS_BITS  (CTRL_V3_TS_BITS | TS_RX_EN)
3019750a3adSRichard Cochran 
3029750a3adSRichard Cochran /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
3039750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_SHIFT   (16)    /* Time Sync Sequence ID Offset */
3049750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_MASK    (0x3f)
3059750a3adSRichard Cochran #define TS_MSG_TYPE_EN_SHIFT     (0)     /* Time Sync Message Type Enable */
3069750a3adSRichard Cochran #define TS_MSG_TYPE_EN_MASK      (0xffff)
3079750a3adSRichard Cochran 
3089750a3adSRichard Cochran /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
3099750a3adSRichard Cochran #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
310df828598SMugunthan V N 
3112e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_CTL register */
3122e5b38abSRichard Cochran #define CPSW_V1_TS_RX_EN		BIT(0)
3132e5b38abSRichard Cochran #define CPSW_V1_TS_TX_EN		BIT(4)
3142e5b38abSRichard Cochran #define CPSW_V1_MSG_TYPE_OFS		16
3152e5b38abSRichard Cochran 
3162e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
3172e5b38abSRichard Cochran #define CPSW_V1_SEQ_ID_OFS_SHIFT	16
3182e5b38abSRichard Cochran 
31948f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX		15
32048f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX_SHIFT		4
32148f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_RX		5
32248f5bcccSGrygorii Strashko 
323df828598SMugunthan V N struct cpsw_host_regs {
324df828598SMugunthan V N 	u32	max_blks;
325df828598SMugunthan V N 	u32	blk_cnt;
326d9ba8f9eSMugunthan V N 	u32	tx_in_ctl;
327df828598SMugunthan V N 	u32	port_vlan;
328df828598SMugunthan V N 	u32	tx_pri_map;
329df828598SMugunthan V N 	u32	cpdma_tx_pri_map;
330df828598SMugunthan V N 	u32	cpdma_rx_chan_map;
331df828598SMugunthan V N };
332df828598SMugunthan V N 
333df828598SMugunthan V N struct cpsw_sliver_regs {
334df828598SMugunthan V N 	u32	id_ver;
335df828598SMugunthan V N 	u32	mac_control;
336df828598SMugunthan V N 	u32	mac_status;
337df828598SMugunthan V N 	u32	soft_reset;
338df828598SMugunthan V N 	u32	rx_maxlen;
339df828598SMugunthan V N 	u32	__reserved_0;
340df828598SMugunthan V N 	u32	rx_pause;
341df828598SMugunthan V N 	u32	tx_pause;
342df828598SMugunthan V N 	u32	__reserved_1;
343df828598SMugunthan V N 	u32	rx_pri_map;
344df828598SMugunthan V N };
345df828598SMugunthan V N 
346d9718546SMugunthan V N struct cpsw_hw_stats {
347d9718546SMugunthan V N 	u32	rxgoodframes;
348d9718546SMugunthan V N 	u32	rxbroadcastframes;
349d9718546SMugunthan V N 	u32	rxmulticastframes;
350d9718546SMugunthan V N 	u32	rxpauseframes;
351d9718546SMugunthan V N 	u32	rxcrcerrors;
352d9718546SMugunthan V N 	u32	rxaligncodeerrors;
353d9718546SMugunthan V N 	u32	rxoversizedframes;
354d9718546SMugunthan V N 	u32	rxjabberframes;
355d9718546SMugunthan V N 	u32	rxundersizedframes;
356d9718546SMugunthan V N 	u32	rxfragments;
357d9718546SMugunthan V N 	u32	__pad_0[2];
358d9718546SMugunthan V N 	u32	rxoctets;
359d9718546SMugunthan V N 	u32	txgoodframes;
360d9718546SMugunthan V N 	u32	txbroadcastframes;
361d9718546SMugunthan V N 	u32	txmulticastframes;
362d9718546SMugunthan V N 	u32	txpauseframes;
363d9718546SMugunthan V N 	u32	txdeferredframes;
364d9718546SMugunthan V N 	u32	txcollisionframes;
365d9718546SMugunthan V N 	u32	txsinglecollframes;
366d9718546SMugunthan V N 	u32	txmultcollframes;
367d9718546SMugunthan V N 	u32	txexcessivecollisions;
368d9718546SMugunthan V N 	u32	txlatecollisions;
369d9718546SMugunthan V N 	u32	txunderrun;
370d9718546SMugunthan V N 	u32	txcarriersenseerrors;
371d9718546SMugunthan V N 	u32	txoctets;
372d9718546SMugunthan V N 	u32	octetframes64;
373d9718546SMugunthan V N 	u32	octetframes65t127;
374d9718546SMugunthan V N 	u32	octetframes128t255;
375d9718546SMugunthan V N 	u32	octetframes256t511;
376d9718546SMugunthan V N 	u32	octetframes512t1023;
377d9718546SMugunthan V N 	u32	octetframes1024tup;
378d9718546SMugunthan V N 	u32	netoctets;
379d9718546SMugunthan V N 	u32	rxsofoverruns;
380d9718546SMugunthan V N 	u32	rxmofoverruns;
381d9718546SMugunthan V N 	u32	rxdmaoverruns;
382d9718546SMugunthan V N };
383d9718546SMugunthan V N 
3842c8a14d6SGrygorii Strashko struct cpsw_slave_data {
3852c8a14d6SGrygorii Strashko 	struct device_node *phy_node;
3862c8a14d6SGrygorii Strashko 	char		phy_id[MII_BUS_ID_SIZE];
3872c8a14d6SGrygorii Strashko 	int		phy_if;
3882c8a14d6SGrygorii Strashko 	u8		mac_addr[ETH_ALEN];
3892c8a14d6SGrygorii Strashko 	u16		dual_emac_res_vlan;	/* Reserved VLAN for DualEMAC */
3902c8a14d6SGrygorii Strashko };
3912c8a14d6SGrygorii Strashko 
3922c8a14d6SGrygorii Strashko struct cpsw_platform_data {
3932c8a14d6SGrygorii Strashko 	struct cpsw_slave_data	*slave_data;
3942c8a14d6SGrygorii Strashko 	u32	ss_reg_ofs;	/* Subsystem control register offset */
3952c8a14d6SGrygorii Strashko 	u32	channels;	/* number of cpdma channels (symmetric) */
3962c8a14d6SGrygorii Strashko 	u32	slaves;		/* number of slave cpgmac ports */
3972c8a14d6SGrygorii Strashko 	u32	active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
3982c8a14d6SGrygorii Strashko 	u32	ale_entries;	/* ale table size */
3992c8a14d6SGrygorii Strashko 	u32	bd_ram_size;  /*buffer descriptor ram size */
4002c8a14d6SGrygorii Strashko 	u32	mac_control;	/* Mac control register */
4012c8a14d6SGrygorii Strashko 	u16	default_vlan;	/* Def VLAN for ALE lookup in VLAN aware mode*/
4022c8a14d6SGrygorii Strashko 	bool	dual_emac;	/* Enable Dual EMAC mode */
4032c8a14d6SGrygorii Strashko };
4042c8a14d6SGrygorii Strashko 
405df828598SMugunthan V N struct cpsw_slave {
4069750a3adSRichard Cochran 	void __iomem			*regs;
407df828598SMugunthan V N 	struct cpsw_sliver_regs __iomem	*sliver;
408df828598SMugunthan V N 	int				slave_num;
409df828598SMugunthan V N 	u32				mac_control;
410df828598SMugunthan V N 	struct cpsw_slave_data		*data;
411df828598SMugunthan V N 	struct phy_device		*phy;
412d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
413d9ba8f9eSMugunthan V N 	u32				port_vlan;
414df828598SMugunthan V N };
415df828598SMugunthan V N 
4169750a3adSRichard Cochran static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
4179750a3adSRichard Cochran {
418dda5f5feSGrygorii Strashko 	return readl_relaxed(slave->regs + offset);
4199750a3adSRichard Cochran }
4209750a3adSRichard Cochran 
4219750a3adSRichard Cochran static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
4229750a3adSRichard Cochran {
423dda5f5feSGrygorii Strashko 	writel_relaxed(val, slave->regs + offset);
4249750a3adSRichard Cochran }
4259750a3adSRichard Cochran 
4268feb0a19SIvan Khoronzhuk struct cpsw_vector {
4278feb0a19SIvan Khoronzhuk 	struct cpdma_chan *ch;
4288feb0a19SIvan Khoronzhuk 	int budget;
4298feb0a19SIvan Khoronzhuk };
4308feb0a19SIvan Khoronzhuk 
431649a1688SIvan Khoronzhuk struct cpsw_common {
43256e31bd8SIvan Khoronzhuk 	struct device			*dev;
433606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	data;
434dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_rx;
435dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_tx;
4365d8d0d4dSIvan Khoronzhuk 	struct cpsw_ss_regs __iomem	*regs;
4375d8d0d4dSIvan Khoronzhuk 	struct cpsw_wr_regs __iomem	*wr_regs;
4385d8d0d4dSIvan Khoronzhuk 	u8 __iomem			*hw_stats;
4395d8d0d4dSIvan Khoronzhuk 	struct cpsw_host_regs __iomem	*host_port_regs;
4402a05a622SIvan Khoronzhuk 	u32				version;
4412a05a622SIvan Khoronzhuk 	u32				coal_intvl;
4422a05a622SIvan Khoronzhuk 	u32				bus_freq_mhz;
4432a05a622SIvan Khoronzhuk 	int				rx_packet_max;
444606f3993SIvan Khoronzhuk 	struct cpsw_slave		*slaves;
4452c836bd9SIvan Khoronzhuk 	struct cpdma_ctlr		*dma;
4468feb0a19SIvan Khoronzhuk 	struct cpsw_vector		txv[CPSW_MAX_QUEUES];
4478feb0a19SIvan Khoronzhuk 	struct cpsw_vector		rxv[CPSW_MAX_QUEUES];
4482a05a622SIvan Khoronzhuk 	struct cpsw_ale			*ale;
449e38b5a3dSIvan Khoronzhuk 	bool				quirk_irq;
450e38b5a3dSIvan Khoronzhuk 	bool				rx_irq_disabled;
451e38b5a3dSIvan Khoronzhuk 	bool				tx_irq_disabled;
452e38b5a3dSIvan Khoronzhuk 	u32 irqs_table[IRQ_NUM];
4532a05a622SIvan Khoronzhuk 	struct cpts			*cpts;
454e05107e6SIvan Khoronzhuk 	int				rx_ch_num, tx_ch_num;
4550be01b8eSIvan Khoronzhuk 	int				speed;
456d5bc1613SIvan Khoronzhuk 	int				usage_count;
457649a1688SIvan Khoronzhuk };
458649a1688SIvan Khoronzhuk 
459649a1688SIvan Khoronzhuk struct cpsw_priv {
460df828598SMugunthan V N 	struct net_device		*ndev;
461df828598SMugunthan V N 	struct device			*dev;
462df828598SMugunthan V N 	u32				msg_enable;
463df828598SMugunthan V N 	u8				mac_addr[ETH_ALEN];
4641923d6e4SMugunthan V N 	bool				rx_pause;
4651923d6e4SMugunthan V N 	bool				tx_pause;
4667929a668SIvan Khoronzhuk 	bool				mqprio_hw;
46757d90148SIvan Khoronzhuk 	int				fifo_bw[CPSW_TC_NUM];
46857d90148SIvan Khoronzhuk 	int				shp_cfg_speed;
469d9ba8f9eSMugunthan V N 	u32 emac_port;
470649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw;
471df828598SMugunthan V N };
472df828598SMugunthan V N 
473d9718546SMugunthan V N struct cpsw_stats {
474d9718546SMugunthan V N 	char stat_string[ETH_GSTRING_LEN];
475d9718546SMugunthan V N 	int type;
476d9718546SMugunthan V N 	int sizeof_stat;
477d9718546SMugunthan V N 	int stat_offset;
478d9718546SMugunthan V N };
479d9718546SMugunthan V N 
480d9718546SMugunthan V N enum {
481d9718546SMugunthan V N 	CPSW_STATS,
482d9718546SMugunthan V N 	CPDMA_RX_STATS,
483d9718546SMugunthan V N 	CPDMA_TX_STATS,
484d9718546SMugunthan V N };
485d9718546SMugunthan V N 
486d9718546SMugunthan V N #define CPSW_STAT(m)		CPSW_STATS,				\
487a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpsw_hw_stats, m), \
488d9718546SMugunthan V N 				offsetof(struct cpsw_hw_stats, m)
489d9718546SMugunthan V N #define CPDMA_RX_STAT(m)	CPDMA_RX_STATS,				   \
490a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpdma_chan_stats, m), \
491d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
492d9718546SMugunthan V N #define CPDMA_TX_STAT(m)	CPDMA_TX_STATS,				   \
493a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpdma_chan_stats, m), \
494d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
495d9718546SMugunthan V N 
496d9718546SMugunthan V N static const struct cpsw_stats cpsw_gstrings_stats[] = {
497d9718546SMugunthan V N 	{ "Good Rx Frames", CPSW_STAT(rxgoodframes) },
498d9718546SMugunthan V N 	{ "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
499d9718546SMugunthan V N 	{ "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
500d9718546SMugunthan V N 	{ "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
501d9718546SMugunthan V N 	{ "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
502d9718546SMugunthan V N 	{ "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
503d9718546SMugunthan V N 	{ "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
504d9718546SMugunthan V N 	{ "Rx Jabbers", CPSW_STAT(rxjabberframes) },
505d9718546SMugunthan V N 	{ "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
506d9718546SMugunthan V N 	{ "Rx Fragments", CPSW_STAT(rxfragments) },
507d9718546SMugunthan V N 	{ "Rx Octets", CPSW_STAT(rxoctets) },
508d9718546SMugunthan V N 	{ "Good Tx Frames", CPSW_STAT(txgoodframes) },
509d9718546SMugunthan V N 	{ "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
510d9718546SMugunthan V N 	{ "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
511d9718546SMugunthan V N 	{ "Pause Tx Frames", CPSW_STAT(txpauseframes) },
512d9718546SMugunthan V N 	{ "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
513d9718546SMugunthan V N 	{ "Collisions", CPSW_STAT(txcollisionframes) },
514d9718546SMugunthan V N 	{ "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
515d9718546SMugunthan V N 	{ "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
516d9718546SMugunthan V N 	{ "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
517d9718546SMugunthan V N 	{ "Late Collisions", CPSW_STAT(txlatecollisions) },
518d9718546SMugunthan V N 	{ "Tx Underrun", CPSW_STAT(txunderrun) },
519d9718546SMugunthan V N 	{ "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
520d9718546SMugunthan V N 	{ "Tx Octets", CPSW_STAT(txoctets) },
521d9718546SMugunthan V N 	{ "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
522d9718546SMugunthan V N 	{ "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
523d9718546SMugunthan V N 	{ "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
524d9718546SMugunthan V N 	{ "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
525d9718546SMugunthan V N 	{ "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
526d9718546SMugunthan V N 	{ "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
527d9718546SMugunthan V N 	{ "Net Octets", CPSW_STAT(netoctets) },
528d9718546SMugunthan V N 	{ "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
529d9718546SMugunthan V N 	{ "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
530d9718546SMugunthan V N 	{ "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
531d9718546SMugunthan V N };
532d9718546SMugunthan V N 
533e05107e6SIvan Khoronzhuk static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
534e05107e6SIvan Khoronzhuk 	{ "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
535e05107e6SIvan Khoronzhuk 	{ "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
536e05107e6SIvan Khoronzhuk 	{ "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
537e05107e6SIvan Khoronzhuk 	{ "misqueued", CPDMA_RX_STAT(misqueued) },
538e05107e6SIvan Khoronzhuk 	{ "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
539e05107e6SIvan Khoronzhuk 	{ "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
540e05107e6SIvan Khoronzhuk 	{ "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
541e05107e6SIvan Khoronzhuk 	{ "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
542e05107e6SIvan Khoronzhuk 	{ "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
543e05107e6SIvan Khoronzhuk 	{ "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
544e05107e6SIvan Khoronzhuk 	{ "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
545e05107e6SIvan Khoronzhuk 	{ "requeue", CPDMA_RX_STAT(requeue) },
546e05107e6SIvan Khoronzhuk 	{ "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
547e05107e6SIvan Khoronzhuk };
548e05107e6SIvan Khoronzhuk 
549e05107e6SIvan Khoronzhuk #define CPSW_STATS_COMMON_LEN	ARRAY_SIZE(cpsw_gstrings_stats)
550e05107e6SIvan Khoronzhuk #define CPSW_STATS_CH_LEN	ARRAY_SIZE(cpsw_gstrings_ch_stats)
551d9718546SMugunthan V N 
552649a1688SIvan Khoronzhuk #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
553dbc4ec52SIvan Khoronzhuk #define napi_to_cpsw(napi)	container_of(napi, struct cpsw_common, napi)
554df828598SMugunthan V N #define for_each_slave(priv, func, arg...)				\
555df828598SMugunthan V N 	do {								\
5566e6ceaedSSebastian Siewior 		struct cpsw_slave *slave;				\
557606f3993SIvan Khoronzhuk 		struct cpsw_common *cpsw = (priv)->cpsw;		\
5586e6ceaedSSebastian Siewior 		int n;							\
559606f3993SIvan Khoronzhuk 		if (cpsw->data.dual_emac)				\
560606f3993SIvan Khoronzhuk 			(func)((cpsw)->slaves + priv->emac_port, ##arg);\
561d9ba8f9eSMugunthan V N 		else							\
562606f3993SIvan Khoronzhuk 			for (n = cpsw->data.slaves,			\
563606f3993SIvan Khoronzhuk 					slave = cpsw->slaves;		\
5646e6ceaedSSebastian Siewior 					n; n--)				\
5656e6ceaedSSebastian Siewior 				(func)(slave++, ##arg);			\
566df828598SMugunthan V N 	} while (0)
567d9ba8f9eSMugunthan V N 
5686f1f5836SIvan Khoronzhuk static inline int cpsw_get_slave_port(u32 slave_num)
569d9ba8f9eSMugunthan V N {
570d9ba8f9eSMugunthan V N 	return slave_num + 1;
571d9ba8f9eSMugunthan V N }
572df828598SMugunthan V N 
5735da19489SIvan Khoronzhuk static void cpsw_add_mcast(struct cpsw_priv *priv, const u8 *addr)
574fea49f60SIvan Khoronzhuk {
575fea49f60SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
576fea49f60SIvan Khoronzhuk 
577fea49f60SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
578fea49f60SIvan Khoronzhuk 		struct cpsw_slave *slave = cpsw->slaves + priv->emac_port;
579fea49f60SIvan Khoronzhuk 
5805b3a5a14SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, addr, ALE_PORT_HOST,
581fea49f60SIvan Khoronzhuk 				   ALE_VLAN, slave->port_vlan, 0);
582fea49f60SIvan Khoronzhuk 		return;
583fea49f60SIvan Khoronzhuk 	}
584fea49f60SIvan Khoronzhuk 
585fea49f60SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, addr, ALE_ALL_PORTS, 0, 0, 0);
586fea49f60SIvan Khoronzhuk }
587fea49f60SIvan Khoronzhuk 
5880cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
5890cd8f9ccSMugunthan V N {
5902a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
5912a05a622SIvan Khoronzhuk 	struct cpsw_ale *ale = cpsw->ale;
5920cd8f9ccSMugunthan V N 	int i;
5930cd8f9ccSMugunthan V N 
594606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
5950cd8f9ccSMugunthan V N 		bool flag = false;
5960cd8f9ccSMugunthan V N 
5970cd8f9ccSMugunthan V N 		/* Enabling promiscuous mode for one interface will be
5980cd8f9ccSMugunthan V N 		 * common for both the interface as the interface shares
5990cd8f9ccSMugunthan V N 		 * the same hardware resource.
6000cd8f9ccSMugunthan V N 		 */
601606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++)
602606f3993SIvan Khoronzhuk 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
6030cd8f9ccSMugunthan V N 				flag = true;
6040cd8f9ccSMugunthan V N 
6050cd8f9ccSMugunthan V N 		if (!enable && flag) {
6060cd8f9ccSMugunthan V N 			enable = true;
6070cd8f9ccSMugunthan V N 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
6080cd8f9ccSMugunthan V N 		}
6090cd8f9ccSMugunthan V N 
6100cd8f9ccSMugunthan V N 		if (enable) {
6110cd8f9ccSMugunthan V N 			/* Enable Bypass */
6120cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
6130cd8f9ccSMugunthan V N 
6140cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6150cd8f9ccSMugunthan V N 		} else {
6160cd8f9ccSMugunthan V N 			/* Disable Bypass */
6170cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
6180cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6190cd8f9ccSMugunthan V N 		}
6200cd8f9ccSMugunthan V N 	} else {
6210cd8f9ccSMugunthan V N 		if (enable) {
6220cd8f9ccSMugunthan V N 			unsigned long timeout = jiffies + HZ;
6230cd8f9ccSMugunthan V N 
6246f979eb3SLennart Sorensen 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
625606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6260cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6270cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 1);
6280cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6290cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 1);
6300cd8f9ccSMugunthan V N 			}
6310cd8f9ccSMugunthan V N 
6320cd8f9ccSMugunthan V N 			/* Clear All Untouched entries */
6330cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6340cd8f9ccSMugunthan V N 			do {
6350cd8f9ccSMugunthan V N 				cpu_relax();
6360cd8f9ccSMugunthan V N 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
6370cd8f9ccSMugunthan V N 					break;
6380cd8f9ccSMugunthan V N 			} while (time_after(timeout, jiffies));
6390cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6400cd8f9ccSMugunthan V N 
6410cd8f9ccSMugunthan V N 			/* Clear all mcast from ALE */
64261f1cef9SGrygorii Strashko 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
6430cd8f9ccSMugunthan V N 
6440cd8f9ccSMugunthan V N 			/* Flood All Unicast Packets to Host port */
6450cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
6460cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6470cd8f9ccSMugunthan V N 		} else {
6486f979eb3SLennart Sorensen 			/* Don't Flood All Unicast Packets to Host port */
6490cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
6500cd8f9ccSMugunthan V N 
6516f979eb3SLennart Sorensen 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
652606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6530cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6540cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 0);
6550cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6560cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 0);
6570cd8f9ccSMugunthan V N 			}
6580cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6590cd8f9ccSMugunthan V N 		}
6600cd8f9ccSMugunthan V N 	}
6610cd8f9ccSMugunthan V N }
6620cd8f9ccSMugunthan V N 
6635da19489SIvan Khoronzhuk static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr)
6645da19489SIvan Khoronzhuk {
6655da19489SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
6665da19489SIvan Khoronzhuk 
6675da19489SIvan Khoronzhuk 	cpsw_add_mcast(priv, addr);
6685da19489SIvan Khoronzhuk 	return 0;
6695da19489SIvan Khoronzhuk }
6705da19489SIvan Khoronzhuk 
6715da19489SIvan Khoronzhuk static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr)
6725c50a856SMugunthan V N {
6735c50a856SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
674606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
6755da19489SIvan Khoronzhuk 	int vid, flags;
67625906052SMugunthan V N 
6775da19489SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
678606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
6795da19489SIvan Khoronzhuk 		flags = ALE_VLAN;
6805da19489SIvan Khoronzhuk 	} else {
6815da19489SIvan Khoronzhuk 		vid = 0;
6825da19489SIvan Khoronzhuk 		flags = 0;
6835da19489SIvan Khoronzhuk 	}
6845da19489SIvan Khoronzhuk 
6855da19489SIvan Khoronzhuk 	cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
6865da19489SIvan Khoronzhuk 	return 0;
6875da19489SIvan Khoronzhuk }
6885da19489SIvan Khoronzhuk 
6895da19489SIvan Khoronzhuk static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
6905da19489SIvan Khoronzhuk {
6915da19489SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
6925c50a856SMugunthan V N 
6935c50a856SMugunthan V N 	if (ndev->flags & IFF_PROMISC) {
6945c50a856SMugunthan V N 		/* Enable promiscuous mode */
6950cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, true);
6962a05a622SIvan Khoronzhuk 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
6975c50a856SMugunthan V N 		return;
6980cd8f9ccSMugunthan V N 	} else {
6990cd8f9ccSMugunthan V N 		/* Disable promiscuous mode */
7000cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, false);
7015c50a856SMugunthan V N 	}
7025c50a856SMugunthan V N 
7031e5c4bc4SLennart Sorensen 	/* Restore allmulti on vlans if necessary */
7045da19489SIvan Khoronzhuk 	cpsw_ale_set_allmulti(cpsw->ale, ndev->flags & IFF_ALLMULTI);
7051e5c4bc4SLennart Sorensen 
7065da19489SIvan Khoronzhuk 	__dev_mc_sync(ndev, cpsw_add_mc_addr, cpsw_del_mc_addr);
7075c50a856SMugunthan V N }
7085c50a856SMugunthan V N 
7092c836bd9SIvan Khoronzhuk static void cpsw_intr_enable(struct cpsw_common *cpsw)
710df828598SMugunthan V N {
711dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
712dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
713df828598SMugunthan V N 
7142c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
715df828598SMugunthan V N 	return;
716df828598SMugunthan V N }
717df828598SMugunthan V N 
7182c836bd9SIvan Khoronzhuk static void cpsw_intr_disable(struct cpsw_common *cpsw)
719df828598SMugunthan V N {
720dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
721dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
722df828598SMugunthan V N 
7232c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
724df828598SMugunthan V N 	return;
725df828598SMugunthan V N }
726df828598SMugunthan V N 
7271a3b5056SOlof Johansson static void cpsw_tx_handler(void *token, int len, int status)
728df828598SMugunthan V N {
729e05107e6SIvan Khoronzhuk 	struct netdev_queue	*txq;
730df828598SMugunthan V N 	struct sk_buff		*skb = token;
731df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
7322a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
733df828598SMugunthan V N 
734fae50823SMugunthan V N 	/* Check whether the queue is stopped due to stalled tx dma, if the
735fae50823SMugunthan V N 	 * queue is stopped then start the queue as we have free desc for tx
736fae50823SMugunthan V N 	 */
737e05107e6SIvan Khoronzhuk 	txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
738e05107e6SIvan Khoronzhuk 	if (unlikely(netif_tx_queue_stopped(txq)))
739e05107e6SIvan Khoronzhuk 		netif_tx_wake_queue(txq);
740e05107e6SIvan Khoronzhuk 
7412a05a622SIvan Khoronzhuk 	cpts_tx_timestamp(cpsw->cpts, skb);
7428dc43ddcSTobias Klauser 	ndev->stats.tx_packets++;
7438dc43ddcSTobias Klauser 	ndev->stats.tx_bytes += len;
744df828598SMugunthan V N 	dev_kfree_skb_any(skb);
745df828598SMugunthan V N }
746df828598SMugunthan V N 
747a3a41d2fSGrygorii Strashko static void cpsw_rx_vlan_encap(struct sk_buff *skb)
748a3a41d2fSGrygorii Strashko {
749a3a41d2fSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(skb->dev);
750a3a41d2fSGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
751a3a41d2fSGrygorii Strashko 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
752a3a41d2fSGrygorii Strashko 	u16 vtag, vid, prio, pkt_type;
753a3a41d2fSGrygorii Strashko 
754a3a41d2fSGrygorii Strashko 	/* Remove VLAN header encapsulation word */
755a3a41d2fSGrygorii Strashko 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
756a3a41d2fSGrygorii Strashko 
757a3a41d2fSGrygorii Strashko 	pkt_type = (rx_vlan_encap_hdr >>
758a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
759a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
760a3a41d2fSGrygorii Strashko 	/* Ignore unknown & Priority-tagged packets*/
761a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
762a3a41d2fSGrygorii Strashko 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
763a3a41d2fSGrygorii Strashko 		return;
764a3a41d2fSGrygorii Strashko 
765a3a41d2fSGrygorii Strashko 	vid = (rx_vlan_encap_hdr >>
766a3a41d2fSGrygorii Strashko 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
767a3a41d2fSGrygorii Strashko 	       VLAN_VID_MASK;
768a3a41d2fSGrygorii Strashko 	/* Ignore vid 0 and pass packet as is */
769a3a41d2fSGrygorii Strashko 	if (!vid)
770a3a41d2fSGrygorii Strashko 		return;
771a3a41d2fSGrygorii Strashko 	/* Ignore default vlans in dual mac mode */
772a3a41d2fSGrygorii Strashko 	if (cpsw->data.dual_emac &&
773a3a41d2fSGrygorii Strashko 	    vid == cpsw->slaves[priv->emac_port].port_vlan)
774a3a41d2fSGrygorii Strashko 		return;
775a3a41d2fSGrygorii Strashko 
776a3a41d2fSGrygorii Strashko 	prio = (rx_vlan_encap_hdr >>
777a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
778a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
779a3a41d2fSGrygorii Strashko 
780a3a41d2fSGrygorii Strashko 	vtag = (prio << VLAN_PRIO_SHIFT) | vid;
781a3a41d2fSGrygorii Strashko 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
782a3a41d2fSGrygorii Strashko 
783a3a41d2fSGrygorii Strashko 	/* strip vlan tag for VLAN-tagged packet */
784a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
785a3a41d2fSGrygorii Strashko 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
786a3a41d2fSGrygorii Strashko 		skb_pull(skb, VLAN_HLEN);
787a3a41d2fSGrygorii Strashko 	}
788a3a41d2fSGrygorii Strashko }
789a3a41d2fSGrygorii Strashko 
7901a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status)
791df828598SMugunthan V N {
792e05107e6SIvan Khoronzhuk 	struct cpdma_chan	*ch;
793df828598SMugunthan V N 	struct sk_buff		*skb = token;
794b4727e69SSebastian Siewior 	struct sk_buff		*new_skb;
795df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
796fea49f60SIvan Khoronzhuk 	int			ret = 0, port;
7972a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
798df828598SMugunthan V N 
799fea49f60SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
800fea49f60SIvan Khoronzhuk 		port = CPDMA_RX_SOURCE_PORT(status);
801fea49f60SIvan Khoronzhuk 		if (port) {
802fea49f60SIvan Khoronzhuk 			ndev = cpsw->slaves[--port].ndev;
803fea49f60SIvan Khoronzhuk 			skb->dev = ndev;
804fea49f60SIvan Khoronzhuk 		}
805fea49f60SIvan Khoronzhuk 	}
806d9ba8f9eSMugunthan V N 
80716e5c57dSMugunthan V N 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
808a0e2c822SMugunthan V N 		/* In dual emac mode check for all interfaces */
809d5bc1613SIvan Khoronzhuk 		if (cpsw->data.dual_emac && cpsw->usage_count &&
810fe734d0aSIvan Khoronzhuk 		    (status >= 0)) {
811a0e2c822SMugunthan V N 			/* The packet received is for the interface which
812a0e2c822SMugunthan V N 			 * is already down and the other interface is up
813dbedd44eSJoe Perches 			 * and running, instead of freeing which results
814a0e2c822SMugunthan V N 			 * in reducing of the number of rx descriptor in
815a0e2c822SMugunthan V N 			 * DMA engine, requeue skb back to cpdma.
816a0e2c822SMugunthan V N 			 */
817a0e2c822SMugunthan V N 			new_skb = skb;
818a0e2c822SMugunthan V N 			goto requeue;
819a0e2c822SMugunthan V N 		}
820a0e2c822SMugunthan V N 
821b4727e69SSebastian Siewior 		/* the interface is going down, skbs are purged */
822df828598SMugunthan V N 		dev_kfree_skb_any(skb);
823df828598SMugunthan V N 		return;
824df828598SMugunthan V N 	}
825b4727e69SSebastian Siewior 
8262a05a622SIvan Khoronzhuk 	new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
827b4727e69SSebastian Siewior 	if (new_skb) {
828e05107e6SIvan Khoronzhuk 		skb_copy_queue_mapping(new_skb, skb);
829df828598SMugunthan V N 		skb_put(skb, len);
830a3a41d2fSGrygorii Strashko 		if (status & CPDMA_RX_VLAN_ENCAP)
831a3a41d2fSGrygorii Strashko 			cpsw_rx_vlan_encap(skb);
8322a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
833df828598SMugunthan V N 		skb->protocol = eth_type_trans(skb, ndev);
834df828598SMugunthan V N 		netif_receive_skb(skb);
8358dc43ddcSTobias Klauser 		ndev->stats.rx_bytes += len;
8368dc43ddcSTobias Klauser 		ndev->stats.rx_packets++;
837254a49d5SGrygorii Strashko 		kmemleak_not_leak(new_skb);
838b4727e69SSebastian Siewior 	} else {
8398dc43ddcSTobias Klauser 		ndev->stats.rx_dropped++;
840b4727e69SSebastian Siewior 		new_skb = skb;
841df828598SMugunthan V N 	}
842df828598SMugunthan V N 
843a0e2c822SMugunthan V N requeue:
844ce52c744SIvan Khoronzhuk 	if (netif_dormant(ndev)) {
845ce52c744SIvan Khoronzhuk 		dev_kfree_skb_any(new_skb);
846ce52c744SIvan Khoronzhuk 		return;
847ce52c744SIvan Khoronzhuk 	}
848ce52c744SIvan Khoronzhuk 
8498feb0a19SIvan Khoronzhuk 	ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
850e05107e6SIvan Khoronzhuk 	ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
851b4727e69SSebastian Siewior 				skb_tailroom(new_skb), 0);
852b4727e69SSebastian Siewior 	if (WARN_ON(ret < 0))
853b4727e69SSebastian Siewior 		dev_kfree_skb_any(new_skb);
854df828598SMugunthan V N }
855df828598SMugunthan V N 
85632b78d85SIvan Khoronzhuk static void cpsw_split_res(struct net_device *ndev)
85748e0a83eSIvan Khoronzhuk {
85848e0a83eSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
85932b78d85SIvan Khoronzhuk 	u32 consumed_rate = 0, bigest_rate = 0;
86048e0a83eSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
86148e0a83eSIvan Khoronzhuk 	struct cpsw_vector *txv = cpsw->txv;
86232b78d85SIvan Khoronzhuk 	int i, ch_weight, rlim_ch_num = 0;
86348e0a83eSIvan Khoronzhuk 	int budget, bigest_rate_ch = 0;
86448e0a83eSIvan Khoronzhuk 	u32 ch_rate, max_rate;
86548e0a83eSIvan Khoronzhuk 	int ch_budget = 0;
86648e0a83eSIvan Khoronzhuk 
86748e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
86848e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
86948e0a83eSIvan Khoronzhuk 		if (!ch_rate)
87048e0a83eSIvan Khoronzhuk 			continue;
87148e0a83eSIvan Khoronzhuk 
87248e0a83eSIvan Khoronzhuk 		rlim_ch_num++;
87348e0a83eSIvan Khoronzhuk 		consumed_rate += ch_rate;
87448e0a83eSIvan Khoronzhuk 	}
87548e0a83eSIvan Khoronzhuk 
87648e0a83eSIvan Khoronzhuk 	if (cpsw->tx_ch_num == rlim_ch_num) {
87748e0a83eSIvan Khoronzhuk 		max_rate = consumed_rate;
87832b78d85SIvan Khoronzhuk 	} else if (!rlim_ch_num) {
87932b78d85SIvan Khoronzhuk 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
88032b78d85SIvan Khoronzhuk 		bigest_rate = 0;
88132b78d85SIvan Khoronzhuk 		max_rate = consumed_rate;
88248e0a83eSIvan Khoronzhuk 	} else {
8830be01b8eSIvan Khoronzhuk 		max_rate = cpsw->speed * 1000;
8840be01b8eSIvan Khoronzhuk 
8850be01b8eSIvan Khoronzhuk 		/* if max_rate is less then expected due to reduced link speed,
8860be01b8eSIvan Khoronzhuk 		 * split proportionally according next potential max speed
8870be01b8eSIvan Khoronzhuk 		 */
8880be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8890be01b8eSIvan Khoronzhuk 			max_rate *= 10;
8900be01b8eSIvan Khoronzhuk 
8910be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8920be01b8eSIvan Khoronzhuk 			max_rate *= 10;
89332b78d85SIvan Khoronzhuk 
89448e0a83eSIvan Khoronzhuk 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
89548e0a83eSIvan Khoronzhuk 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
89648e0a83eSIvan Khoronzhuk 			    (cpsw->tx_ch_num - rlim_ch_num);
89748e0a83eSIvan Khoronzhuk 		bigest_rate = (max_rate - consumed_rate) /
89848e0a83eSIvan Khoronzhuk 			      (cpsw->tx_ch_num - rlim_ch_num);
89948e0a83eSIvan Khoronzhuk 	}
90048e0a83eSIvan Khoronzhuk 
90132b78d85SIvan Khoronzhuk 	/* split tx weight/budget */
90248e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
90348e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
90448e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
90548e0a83eSIvan Khoronzhuk 		if (ch_rate) {
90648e0a83eSIvan Khoronzhuk 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
90748e0a83eSIvan Khoronzhuk 			if (!txv[i].budget)
90832b78d85SIvan Khoronzhuk 				txv[i].budget++;
90948e0a83eSIvan Khoronzhuk 			if (ch_rate > bigest_rate) {
91048e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
91148e0a83eSIvan Khoronzhuk 				bigest_rate = ch_rate;
91248e0a83eSIvan Khoronzhuk 			}
91332b78d85SIvan Khoronzhuk 
91432b78d85SIvan Khoronzhuk 			ch_weight = (ch_rate * 100) / max_rate;
91532b78d85SIvan Khoronzhuk 			if (!ch_weight)
91632b78d85SIvan Khoronzhuk 				ch_weight++;
91732b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
91848e0a83eSIvan Khoronzhuk 		} else {
91948e0a83eSIvan Khoronzhuk 			txv[i].budget = ch_budget;
92048e0a83eSIvan Khoronzhuk 			if (!bigest_rate_ch)
92148e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
92232b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
92348e0a83eSIvan Khoronzhuk 		}
92448e0a83eSIvan Khoronzhuk 
92548e0a83eSIvan Khoronzhuk 		budget -= txv[i].budget;
92648e0a83eSIvan Khoronzhuk 	}
92748e0a83eSIvan Khoronzhuk 
92848e0a83eSIvan Khoronzhuk 	if (budget)
92948e0a83eSIvan Khoronzhuk 		txv[bigest_rate_ch].budget += budget;
93048e0a83eSIvan Khoronzhuk 
93148e0a83eSIvan Khoronzhuk 	/* split rx budget */
93248e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
93348e0a83eSIvan Khoronzhuk 	ch_budget = budget / cpsw->rx_ch_num;
93448e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->rx_ch_num; i++) {
93548e0a83eSIvan Khoronzhuk 		cpsw->rxv[i].budget = ch_budget;
93648e0a83eSIvan Khoronzhuk 		budget -= ch_budget;
93748e0a83eSIvan Khoronzhuk 	}
93848e0a83eSIvan Khoronzhuk 
93948e0a83eSIvan Khoronzhuk 	if (budget)
94048e0a83eSIvan Khoronzhuk 		cpsw->rxv[0].budget += budget;
94148e0a83eSIvan Khoronzhuk }
94248e0a83eSIvan Khoronzhuk 
943c03abd84SFelipe Balbi static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
944df828598SMugunthan V N {
945dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
9467ce67a38SFelipe Balbi 
9475d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->tx_en);
9482c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
949c03abd84SFelipe Balbi 
950e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
951e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[1]);
952e38b5a3dSIvan Khoronzhuk 		cpsw->tx_irq_disabled = true;
9537da11600SMugunthan V N 	}
9547da11600SMugunthan V N 
955dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_tx);
956c03abd84SFelipe Balbi 	return IRQ_HANDLED;
957c03abd84SFelipe Balbi }
958c03abd84SFelipe Balbi 
959c03abd84SFelipe Balbi static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
960c03abd84SFelipe Balbi {
961dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
962c03abd84SFelipe Balbi 
9632c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
9645d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->rx_en);
965fd51cf19SSebastian Siewior 
966e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
967e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[0]);
968e38b5a3dSIvan Khoronzhuk 		cpsw->rx_irq_disabled = true;
9697da11600SMugunthan V N 	}
9707da11600SMugunthan V N 
971dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_rx);
972df828598SMugunthan V N 	return IRQ_HANDLED;
973df828598SMugunthan V N }
974df828598SMugunthan V N 
9759611d6d6SIvan Khoronzhuk static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
976df828598SMugunthan V N {
977e05107e6SIvan Khoronzhuk 	u32			ch_map;
9788feb0a19SIvan Khoronzhuk 	int			num_tx, cur_budget, ch;
979dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
9808feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*txv;
98132a7432cSMugunthan V N 
982e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
983e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
98479b3325dSIvan Khoronzhuk 	for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
98579b3325dSIvan Khoronzhuk 		if (!(ch_map & 0x80))
986e05107e6SIvan Khoronzhuk 			continue;
987e05107e6SIvan Khoronzhuk 
9888feb0a19SIvan Khoronzhuk 		txv = &cpsw->txv[ch];
9898feb0a19SIvan Khoronzhuk 		if (unlikely(txv->budget > budget - num_tx))
9908feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_tx;
9918feb0a19SIvan Khoronzhuk 		else
9928feb0a19SIvan Khoronzhuk 			cur_budget = txv->budget;
9938feb0a19SIvan Khoronzhuk 
9948feb0a19SIvan Khoronzhuk 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
995342934a5SIvan Khoronzhuk 		if (num_tx >= budget)
996342934a5SIvan Khoronzhuk 			break;
997e05107e6SIvan Khoronzhuk 	}
998e05107e6SIvan Khoronzhuk 
99932a7432cSMugunthan V N 	if (num_tx < budget) {
100032a7432cSMugunthan V N 		napi_complete(napi_tx);
10015d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
10029611d6d6SIvan Khoronzhuk 	}
10039611d6d6SIvan Khoronzhuk 
10049611d6d6SIvan Khoronzhuk 	return num_tx;
10059611d6d6SIvan Khoronzhuk }
10069611d6d6SIvan Khoronzhuk 
10079611d6d6SIvan Khoronzhuk static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
10089611d6d6SIvan Khoronzhuk {
10099611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
10109611d6d6SIvan Khoronzhuk 	int num_tx;
10119611d6d6SIvan Khoronzhuk 
10129611d6d6SIvan Khoronzhuk 	num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
10139611d6d6SIvan Khoronzhuk 	if (num_tx < budget) {
10149611d6d6SIvan Khoronzhuk 		napi_complete(napi_tx);
10159611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
10169611d6d6SIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1017e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1018e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
10197da11600SMugunthan V N 		}
102032a7432cSMugunthan V N 	}
102132a7432cSMugunthan V N 
102232a7432cSMugunthan V N 	return num_tx;
102332a7432cSMugunthan V N }
102432a7432cSMugunthan V N 
10259611d6d6SIvan Khoronzhuk static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
102632a7432cSMugunthan V N {
1027e05107e6SIvan Khoronzhuk 	u32			ch_map;
10288feb0a19SIvan Khoronzhuk 	int			num_rx, cur_budget, ch;
1029dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
10308feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*rxv;
1031510a1e72SMugunthan V N 
1032e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
1033e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1034342934a5SIvan Khoronzhuk 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1035e05107e6SIvan Khoronzhuk 		if (!(ch_map & 0x01))
1036e05107e6SIvan Khoronzhuk 			continue;
1037e05107e6SIvan Khoronzhuk 
10388feb0a19SIvan Khoronzhuk 		rxv = &cpsw->rxv[ch];
10398feb0a19SIvan Khoronzhuk 		if (unlikely(rxv->budget > budget - num_rx))
10408feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_rx;
10418feb0a19SIvan Khoronzhuk 		else
10428feb0a19SIvan Khoronzhuk 			cur_budget = rxv->budget;
10438feb0a19SIvan Khoronzhuk 
10448feb0a19SIvan Khoronzhuk 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1045342934a5SIvan Khoronzhuk 		if (num_rx >= budget)
1046342934a5SIvan Khoronzhuk 			break;
1047e05107e6SIvan Khoronzhuk 	}
1048e05107e6SIvan Khoronzhuk 
1049510a1e72SMugunthan V N 	if (num_rx < budget) {
10506ad20165SEric Dumazet 		napi_complete_done(napi_rx, num_rx);
10515d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10529611d6d6SIvan Khoronzhuk 	}
10539611d6d6SIvan Khoronzhuk 
10549611d6d6SIvan Khoronzhuk 	return num_rx;
10559611d6d6SIvan Khoronzhuk }
10569611d6d6SIvan Khoronzhuk 
10579611d6d6SIvan Khoronzhuk static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
10589611d6d6SIvan Khoronzhuk {
10599611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
10609611d6d6SIvan Khoronzhuk 	int num_rx;
10619611d6d6SIvan Khoronzhuk 
10629611d6d6SIvan Khoronzhuk 	num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
10639611d6d6SIvan Khoronzhuk 	if (num_rx < budget) {
10649611d6d6SIvan Khoronzhuk 		napi_complete_done(napi_rx, num_rx);
10659611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10669611d6d6SIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1067e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1068e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
10697da11600SMugunthan V N 		}
1070510a1e72SMugunthan V N 	}
1071df828598SMugunthan V N 
1072df828598SMugunthan V N 	return num_rx;
1073df828598SMugunthan V N }
1074df828598SMugunthan V N 
1075df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg)
1076df828598SMugunthan V N {
1077df828598SMugunthan V N 	unsigned long timeout = jiffies + HZ;
1078df828598SMugunthan V N 
1079dda5f5feSGrygorii Strashko 	writel_relaxed(1, reg);
1080df828598SMugunthan V N 	do {
1081df828598SMugunthan V N 		cpu_relax();
1082dda5f5feSGrygorii Strashko 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1083df828598SMugunthan V N 
1084dda5f5feSGrygorii Strashko 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1085df828598SMugunthan V N }
1086df828598SMugunthan V N 
1087df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1088df828598SMugunthan V N 			       struct cpsw_priv *priv)
1089df828598SMugunthan V N {
10909750a3adSRichard Cochran 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
10919750a3adSRichard Cochran 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1092df828598SMugunthan V N }
1093df828598SMugunthan V N 
109457d90148SIvan Khoronzhuk static bool cpsw_shp_is_off(struct cpsw_priv *priv)
109557d90148SIvan Khoronzhuk {
109657d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
109757d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
109857d90148SIvan Khoronzhuk 	u32 shift, mask, val;
109957d90148SIvan Khoronzhuk 
110057d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
110157d90148SIvan Khoronzhuk 
110257d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
110357d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
110457d90148SIvan Khoronzhuk 	mask = 7 << shift;
110557d90148SIvan Khoronzhuk 	val = val & mask;
110657d90148SIvan Khoronzhuk 
110757d90148SIvan Khoronzhuk 	return !val;
110857d90148SIvan Khoronzhuk }
110957d90148SIvan Khoronzhuk 
111057d90148SIvan Khoronzhuk static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
111157d90148SIvan Khoronzhuk {
111257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
111357d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
111457d90148SIvan Khoronzhuk 	u32 shift, mask, val;
111557d90148SIvan Khoronzhuk 
111657d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
111757d90148SIvan Khoronzhuk 
111857d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
111957d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
112057d90148SIvan Khoronzhuk 	mask = (1 << --fifo) << shift;
112157d90148SIvan Khoronzhuk 	val = on ? val | mask : val & ~mask;
112257d90148SIvan Khoronzhuk 
112357d90148SIvan Khoronzhuk 	writel_relaxed(val, &cpsw->regs->ptype);
112457d90148SIvan Khoronzhuk }
112557d90148SIvan Khoronzhuk 
1126df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
1127df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
1128df828598SMugunthan V N {
1129df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
1130df828598SMugunthan V N 	u32			mac_control = 0;
1131df828598SMugunthan V N 	u32			slave_port;
1132606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1133df828598SMugunthan V N 
1134df828598SMugunthan V N 	if (!phy)
1135df828598SMugunthan V N 		return;
1136df828598SMugunthan V N 
11376f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1138df828598SMugunthan V N 
1139df828598SMugunthan V N 	if (phy->link) {
1140606f3993SIvan Khoronzhuk 		mac_control = cpsw->data.mac_control;
1141df828598SMugunthan V N 
1142df828598SMugunthan V N 		/* enable forwarding */
11432a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1144df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1145df828598SMugunthan V N 
1146df828598SMugunthan V N 		if (phy->speed == 1000)
1147df828598SMugunthan V N 			mac_control |= BIT(7);	/* GIGABITEN	*/
1148df828598SMugunthan V N 		if (phy->duplex)
1149df828598SMugunthan V N 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
1150342b7b74SDaniel Mack 
1151342b7b74SDaniel Mack 		/* set speed_in input in case RMII mode is used in 100Mbps */
1152342b7b74SDaniel Mack 		if (phy->speed == 100)
1153342b7b74SDaniel Mack 			mac_control |= BIT(15);
1154f9db5069SSZ Lin (林上智) 		/* in band mode only works in 10Mbps RGMII mode */
1155f9db5069SSZ Lin (林上智) 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1156a81d8762SMugunthan V N 			mac_control |= BIT(18); /* In Band mode */
1157342b7b74SDaniel Mack 
11581923d6e4SMugunthan V N 		if (priv->rx_pause)
11591923d6e4SMugunthan V N 			mac_control |= BIT(3);
11601923d6e4SMugunthan V N 
11611923d6e4SMugunthan V N 		if (priv->tx_pause)
11621923d6e4SMugunthan V N 			mac_control |= BIT(4);
11631923d6e4SMugunthan V N 
1164df828598SMugunthan V N 		*link = true;
116557d90148SIvan Khoronzhuk 
116657d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
116757d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
116857d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
116957d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
117057d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
1171df828598SMugunthan V N 	} else {
1172df828598SMugunthan V N 		mac_control = 0;
1173df828598SMugunthan V N 		/* disable forwarding */
11742a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1175df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1176df828598SMugunthan V N 	}
1177df828598SMugunthan V N 
1178df828598SMugunthan V N 	if (mac_control != slave->mac_control) {
1179df828598SMugunthan V N 		phy_print_status(phy);
1180dda5f5feSGrygorii Strashko 		writel_relaxed(mac_control, &slave->sliver->mac_control);
1181df828598SMugunthan V N 	}
1182df828598SMugunthan V N 
1183df828598SMugunthan V N 	slave->mac_control = mac_control;
1184df828598SMugunthan V N }
1185df828598SMugunthan V N 
11860be01b8eSIvan Khoronzhuk static int cpsw_get_common_speed(struct cpsw_common *cpsw)
11870be01b8eSIvan Khoronzhuk {
11880be01b8eSIvan Khoronzhuk 	int i, speed;
11890be01b8eSIvan Khoronzhuk 
11900be01b8eSIvan Khoronzhuk 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
11910be01b8eSIvan Khoronzhuk 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
11920be01b8eSIvan Khoronzhuk 			speed += cpsw->slaves[i].phy->speed;
11930be01b8eSIvan Khoronzhuk 
11940be01b8eSIvan Khoronzhuk 	return speed;
11950be01b8eSIvan Khoronzhuk }
11960be01b8eSIvan Khoronzhuk 
11970be01b8eSIvan Khoronzhuk static int cpsw_need_resplit(struct cpsw_common *cpsw)
11980be01b8eSIvan Khoronzhuk {
11990be01b8eSIvan Khoronzhuk 	int i, rlim_ch_num;
12000be01b8eSIvan Khoronzhuk 	int speed, ch_rate;
12010be01b8eSIvan Khoronzhuk 
12020be01b8eSIvan Khoronzhuk 	/* re-split resources only in case speed was changed */
12030be01b8eSIvan Khoronzhuk 	speed = cpsw_get_common_speed(cpsw);
12040be01b8eSIvan Khoronzhuk 	if (speed == cpsw->speed || !speed)
12050be01b8eSIvan Khoronzhuk 		return 0;
12060be01b8eSIvan Khoronzhuk 
12070be01b8eSIvan Khoronzhuk 	cpsw->speed = speed;
12080be01b8eSIvan Khoronzhuk 
12090be01b8eSIvan Khoronzhuk 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
12100be01b8eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
12110be01b8eSIvan Khoronzhuk 		if (!ch_rate)
12120be01b8eSIvan Khoronzhuk 			break;
12130be01b8eSIvan Khoronzhuk 
12140be01b8eSIvan Khoronzhuk 		rlim_ch_num++;
12150be01b8eSIvan Khoronzhuk 	}
12160be01b8eSIvan Khoronzhuk 
12170be01b8eSIvan Khoronzhuk 	/* cases not dependent on speed */
12180be01b8eSIvan Khoronzhuk 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
12190be01b8eSIvan Khoronzhuk 		return 0;
12200be01b8eSIvan Khoronzhuk 
12210be01b8eSIvan Khoronzhuk 	return 1;
12220be01b8eSIvan Khoronzhuk }
12230be01b8eSIvan Khoronzhuk 
1224df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
1225df828598SMugunthan V N {
1226df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
12270be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
1228df828598SMugunthan V N 	bool			link = false;
1229df828598SMugunthan V N 
1230df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1231df828598SMugunthan V N 
1232df828598SMugunthan V N 	if (link) {
12330be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
12340be01b8eSIvan Khoronzhuk 			cpsw_split_res(ndev);
12350be01b8eSIvan Khoronzhuk 
1236df828598SMugunthan V N 		netif_carrier_on(ndev);
1237df828598SMugunthan V N 		if (netif_running(ndev))
1238e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
1239df828598SMugunthan V N 	} else {
1240df828598SMugunthan V N 		netif_carrier_off(ndev);
1241e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
1242df828598SMugunthan V N 	}
1243df828598SMugunthan V N }
1244df828598SMugunthan V N 
1245ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev,
1246ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1247ff5b8ef2SMugunthan V N {
12482a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1249ff5b8ef2SMugunthan V N 
12502a05a622SIvan Khoronzhuk 	coal->rx_coalesce_usecs = cpsw->coal_intvl;
1251ff5b8ef2SMugunthan V N 	return 0;
1252ff5b8ef2SMugunthan V N }
1253ff5b8ef2SMugunthan V N 
1254ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev,
1255ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1256ff5b8ef2SMugunthan V N {
1257ff5b8ef2SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1258ff5b8ef2SMugunthan V N 	u32 int_ctrl;
1259ff5b8ef2SMugunthan V N 	u32 num_interrupts = 0;
1260ff5b8ef2SMugunthan V N 	u32 prescale = 0;
1261ff5b8ef2SMugunthan V N 	u32 addnl_dvdr = 1;
1262ff5b8ef2SMugunthan V N 	u32 coal_intvl = 0;
12635d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1264ff5b8ef2SMugunthan V N 
1265ff5b8ef2SMugunthan V N 	coal_intvl = coal->rx_coalesce_usecs;
1266ff5b8ef2SMugunthan V N 
12675d8d0d4dSIvan Khoronzhuk 	int_ctrl =  readl(&cpsw->wr_regs->int_control);
12682a05a622SIvan Khoronzhuk 	prescale = cpsw->bus_freq_mhz * 4;
1269ff5b8ef2SMugunthan V N 
1270a84bc2a9SMugunthan V N 	if (!coal->rx_coalesce_usecs) {
1271a84bc2a9SMugunthan V N 		int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1272a84bc2a9SMugunthan V N 		goto update_return;
1273a84bc2a9SMugunthan V N 	}
1274a84bc2a9SMugunthan V N 
1275ff5b8ef2SMugunthan V N 	if (coal_intvl < CPSW_CMINTMIN_INTVL)
1276ff5b8ef2SMugunthan V N 		coal_intvl = CPSW_CMINTMIN_INTVL;
1277ff5b8ef2SMugunthan V N 
1278ff5b8ef2SMugunthan V N 	if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1279ff5b8ef2SMugunthan V N 		/* Interrupt pacer works with 4us Pulse, we can
1280ff5b8ef2SMugunthan V N 		 * throttle further by dilating the 4us pulse.
1281ff5b8ef2SMugunthan V N 		 */
1282ff5b8ef2SMugunthan V N 		addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1283ff5b8ef2SMugunthan V N 
1284ff5b8ef2SMugunthan V N 		if (addnl_dvdr > 1) {
1285ff5b8ef2SMugunthan V N 			prescale *= addnl_dvdr;
1286ff5b8ef2SMugunthan V N 			if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1287ff5b8ef2SMugunthan V N 				coal_intvl = (CPSW_CMINTMAX_INTVL
1288ff5b8ef2SMugunthan V N 						* addnl_dvdr);
1289ff5b8ef2SMugunthan V N 		} else {
1290ff5b8ef2SMugunthan V N 			addnl_dvdr = 1;
1291ff5b8ef2SMugunthan V N 			coal_intvl = CPSW_CMINTMAX_INTVL;
1292ff5b8ef2SMugunthan V N 		}
1293ff5b8ef2SMugunthan V N 	}
1294ff5b8ef2SMugunthan V N 
1295ff5b8ef2SMugunthan V N 	num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
12965d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->rx_imax);
12975d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1298ff5b8ef2SMugunthan V N 
1299ff5b8ef2SMugunthan V N 	int_ctrl |= CPSW_INTPACEEN;
1300ff5b8ef2SMugunthan V N 	int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1301ff5b8ef2SMugunthan V N 	int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1302a84bc2a9SMugunthan V N 
1303a84bc2a9SMugunthan V N update_return:
13045d8d0d4dSIvan Khoronzhuk 	writel(int_ctrl, &cpsw->wr_regs->int_control);
1305ff5b8ef2SMugunthan V N 
1306ff5b8ef2SMugunthan V N 	cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
13072a05a622SIvan Khoronzhuk 	cpsw->coal_intvl = coal_intvl;
1308ff5b8ef2SMugunthan V N 
1309ff5b8ef2SMugunthan V N 	return 0;
1310ff5b8ef2SMugunthan V N }
1311ff5b8ef2SMugunthan V N 
1312d9718546SMugunthan V N static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1313d9718546SMugunthan V N {
1314e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1315e05107e6SIvan Khoronzhuk 
1316d9718546SMugunthan V N 	switch (sset) {
1317d9718546SMugunthan V N 	case ETH_SS_STATS:
1318e05107e6SIvan Khoronzhuk 		return (CPSW_STATS_COMMON_LEN +
1319e05107e6SIvan Khoronzhuk 		       (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1320e05107e6SIvan Khoronzhuk 		       CPSW_STATS_CH_LEN);
1321d9718546SMugunthan V N 	default:
1322d9718546SMugunthan V N 		return -EOPNOTSUPP;
1323d9718546SMugunthan V N 	}
1324d9718546SMugunthan V N }
1325d9718546SMugunthan V N 
1326e05107e6SIvan Khoronzhuk static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1327e05107e6SIvan Khoronzhuk {
1328e05107e6SIvan Khoronzhuk 	int ch_stats_len;
1329e05107e6SIvan Khoronzhuk 	int line;
1330e05107e6SIvan Khoronzhuk 	int i;
1331e05107e6SIvan Khoronzhuk 
1332e05107e6SIvan Khoronzhuk 	ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1333e05107e6SIvan Khoronzhuk 	for (i = 0; i < ch_stats_len; i++) {
1334e05107e6SIvan Khoronzhuk 		line = i % CPSW_STATS_CH_LEN;
1335e05107e6SIvan Khoronzhuk 		snprintf(*p, ETH_GSTRING_LEN,
1336bf2ce3fdSFlorian Fainelli 			 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1337bf2ce3fdSFlorian Fainelli 			 (long)(i / CPSW_STATS_CH_LEN),
1338e05107e6SIvan Khoronzhuk 			 cpsw_gstrings_ch_stats[line].stat_string);
1339e05107e6SIvan Khoronzhuk 		*p += ETH_GSTRING_LEN;
1340e05107e6SIvan Khoronzhuk 	}
1341e05107e6SIvan Khoronzhuk }
1342e05107e6SIvan Khoronzhuk 
1343d9718546SMugunthan V N static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1344d9718546SMugunthan V N {
1345e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1346d9718546SMugunthan V N 	u8 *p = data;
1347d9718546SMugunthan V N 	int i;
1348d9718546SMugunthan V N 
1349d9718546SMugunthan V N 	switch (stringset) {
1350d9718546SMugunthan V N 	case ETH_SS_STATS:
1351e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1352d9718546SMugunthan V N 			memcpy(p, cpsw_gstrings_stats[i].stat_string,
1353d9718546SMugunthan V N 			       ETH_GSTRING_LEN);
1354d9718546SMugunthan V N 			p += ETH_GSTRING_LEN;
1355d9718546SMugunthan V N 		}
1356e05107e6SIvan Khoronzhuk 
1357e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1358e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1359d9718546SMugunthan V N 		break;
1360d9718546SMugunthan V N 	}
1361d9718546SMugunthan V N }
1362d9718546SMugunthan V N 
1363d9718546SMugunthan V N static void cpsw_get_ethtool_stats(struct net_device *ndev,
1364d9718546SMugunthan V N 				    struct ethtool_stats *stats, u64 *data)
1365d9718546SMugunthan V N {
1366d9718546SMugunthan V N 	u8 *p;
13672c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1368e05107e6SIvan Khoronzhuk 	struct cpdma_chan_stats ch_stats;
1369e05107e6SIvan Khoronzhuk 	int i, l, ch;
1370d9718546SMugunthan V N 
1371d9718546SMugunthan V N 	/* Collect Davinci CPDMA stats for Rx and Tx Channel */
1372e05107e6SIvan Khoronzhuk 	for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1373e05107e6SIvan Khoronzhuk 		data[l] = readl(cpsw->hw_stats +
1374e05107e6SIvan Khoronzhuk 				cpsw_gstrings_stats[l].stat_offset);
1375d9718546SMugunthan V N 
1376e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
13778feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1378e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1379e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1380e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1381e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1382e05107e6SIvan Khoronzhuk 		}
1383e05107e6SIvan Khoronzhuk 	}
1384d9718546SMugunthan V N 
1385e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
13868feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1387e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1388e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1389e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1390e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1391d9718546SMugunthan V N 		}
1392d9718546SMugunthan V N 	}
1393d9718546SMugunthan V N }
1394d9718546SMugunthan V N 
139527e9e103SIvan Khoronzhuk static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1396e05107e6SIvan Khoronzhuk 					struct sk_buff *skb,
1397e05107e6SIvan Khoronzhuk 					struct cpdma_chan *txch)
1398d9ba8f9eSMugunthan V N {
13992c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
14002c836bd9SIvan Khoronzhuk 
140198fdd857SIvan Khoronzhuk 	skb_tx_timestamp(skb);
1402e05107e6SIvan Khoronzhuk 	return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1403606f3993SIvan Khoronzhuk 				 priv->emac_port + cpsw->data.dual_emac);
1404d9ba8f9eSMugunthan V N }
1405d9ba8f9eSMugunthan V N 
1406d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
1407d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
1408d9ba8f9eSMugunthan V N 		u32 slave_port)
1409d9ba8f9eSMugunthan V N {
14102a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
141171a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1412d9ba8f9eSMugunthan V N 
14132a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
1414d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1415d9ba8f9eSMugunthan V N 	else
1416d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
14172a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1418d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
14192a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
14205b3a5a14SIvan Khoronzhuk 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
14212a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
14222a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
14232a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
14245e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
14255e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1426d9ba8f9eSMugunthan V N }
1427d9ba8f9eSMugunthan V N 
14281e7a2e21SDaniel Mack static void soft_reset_slave(struct cpsw_slave *slave)
1429df828598SMugunthan V N {
1430df828598SMugunthan V N 	char name[32];
14311e7a2e21SDaniel Mack 
14321e7a2e21SDaniel Mack 	snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
14331e7a2e21SDaniel Mack 	soft_reset(name, &slave->sliver->soft_reset);
14341e7a2e21SDaniel Mack }
14351e7a2e21SDaniel Mack 
14361e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
14371e7a2e21SDaniel Mack {
1438df828598SMugunthan V N 	u32 slave_port;
143930c57f07SSekhar Nori 	struct phy_device *phy;
1440649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1441df828598SMugunthan V N 
14421e7a2e21SDaniel Mack 	soft_reset_slave(slave);
1443df828598SMugunthan V N 
1444df828598SMugunthan V N 	/* setup priority mapping */
1445dda5f5feSGrygorii Strashko 	writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
14469750a3adSRichard Cochran 
14472a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
14489750a3adSRichard Cochran 	case CPSW_VERSION_1:
14499750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
145048f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
145148f5bcccSGrygorii Strashko 		 * flow control mode
145248f5bcccSGrygorii Strashko 		 */
145348f5bcccSGrygorii Strashko 		slave_write(slave,
145448f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
145548f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
14569750a3adSRichard Cochran 		break;
14579750a3adSRichard Cochran 	case CPSW_VERSION_2:
1458c193f365SMugunthan V N 	case CPSW_VERSION_3:
1459926489beSMugunthan V N 	case CPSW_VERSION_4:
14609750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
146148f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
146248f5bcccSGrygorii Strashko 		 * flow control mode
146348f5bcccSGrygorii Strashko 		 */
146448f5bcccSGrygorii Strashko 		slave_write(slave,
146548f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
146648f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
14679750a3adSRichard Cochran 		break;
14689750a3adSRichard Cochran 	}
1469df828598SMugunthan V N 
1470df828598SMugunthan V N 	/* setup max packet size, and mac address */
1471dda5f5feSGrygorii Strashko 	writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1472df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
1473df828598SMugunthan V N 
1474df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
1475df828598SMugunthan V N 
14766f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1477df828598SMugunthan V N 
1478606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
1479d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1480d9ba8f9eSMugunthan V N 	else
14812a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1482e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1483df828598SMugunthan V N 
1484d733f754SDavid Rivshin 	if (slave->data->phy_node) {
148530c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
14869e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
148730c57f07SSekhar Nori 		if (!phy) {
1488f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1489f7ce9103SRob Herring 				slave->data->phy_node,
1490d733f754SDavid Rivshin 				slave->slave_num);
1491d733f754SDavid Rivshin 			return;
1492d733f754SDavid Rivshin 		}
1493d733f754SDavid Rivshin 	} else {
149430c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
1495f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
149630c57f07SSekhar Nori 		if (IS_ERR(phy)) {
1497d733f754SDavid Rivshin 			dev_err(priv->dev,
1498d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
1499d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
150030c57f07SSekhar Nori 				PTR_ERR(phy));
1501d733f754SDavid Rivshin 			return;
1502d733f754SDavid Rivshin 		}
1503d733f754SDavid Rivshin 	}
1504d733f754SDavid Rivshin 
150530c57f07SSekhar Nori 	slave->phy = phy;
150630c57f07SSekhar Nori 
15072220943aSAndrew Lunn 	phy_attached_info(slave->phy);
15082220943aSAndrew Lunn 
1509df828598SMugunthan V N 	phy_start(slave->phy);
1510388367a5SMugunthan V N 
1511388367a5SMugunthan V N 	/* Configure GMII_SEL register */
151256e31bd8SIvan Khoronzhuk 	cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1513df828598SMugunthan V N }
1514df828598SMugunthan V N 
15153b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
15163b72c2feSMugunthan V N {
1517606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1518606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
15193b72c2feSMugunthan V N 	u32 reg;
15203b72c2feSMugunthan V N 	int i;
15211e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
15223b72c2feSMugunthan V N 
15232a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
15243b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
15253b72c2feSMugunthan V N 
15265d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
15273b72c2feSMugunthan V N 
1528606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1529606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
15303b72c2feSMugunthan V N 
15311e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
15321e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
15331e5c4bc4SLennart Sorensen 	else
15341e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
15351e5c4bc4SLennart Sorensen 
15362a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
153761f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
153861f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
15393b72c2feSMugunthan V N }
15403b72c2feSMugunthan V N 
1541df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
1542df828598SMugunthan V N {
1543d9ba8f9eSMugunthan V N 	u32 fifo_mode;
15445d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
15455d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15463b72c2feSMugunthan V N 
1547df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
15485d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
15492a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
1550df828598SMugunthan V N 
1551df828598SMugunthan V N 	/* switch to vlan unaware mode */
15522a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
15533b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
15545d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
1555a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
15565d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
1557606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1558d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
15595d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1560df828598SMugunthan V N 
1561df828598SMugunthan V N 	/* setup host port priority mapping */
1562dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
15635d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
1564dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1565df828598SMugunthan V N 
15662a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1567df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1568df828598SMugunthan V N 
1569606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
15702a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1571d9ba8f9eSMugunthan V N 				   0, 0);
15722a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
157371a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1574df828598SMugunthan V N 	}
1575d9ba8f9eSMugunthan V N }
1576df828598SMugunthan V N 
15773802dce1SIvan Khoronzhuk static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
15783802dce1SIvan Khoronzhuk {
15793802dce1SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15803802dce1SIvan Khoronzhuk 	struct sk_buff *skb;
15813802dce1SIvan Khoronzhuk 	int ch_buf_num;
1582e05107e6SIvan Khoronzhuk 	int ch, i, ret;
15833802dce1SIvan Khoronzhuk 
1584e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
15858feb0a19SIvan Khoronzhuk 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
15863802dce1SIvan Khoronzhuk 		for (i = 0; i < ch_buf_num; i++) {
15873802dce1SIvan Khoronzhuk 			skb = __netdev_alloc_skb_ip_align(priv->ndev,
15883802dce1SIvan Khoronzhuk 							  cpsw->rx_packet_max,
15893802dce1SIvan Khoronzhuk 							  GFP_KERNEL);
15903802dce1SIvan Khoronzhuk 			if (!skb) {
15913802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup, "cannot allocate skb\n");
15923802dce1SIvan Khoronzhuk 				return -ENOMEM;
15933802dce1SIvan Khoronzhuk 			}
15943802dce1SIvan Khoronzhuk 
1595e05107e6SIvan Khoronzhuk 			skb_set_queue_mapping(skb, ch);
15968feb0a19SIvan Khoronzhuk 			ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
15978feb0a19SIvan Khoronzhuk 						skb->data, skb_tailroom(skb),
15988feb0a19SIvan Khoronzhuk 						0);
15993802dce1SIvan Khoronzhuk 			if (ret < 0) {
16003802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup,
1601e05107e6SIvan Khoronzhuk 					 "cannot submit skb to channel %d rx, error %d\n",
1602e05107e6SIvan Khoronzhuk 					 ch, ret);
16033802dce1SIvan Khoronzhuk 				kfree_skb(skb);
16043802dce1SIvan Khoronzhuk 				return ret;
16053802dce1SIvan Khoronzhuk 			}
16063802dce1SIvan Khoronzhuk 			kmemleak_not_leak(skb);
16073802dce1SIvan Khoronzhuk 		}
16083802dce1SIvan Khoronzhuk 
1609e05107e6SIvan Khoronzhuk 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1610e05107e6SIvan Khoronzhuk 			  ch, ch_buf_num);
1611e05107e6SIvan Khoronzhuk 	}
16123802dce1SIvan Khoronzhuk 
1613e05107e6SIvan Khoronzhuk 	return 0;
16143802dce1SIvan Khoronzhuk }
16153802dce1SIvan Khoronzhuk 
16162a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1617aacebbf8SSebastian Siewior {
16183995d265SSchuyler Patton 	u32 slave_port;
16193995d265SSchuyler Patton 
16206f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
16213995d265SSchuyler Patton 
1622aacebbf8SSebastian Siewior 	if (!slave->phy)
1623aacebbf8SSebastian Siewior 		return;
1624aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
1625aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
1626aacebbf8SSebastian Siewior 	slave->phy = NULL;
16272a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
16283995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
16291f95ba00SGrygorii Strashko 	soft_reset_slave(slave);
1630aacebbf8SSebastian Siewior }
1631aacebbf8SSebastian Siewior 
16327929a668SIvan Khoronzhuk static int cpsw_tc_to_fifo(int tc, int num_tc)
16337929a668SIvan Khoronzhuk {
16347929a668SIvan Khoronzhuk 	if (tc == num_tc - 1)
16357929a668SIvan Khoronzhuk 		return 0;
16367929a668SIvan Khoronzhuk 
16377929a668SIvan Khoronzhuk 	return CPSW_FIFO_SHAPERS_NUM - tc;
16387929a668SIvan Khoronzhuk }
16397929a668SIvan Khoronzhuk 
164057d90148SIvan Khoronzhuk static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
164157d90148SIvan Khoronzhuk {
164257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
164357d90148SIvan Khoronzhuk 	u32 val = 0, send_pct, shift;
164457d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
164557d90148SIvan Khoronzhuk 	int pct = 0, i;
164657d90148SIvan Khoronzhuk 
164757d90148SIvan Khoronzhuk 	if (bw > priv->shp_cfg_speed * 1000)
164857d90148SIvan Khoronzhuk 		goto err;
164957d90148SIvan Khoronzhuk 
165057d90148SIvan Khoronzhuk 	/* shaping has to stay enabled for highest fifos linearly
165157d90148SIvan Khoronzhuk 	 * and fifo bw no more then interface can allow
165257d90148SIvan Khoronzhuk 	 */
165357d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
165457d90148SIvan Khoronzhuk 	send_pct = slave_read(slave, SEND_PERCENT);
165557d90148SIvan Khoronzhuk 	for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
165657d90148SIvan Khoronzhuk 		if (!bw) {
165757d90148SIvan Khoronzhuk 			if (i >= fifo || !priv->fifo_bw[i])
165857d90148SIvan Khoronzhuk 				continue;
165957d90148SIvan Khoronzhuk 
166057d90148SIvan Khoronzhuk 			dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
166157d90148SIvan Khoronzhuk 			continue;
166257d90148SIvan Khoronzhuk 		}
166357d90148SIvan Khoronzhuk 
166457d90148SIvan Khoronzhuk 		if (!priv->fifo_bw[i] && i > fifo) {
166557d90148SIvan Khoronzhuk 			dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
166657d90148SIvan Khoronzhuk 			return -EINVAL;
166757d90148SIvan Khoronzhuk 		}
166857d90148SIvan Khoronzhuk 
166957d90148SIvan Khoronzhuk 		shift = (i - 1) * 8;
167057d90148SIvan Khoronzhuk 		if (i == fifo) {
167157d90148SIvan Khoronzhuk 			send_pct &= ~(CPSW_PCT_MASK << shift);
167257d90148SIvan Khoronzhuk 			val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
167357d90148SIvan Khoronzhuk 			if (!val)
167457d90148SIvan Khoronzhuk 				val = 1;
167557d90148SIvan Khoronzhuk 
167657d90148SIvan Khoronzhuk 			send_pct |= val << shift;
167757d90148SIvan Khoronzhuk 			pct += val;
167857d90148SIvan Khoronzhuk 			continue;
167957d90148SIvan Khoronzhuk 		}
168057d90148SIvan Khoronzhuk 
168157d90148SIvan Khoronzhuk 		if (priv->fifo_bw[i])
168257d90148SIvan Khoronzhuk 			pct += (send_pct >> shift) & CPSW_PCT_MASK;
168357d90148SIvan Khoronzhuk 	}
168457d90148SIvan Khoronzhuk 
168557d90148SIvan Khoronzhuk 	if (pct >= 100)
168657d90148SIvan Khoronzhuk 		goto err;
168757d90148SIvan Khoronzhuk 
168857d90148SIvan Khoronzhuk 	slave_write(slave, send_pct, SEND_PERCENT);
168957d90148SIvan Khoronzhuk 	priv->fifo_bw[fifo] = bw;
169057d90148SIvan Khoronzhuk 
169157d90148SIvan Khoronzhuk 	dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
169257d90148SIvan Khoronzhuk 		 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
169357d90148SIvan Khoronzhuk 
169457d90148SIvan Khoronzhuk 	return 0;
169557d90148SIvan Khoronzhuk err:
169657d90148SIvan Khoronzhuk 	dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
169757d90148SIvan Khoronzhuk 	return -EINVAL;
169857d90148SIvan Khoronzhuk }
169957d90148SIvan Khoronzhuk 
170057d90148SIvan Khoronzhuk static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
170157d90148SIvan Khoronzhuk {
170257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
170357d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
170457d90148SIvan Khoronzhuk 	u32 tx_in_ctl_rg, val;
170557d90148SIvan Khoronzhuk 	int ret;
170657d90148SIvan Khoronzhuk 
170757d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_bw(priv, fifo, bw);
170857d90148SIvan Khoronzhuk 	if (ret)
170957d90148SIvan Khoronzhuk 		return ret;
171057d90148SIvan Khoronzhuk 
171157d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
171257d90148SIvan Khoronzhuk 	tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
171357d90148SIvan Khoronzhuk 		       CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
171457d90148SIvan Khoronzhuk 
171557d90148SIvan Khoronzhuk 	if (!bw)
171657d90148SIvan Khoronzhuk 		cpsw_fifo_shp_on(priv, fifo, bw);
171757d90148SIvan Khoronzhuk 
171857d90148SIvan Khoronzhuk 	val = slave_read(slave, tx_in_ctl_rg);
171957d90148SIvan Khoronzhuk 	if (cpsw_shp_is_off(priv)) {
172057d90148SIvan Khoronzhuk 		/* disable FIFOs rate limited queues */
172157d90148SIvan Khoronzhuk 		val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
172257d90148SIvan Khoronzhuk 
172357d90148SIvan Khoronzhuk 		/* set type of FIFO queues to normal priority mode */
172457d90148SIvan Khoronzhuk 		val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
172557d90148SIvan Khoronzhuk 
172657d90148SIvan Khoronzhuk 		/* set type of FIFO queues to be rate limited */
172757d90148SIvan Khoronzhuk 		if (bw)
172857d90148SIvan Khoronzhuk 			val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
172957d90148SIvan Khoronzhuk 		else
173057d90148SIvan Khoronzhuk 			priv->shp_cfg_speed = 0;
173157d90148SIvan Khoronzhuk 	}
173257d90148SIvan Khoronzhuk 
173357d90148SIvan Khoronzhuk 	/* toggle a FIFO rate limited queue */
173457d90148SIvan Khoronzhuk 	if (bw)
173557d90148SIvan Khoronzhuk 		val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
173657d90148SIvan Khoronzhuk 	else
173757d90148SIvan Khoronzhuk 		val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
173857d90148SIvan Khoronzhuk 	slave_write(slave, val, tx_in_ctl_rg);
173957d90148SIvan Khoronzhuk 
174057d90148SIvan Khoronzhuk 	/* FIFO transmit shape enable */
174157d90148SIvan Khoronzhuk 	cpsw_fifo_shp_on(priv, fifo, bw);
174257d90148SIvan Khoronzhuk 	return 0;
174357d90148SIvan Khoronzhuk }
174457d90148SIvan Khoronzhuk 
174557d90148SIvan Khoronzhuk /* Defaults:
174657d90148SIvan Khoronzhuk  * class A - prio 3
174757d90148SIvan Khoronzhuk  * class B - prio 2
174857d90148SIvan Khoronzhuk  * shaping for class A should be set first
174957d90148SIvan Khoronzhuk  */
175057d90148SIvan Khoronzhuk static int cpsw_set_cbs(struct net_device *ndev,
175157d90148SIvan Khoronzhuk 			struct tc_cbs_qopt_offload *qopt)
175257d90148SIvan Khoronzhuk {
175357d90148SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
175457d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
175557d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
175657d90148SIvan Khoronzhuk 	int prev_speed = 0;
175757d90148SIvan Khoronzhuk 	int tc, ret, fifo;
175857d90148SIvan Khoronzhuk 	u32 bw = 0;
175957d90148SIvan Khoronzhuk 
176057d90148SIvan Khoronzhuk 	tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
176157d90148SIvan Khoronzhuk 
176257d90148SIvan Khoronzhuk 	/* enable channels in backward order, as highest FIFOs must be rate
176357d90148SIvan Khoronzhuk 	 * limited first and for compliance with CPDMA rate limited channels
176457d90148SIvan Khoronzhuk 	 * that also used in bacward order. FIFO0 cannot be rate limited.
176557d90148SIvan Khoronzhuk 	 */
176657d90148SIvan Khoronzhuk 	fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
176757d90148SIvan Khoronzhuk 	if (!fifo) {
176857d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
176957d90148SIvan Khoronzhuk 		return -EINVAL;
177057d90148SIvan Khoronzhuk 	}
177157d90148SIvan Khoronzhuk 
177257d90148SIvan Khoronzhuk 	/* do nothing, it's disabled anyway */
177357d90148SIvan Khoronzhuk 	if (!qopt->enable && !priv->fifo_bw[fifo])
177457d90148SIvan Khoronzhuk 		return 0;
177557d90148SIvan Khoronzhuk 
177657d90148SIvan Khoronzhuk 	/* shapers can be set if link speed is known */
177757d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
177857d90148SIvan Khoronzhuk 	if (slave->phy && slave->phy->link) {
177957d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
178057d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed)
178157d90148SIvan Khoronzhuk 			prev_speed = priv->shp_cfg_speed;
178257d90148SIvan Khoronzhuk 
178357d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = slave->phy->speed;
178457d90148SIvan Khoronzhuk 	}
178557d90148SIvan Khoronzhuk 
178657d90148SIvan Khoronzhuk 	if (!priv->shp_cfg_speed) {
178757d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Link speed is not known");
178857d90148SIvan Khoronzhuk 		return -1;
178957d90148SIvan Khoronzhuk 	}
179057d90148SIvan Khoronzhuk 
179157d90148SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
179257d90148SIvan Khoronzhuk 	if (ret < 0) {
179357d90148SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
179457d90148SIvan Khoronzhuk 		return ret;
179557d90148SIvan Khoronzhuk 	}
179657d90148SIvan Khoronzhuk 
179757d90148SIvan Khoronzhuk 	bw = qopt->enable ? qopt->idleslope : 0;
179857d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
179957d90148SIvan Khoronzhuk 	if (ret) {
180057d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = prev_speed;
180157d90148SIvan Khoronzhuk 		prev_speed = 0;
180257d90148SIvan Khoronzhuk 	}
180357d90148SIvan Khoronzhuk 
180457d90148SIvan Khoronzhuk 	if (bw && prev_speed)
180557d90148SIvan Khoronzhuk 		dev_warn(priv->dev,
180657d90148SIvan Khoronzhuk 			 "Speed was changed, CBS shaper speeds are changed!");
180757d90148SIvan Khoronzhuk 
180857d90148SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
180957d90148SIvan Khoronzhuk 	return ret;
181057d90148SIvan Khoronzhuk }
181157d90148SIvan Khoronzhuk 
18124b4255edSIvan Khoronzhuk static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
18134b4255edSIvan Khoronzhuk {
18144b4255edSIvan Khoronzhuk 	int fifo, bw;
18154b4255edSIvan Khoronzhuk 
18164b4255edSIvan Khoronzhuk 	for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
18174b4255edSIvan Khoronzhuk 		bw = priv->fifo_bw[fifo];
18184b4255edSIvan Khoronzhuk 		if (!bw)
18194b4255edSIvan Khoronzhuk 			continue;
18204b4255edSIvan Khoronzhuk 
18214b4255edSIvan Khoronzhuk 		cpsw_set_fifo_rlimit(priv, fifo, bw);
18224b4255edSIvan Khoronzhuk 	}
18234b4255edSIvan Khoronzhuk }
18244b4255edSIvan Khoronzhuk 
18254b4255edSIvan Khoronzhuk static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
18264b4255edSIvan Khoronzhuk {
18274b4255edSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18284b4255edSIvan Khoronzhuk 	u32 tx_prio_map = 0;
18294b4255edSIvan Khoronzhuk 	int i, tc, fifo;
18304b4255edSIvan Khoronzhuk 	u32 tx_prio_rg;
18314b4255edSIvan Khoronzhuk 
18324b4255edSIvan Khoronzhuk 	if (!priv->mqprio_hw)
18334b4255edSIvan Khoronzhuk 		return;
18344b4255edSIvan Khoronzhuk 
18354b4255edSIvan Khoronzhuk 	for (i = 0; i < 8; i++) {
18364b4255edSIvan Khoronzhuk 		tc = netdev_get_prio_tc_map(priv->ndev, i);
18374b4255edSIvan Khoronzhuk 		fifo = CPSW_FIFO_SHAPERS_NUM - tc;
18384b4255edSIvan Khoronzhuk 		tx_prio_map |= fifo << (4 * i);
18394b4255edSIvan Khoronzhuk 	}
18404b4255edSIvan Khoronzhuk 
18414b4255edSIvan Khoronzhuk 	tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
18424b4255edSIvan Khoronzhuk 		     CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
18434b4255edSIvan Khoronzhuk 
18444b4255edSIvan Khoronzhuk 	slave_write(slave, tx_prio_map, tx_prio_rg);
18454b4255edSIvan Khoronzhuk }
18464b4255edSIvan Khoronzhuk 
18474b4255edSIvan Khoronzhuk /* restore resources after port reset */
18484b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
18494b4255edSIvan Khoronzhuk {
18504b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
18514b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
18524b4255edSIvan Khoronzhuk 
18534b4255edSIvan Khoronzhuk 	/* restore CBS offload */
18544b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
18554b4255edSIvan Khoronzhuk }
18564b4255edSIvan Khoronzhuk 
1857df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
1858df828598SMugunthan V N {
1859df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1860649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18613802dce1SIvan Khoronzhuk 	int ret;
1862df828598SMugunthan V N 	u32 reg;
1863df828598SMugunthan V N 
186456e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1865108a6537SGrygorii Strashko 	if (ret < 0) {
186656e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1867108a6537SGrygorii Strashko 		return ret;
1868108a6537SGrygorii Strashko 	}
18693fa88c51SGrygorii Strashko 
1870df828598SMugunthan V N 	netif_carrier_off(ndev);
1871df828598SMugunthan V N 
1872e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
1873e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1874e05107e6SIvan Khoronzhuk 	if (ret) {
1875e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
1876e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1877e05107e6SIvan Khoronzhuk 	}
1878e05107e6SIvan Khoronzhuk 
1879e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1880e05107e6SIvan Khoronzhuk 	if (ret) {
1881e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
1882e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1883e05107e6SIvan Khoronzhuk 	}
1884e05107e6SIvan Khoronzhuk 
18852a05a622SIvan Khoronzhuk 	reg = cpsw->version;
1886df828598SMugunthan V N 
1887df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1888df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1889df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
1890df828598SMugunthan V N 
1891d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
1892d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
1893df828598SMugunthan V N 		cpsw_init_host_port(priv);
1894df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
1895df828598SMugunthan V N 
18963b72c2feSMugunthan V N 	/* Add default VLAN */
1897606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
18983b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
1899e6afea0bSMugunthan V N 	else
19002a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
190161f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
19023b72c2feSMugunthan V N 
1903d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
1904d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
1905d9ba8f9eSMugunthan V N 		/* disable priority elevation */
1906dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
1907df828598SMugunthan V N 
1908d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
1909dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1910df828598SMugunthan V N 
19111923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
19125d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
19131923d6e4SMugunthan V N 
1914dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
1915dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
1916d354eb85SMugunthan V N 
1917e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1918e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1919e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
19207da11600SMugunthan V N 		}
19217da11600SMugunthan V N 
1922e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1923e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1924e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
19257da11600SMugunthan V N 		}
19267da11600SMugunthan V N 
19273802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
19283802dce1SIvan Khoronzhuk 		if (ret < 0)
1929aacebbf8SSebastian Siewior 			goto err_cleanup;
1930f280e89aSMugunthan V N 
19318a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
1932f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
1933f280e89aSMugunthan V N 
1934d9ba8f9eSMugunthan V N 	}
1935df828598SMugunthan V N 
19364b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
19374b4255edSIvan Khoronzhuk 
1938ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
19392a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
1940ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
1941ff5b8ef2SMugunthan V N 
19422a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
1943ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
1944ff5b8ef2SMugunthan V N 	}
1945ff5b8ef2SMugunthan V N 
19462c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
19472c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
1948d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
1949f63a975eSMugunthan V N 
1950df828598SMugunthan V N 	return 0;
1951df828598SMugunthan V N 
1952aacebbf8SSebastian Siewior err_cleanup:
19532c836bd9SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
19542a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
195556e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1956aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
1957aacebbf8SSebastian Siewior 	return ret;
1958df828598SMugunthan V N }
1959df828598SMugunthan V N 
1960df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
1961df828598SMugunthan V N {
1962df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1963649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1964df828598SMugunthan V N 
1965df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
19665da19489SIvan Khoronzhuk 	__dev_mc_unsync(priv->ndev, cpsw_del_mc_addr);
1967e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
1968df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
1969d9ba8f9eSMugunthan V N 
1970d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
1971dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
1972dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
19732a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
19742c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
19752c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
19762a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
1977d9ba8f9eSMugunthan V N 	}
19782a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
19790be01b8eSIvan Khoronzhuk 
19800be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
19810be01b8eSIvan Khoronzhuk 		cpsw_split_res(ndev);
19820be01b8eSIvan Khoronzhuk 
1983d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
198456e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1985df828598SMugunthan V N 	return 0;
1986df828598SMugunthan V N }
1987df828598SMugunthan V N 
1988df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1989df828598SMugunthan V N 				       struct net_device *ndev)
1990df828598SMugunthan V N {
1991df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
19922c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1993f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
1994e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
1995e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
1996e05107e6SIvan Khoronzhuk 	int ret, q_idx;
1997df828598SMugunthan V N 
1998df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1999df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
20008dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
20011bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
2002df828598SMugunthan V N 	}
2003df828598SMugunthan V N 
20049232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2005f44f8417SIvan Khoronzhuk 	    cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
20062e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
20072e5b38abSRichard Cochran 
2008e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
2009e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
2010e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
2011e05107e6SIvan Khoronzhuk 
20128feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
201362f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
2014e05107e6SIvan Khoronzhuk 	ret = cpsw_tx_packet_submit(priv, skb, txch);
2015df828598SMugunthan V N 	if (unlikely(ret != 0)) {
2016df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
2017df828598SMugunthan V N 		goto fail;
2018df828598SMugunthan V N 	}
2019df828598SMugunthan V N 
2020fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
2021fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
2022fae50823SMugunthan V N 	 */
2023e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
2024e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
202562f94c21SGrygorii Strashko 
202662f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
202762f94c21SGrygorii Strashko 		smp_mb__after_atomic();
202862f94c21SGrygorii Strashko 
202962f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
203062f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
2031e05107e6SIvan Khoronzhuk 	}
2032fae50823SMugunthan V N 
2033df828598SMugunthan V N 	return NETDEV_TX_OK;
2034df828598SMugunthan V N fail:
20358dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
2036e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
203762f94c21SGrygorii Strashko 
203862f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
203962f94c21SGrygorii Strashko 	smp_mb__after_atomic();
204062f94c21SGrygorii Strashko 
204162f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
204262f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
204362f94c21SGrygorii Strashko 
2044df828598SMugunthan V N 	return NETDEV_TX_BUSY;
2045df828598SMugunthan V N }
2046df828598SMugunthan V N 
2047c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
20482e5b38abSRichard Cochran 
20492a05a622SIvan Khoronzhuk static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
20502e5b38abSRichard Cochran {
2051606f3993SIvan Khoronzhuk 	struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
20522e5b38abSRichard Cochran 	u32 ts_en, seq_id;
20532e5b38abSRichard Cochran 
2054b63ba58eSGrygorii Strashko 	if (!cpts_is_tx_enabled(cpsw->cpts) &&
2055b63ba58eSGrygorii Strashko 	    !cpts_is_rx_enabled(cpsw->cpts)) {
20562e5b38abSRichard Cochran 		slave_write(slave, 0, CPSW1_TS_CTL);
20572e5b38abSRichard Cochran 		return;
20582e5b38abSRichard Cochran 	}
20592e5b38abSRichard Cochran 
20602e5b38abSRichard Cochran 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
20612e5b38abSRichard Cochran 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
20622e5b38abSRichard Cochran 
2063b63ba58eSGrygorii Strashko 	if (cpts_is_tx_enabled(cpsw->cpts))
20642e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_TX_EN;
20652e5b38abSRichard Cochran 
2066b63ba58eSGrygorii Strashko 	if (cpts_is_rx_enabled(cpsw->cpts))
20672e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_RX_EN;
20682e5b38abSRichard Cochran 
20692e5b38abSRichard Cochran 	slave_write(slave, ts_en, CPSW1_TS_CTL);
20702e5b38abSRichard Cochran 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
20712e5b38abSRichard Cochran }
20722e5b38abSRichard Cochran 
20732e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
20742e5b38abSRichard Cochran {
2075d9ba8f9eSMugunthan V N 	struct cpsw_slave *slave;
20765d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
20772e5b38abSRichard Cochran 	u32 ctrl, mtype;
20782e5b38abSRichard Cochran 
2079cb7d78d0SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2080d9ba8f9eSMugunthan V N 
20812e5b38abSRichard Cochran 	ctrl = slave_read(slave, CPSW2_CONTROL);
20822a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
208309c55372SGeorge Cherian 	case CPSW_VERSION_2:
208409c55372SGeorge Cherian 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
20852e5b38abSRichard Cochran 
2086b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
208709c55372SGeorge Cherian 			ctrl |= CTRL_V2_TX_TS_BITS;
20882e5b38abSRichard Cochran 
2089b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
209009c55372SGeorge Cherian 			ctrl |= CTRL_V2_RX_TS_BITS;
209109c55372SGeorge Cherian 		break;
209209c55372SGeorge Cherian 	case CPSW_VERSION_3:
209309c55372SGeorge Cherian 	default:
209409c55372SGeorge Cherian 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
209509c55372SGeorge Cherian 
2096b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
209709c55372SGeorge Cherian 			ctrl |= CTRL_V3_TX_TS_BITS;
209809c55372SGeorge Cherian 
2099b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
210009c55372SGeorge Cherian 			ctrl |= CTRL_V3_RX_TS_BITS;
210109c55372SGeorge Cherian 		break;
210209c55372SGeorge Cherian 	}
21032e5b38abSRichard Cochran 
21042e5b38abSRichard Cochran 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
21052e5b38abSRichard Cochran 
21062e5b38abSRichard Cochran 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
21072e5b38abSRichard Cochran 	slave_write(slave, ctrl, CPSW2_CONTROL);
2108dda5f5feSGrygorii Strashko 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
21092e5b38abSRichard Cochran }
21102e5b38abSRichard Cochran 
2111a5b4145bSBen Hutchings static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
21122e5b38abSRichard Cochran {
21133177bf6fSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
21142e5b38abSRichard Cochran 	struct hwtstamp_config cfg;
21152a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
21162a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
21172e5b38abSRichard Cochran 
21182a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
21192a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
21202a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
21212ee91e54SBen Hutchings 		return -EOPNOTSUPP;
21222ee91e54SBen Hutchings 
21232e5b38abSRichard Cochran 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
21242e5b38abSRichard Cochran 		return -EFAULT;
21252e5b38abSRichard Cochran 
21262e5b38abSRichard Cochran 	/* reserved for future extensions */
21272e5b38abSRichard Cochran 	if (cfg.flags)
21282e5b38abSRichard Cochran 		return -EINVAL;
21292e5b38abSRichard Cochran 
21302ee91e54SBen Hutchings 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
21312e5b38abSRichard Cochran 		return -ERANGE;
21322e5b38abSRichard Cochran 
21332e5b38abSRichard Cochran 	switch (cfg.rx_filter) {
21342e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_NONE:
2135b63ba58eSGrygorii Strashko 		cpts_rx_enable(cpts, 0);
21362e5b38abSRichard Cochran 		break;
21372e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_ALL:
2138e9523a5aSGrygorii Strashko 	case HWTSTAMP_FILTER_NTP_ALL:
2139e9523a5aSGrygorii Strashko 		return -ERANGE;
21402e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
21412e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
21422e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2143e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
2144e9523a5aSGrygorii Strashko 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
2145e9523a5aSGrygorii Strashko 		break;
21462e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
21472e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
21482e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
21492e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
21502e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
21512e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
21522e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
21532e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
21542e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2155e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
21562e5b38abSRichard Cochran 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
21572e5b38abSRichard Cochran 		break;
21582e5b38abSRichard Cochran 	default:
21592e5b38abSRichard Cochran 		return -ERANGE;
21602e5b38abSRichard Cochran 	}
21612e5b38abSRichard Cochran 
2162b63ba58eSGrygorii Strashko 	cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
21632ee91e54SBen Hutchings 
21642a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
21652e5b38abSRichard Cochran 	case CPSW_VERSION_1:
21662a05a622SIvan Khoronzhuk 		cpsw_hwtstamp_v1(cpsw);
21672e5b38abSRichard Cochran 		break;
21682e5b38abSRichard Cochran 	case CPSW_VERSION_2:
2169f7d403cbSGeorge Cherian 	case CPSW_VERSION_3:
21702e5b38abSRichard Cochran 		cpsw_hwtstamp_v2(priv);
21712e5b38abSRichard Cochran 		break;
21722e5b38abSRichard Cochran 	default:
21732ee91e54SBen Hutchings 		WARN_ON(1);
21742e5b38abSRichard Cochran 	}
21752e5b38abSRichard Cochran 
21762e5b38abSRichard Cochran 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
21772e5b38abSRichard Cochran }
21782e5b38abSRichard Cochran 
2179a5b4145bSBen Hutchings static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2180a5b4145bSBen Hutchings {
21812a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
21822a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
2183a5b4145bSBen Hutchings 	struct hwtstamp_config cfg;
2184a5b4145bSBen Hutchings 
21852a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
21862a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
21872a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
2188a5b4145bSBen Hutchings 		return -EOPNOTSUPP;
2189a5b4145bSBen Hutchings 
2190a5b4145bSBen Hutchings 	cfg.flags = 0;
2191b63ba58eSGrygorii Strashko 	cfg.tx_type = cpts_is_tx_enabled(cpts) ?
2192b63ba58eSGrygorii Strashko 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2193b63ba58eSGrygorii Strashko 	cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
2194e9523a5aSGrygorii Strashko 			 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
2195a5b4145bSBen Hutchings 
2196a5b4145bSBen Hutchings 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2197a5b4145bSBen Hutchings }
2198c8395d4eSGrygorii Strashko #else
2199c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2200c8395d4eSGrygorii Strashko {
2201c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2202c8395d4eSGrygorii Strashko }
2203a5b4145bSBen Hutchings 
2204c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2205c8395d4eSGrygorii Strashko {
2206c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2207c8395d4eSGrygorii Strashko }
22082e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/
22092e5b38abSRichard Cochran 
22102e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
22112e5b38abSRichard Cochran {
221211f2c988SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
2213606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2214606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
221511f2c988SMugunthan V N 
22162e5b38abSRichard Cochran 	if (!netif_running(dev))
22172e5b38abSRichard Cochran 		return -EINVAL;
22182e5b38abSRichard Cochran 
221911f2c988SMugunthan V N 	switch (cmd) {
222011f2c988SMugunthan V N 	case SIOCSHWTSTAMP:
2221a5b4145bSBen Hutchings 		return cpsw_hwtstamp_set(dev, req);
2222a5b4145bSBen Hutchings 	case SIOCGHWTSTAMP:
2223a5b4145bSBen Hutchings 		return cpsw_hwtstamp_get(dev, req);
22242e5b38abSRichard Cochran 	}
22252e5b38abSRichard Cochran 
2226606f3993SIvan Khoronzhuk 	if (!cpsw->slaves[slave_no].phy)
2227c1b59947SStefan Sørensen 		return -EOPNOTSUPP;
2228606f3993SIvan Khoronzhuk 	return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
222911f2c988SMugunthan V N }
223011f2c988SMugunthan V N 
2231df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev)
2232df828598SMugunthan V N {
2233df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
22342c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2235e05107e6SIvan Khoronzhuk 	int ch;
2236df828598SMugunthan V N 
2237df828598SMugunthan V N 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
22388dc43ddcSTobias Klauser 	ndev->stats.tx_errors++;
22392c836bd9SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2240e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
22418feb0a19SIvan Khoronzhuk 		cpdma_chan_stop(cpsw->txv[ch].ch);
22428feb0a19SIvan Khoronzhuk 		cpdma_chan_start(cpsw->txv[ch].ch);
2243e05107e6SIvan Khoronzhuk 	}
2244e05107e6SIvan Khoronzhuk 
22452c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
224675514b66SGrygorii Strashko 	netif_trans_update(ndev);
224775514b66SGrygorii Strashko 	netif_tx_wake_all_queues(ndev);
2248df828598SMugunthan V N }
2249df828598SMugunthan V N 
2250dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
2251dcfd8d58SMugunthan V N {
2252dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2253dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
2254649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2255dcfd8d58SMugunthan V N 	int flags = 0;
2256dcfd8d58SMugunthan V N 	u16 vid = 0;
2257a6c5d14fSGrygorii Strashko 	int ret;
2258dcfd8d58SMugunthan V N 
2259dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
2260dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
2261dcfd8d58SMugunthan V N 
226256e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2263a6c5d14fSGrygorii Strashko 	if (ret < 0) {
226456e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2265a6c5d14fSGrygorii Strashko 		return ret;
2266a6c5d14fSGrygorii Strashko 	}
2267a6c5d14fSGrygorii Strashko 
2268606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
2269606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
2270dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
2271dcfd8d58SMugunthan V N 	}
2272dcfd8d58SMugunthan V N 
22732a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
2274dcfd8d58SMugunthan V N 			   flags, vid);
22752a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
2276dcfd8d58SMugunthan V N 			   flags, vid);
2277dcfd8d58SMugunthan V N 
2278dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2279dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2280dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
2281dcfd8d58SMugunthan V N 
228256e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2283a6c5d14fSGrygorii Strashko 
2284dcfd8d58SMugunthan V N 	return 0;
2285dcfd8d58SMugunthan V N }
2286dcfd8d58SMugunthan V N 
2287df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2288df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev)
2289df828598SMugunthan V N {
2290dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2291df828598SMugunthan V N 
2292dbc4ec52SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2293dbc4ec52SIvan Khoronzhuk 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2294dbc4ec52SIvan Khoronzhuk 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2295dbc4ec52SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
2296df828598SMugunthan V N }
2297df828598SMugunthan V N #endif
2298df828598SMugunthan V N 
22993b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
23003b72c2feSMugunthan V N 				unsigned short vid)
23013b72c2feSMugunthan V N {
23023b72c2feSMugunthan V N 	int ret;
23039f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
23045b3a5a14SIvan Khoronzhuk 	int mcast_mask;
23059f6bd8faSMugunthan V N 	u32 port_mask;
2306606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
23079f6bd8faSMugunthan V N 
2308606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
23099f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
23109f6bd8faSMugunthan V N 
23115b3a5a14SIvan Khoronzhuk 		mcast_mask = ALE_PORT_HOST;
23129f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
23135b3a5a14SIvan Khoronzhuk 			unreg_mcast_mask = mcast_mask;
23149f6bd8faSMugunthan V N 	} else {
23159f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
23165b3a5a14SIvan Khoronzhuk 		mcast_mask = port_mask;
23171e5c4bc4SLennart Sorensen 
23181e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
23191e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
23201e5c4bc4SLennart Sorensen 		else
23211e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
23229f6bd8faSMugunthan V N 	}
23233b72c2feSMugunthan V N 
23242a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
232561f1cef9SGrygorii Strashko 				unreg_mcast_mask);
23263b72c2feSMugunthan V N 	if (ret != 0)
23273b72c2feSMugunthan V N 		return ret;
23283b72c2feSMugunthan V N 
23292a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
233071a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
23313b72c2feSMugunthan V N 	if (ret != 0)
23323b72c2feSMugunthan V N 		goto clean_vid;
23333b72c2feSMugunthan V N 
23342a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
23355b3a5a14SIvan Khoronzhuk 				 mcast_mask, ALE_VLAN, vid, 0);
23363b72c2feSMugunthan V N 	if (ret != 0)
23373b72c2feSMugunthan V N 		goto clean_vlan_ucast;
23383b72c2feSMugunthan V N 	return 0;
23393b72c2feSMugunthan V N 
23403b72c2feSMugunthan V N clean_vlan_ucast:
23412a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
234271a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
23433b72c2feSMugunthan V N clean_vid:
23442a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
23453b72c2feSMugunthan V N 	return ret;
23463b72c2feSMugunthan V N }
23473b72c2feSMugunthan V N 
23483b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
234980d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
23503b72c2feSMugunthan V N {
23513b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2352649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2353a6c5d14fSGrygorii Strashko 	int ret;
23543b72c2feSMugunthan V N 
2355606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
23563b72c2feSMugunthan V N 		return 0;
23573b72c2feSMugunthan V N 
235856e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2359a6c5d14fSGrygorii Strashko 	if (ret < 0) {
236056e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2361a6c5d14fSGrygorii Strashko 		return ret;
2362a6c5d14fSGrygorii Strashko 	}
2363a6c5d14fSGrygorii Strashko 
2364606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
236502a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
236602a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
236702a54164SMugunthan V N 		 * EMAC port separation
236802a54164SMugunthan V N 		 */
236902a54164SMugunthan V N 		int i;
237002a54164SMugunthan V N 
2371606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2372803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
2373803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
2374803c4f64SIvan Khoronzhuk 				goto err;
2375803c4f64SIvan Khoronzhuk 			}
237602a54164SMugunthan V N 		}
237702a54164SMugunthan V N 	}
237802a54164SMugunthan V N 
23793b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2380a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
2381803c4f64SIvan Khoronzhuk err:
238256e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2383a6c5d14fSGrygorii Strashko 	return ret;
23843b72c2feSMugunthan V N }
23853b72c2feSMugunthan V N 
23863b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
238780d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
23883b72c2feSMugunthan V N {
23893b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2390649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
23913b72c2feSMugunthan V N 	int ret;
23923b72c2feSMugunthan V N 
2393606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
23943b72c2feSMugunthan V N 		return 0;
23953b72c2feSMugunthan V N 
239656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2397a6c5d14fSGrygorii Strashko 	if (ret < 0) {
239856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2399a6c5d14fSGrygorii Strashko 		return ret;
2400a6c5d14fSGrygorii Strashko 	}
2401a6c5d14fSGrygorii Strashko 
2402606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
240302a54164SMugunthan V N 		int i;
240402a54164SMugunthan V N 
2405606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2406606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
2407803c4f64SIvan Khoronzhuk 				goto err;
240802a54164SMugunthan V N 		}
240902a54164SMugunthan V N 	}
241002a54164SMugunthan V N 
24113b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
24122a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2413be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
241461f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
2415be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
24163b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
2417803c4f64SIvan Khoronzhuk err:
241856e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2419a6c5d14fSGrygorii Strashko 	return ret;
24203b72c2feSMugunthan V N }
24213b72c2feSMugunthan V N 
242283fcad0cSIvan Khoronzhuk static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
242383fcad0cSIvan Khoronzhuk {
242483fcad0cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
242583fcad0cSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
242652986a2fSIvan Khoronzhuk 	struct cpsw_slave *slave;
242732b78d85SIvan Khoronzhuk 	u32 min_rate;
242883fcad0cSIvan Khoronzhuk 	u32 ch_rate;
242952986a2fSIvan Khoronzhuk 	int i, ret;
243083fcad0cSIvan Khoronzhuk 
243183fcad0cSIvan Khoronzhuk 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
243283fcad0cSIvan Khoronzhuk 	if (ch_rate == rate)
243383fcad0cSIvan Khoronzhuk 		return 0;
243483fcad0cSIvan Khoronzhuk 
243532b78d85SIvan Khoronzhuk 	ch_rate = rate * 1000;
243683fcad0cSIvan Khoronzhuk 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
243732b78d85SIvan Khoronzhuk 	if ((ch_rate < min_rate && ch_rate)) {
243832b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
243983fcad0cSIvan Khoronzhuk 			min_rate);
244083fcad0cSIvan Khoronzhuk 		return -EINVAL;
244183fcad0cSIvan Khoronzhuk 	}
244283fcad0cSIvan Khoronzhuk 
24430be01b8eSIvan Khoronzhuk 	if (rate > cpsw->speed) {
244432b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
244532b78d85SIvan Khoronzhuk 		return -EINVAL;
244632b78d85SIvan Khoronzhuk 	}
244732b78d85SIvan Khoronzhuk 
244883fcad0cSIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
244983fcad0cSIvan Khoronzhuk 	if (ret < 0) {
245083fcad0cSIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
245183fcad0cSIvan Khoronzhuk 		return ret;
245283fcad0cSIvan Khoronzhuk 	}
245383fcad0cSIvan Khoronzhuk 
245432b78d85SIvan Khoronzhuk 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
245583fcad0cSIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
245632b78d85SIvan Khoronzhuk 
245732b78d85SIvan Khoronzhuk 	if (ret)
245832b78d85SIvan Khoronzhuk 		return ret;
245932b78d85SIvan Khoronzhuk 
246052986a2fSIvan Khoronzhuk 	/* update rates for slaves tx queues */
246152986a2fSIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
246252986a2fSIvan Khoronzhuk 		slave = &cpsw->slaves[i];
246352986a2fSIvan Khoronzhuk 		if (!slave->ndev)
246452986a2fSIvan Khoronzhuk 			continue;
246552986a2fSIvan Khoronzhuk 
246652986a2fSIvan Khoronzhuk 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
246752986a2fSIvan Khoronzhuk 	}
246852986a2fSIvan Khoronzhuk 
246932b78d85SIvan Khoronzhuk 	cpsw_split_res(ndev);
247083fcad0cSIvan Khoronzhuk 	return ret;
247183fcad0cSIvan Khoronzhuk }
247283fcad0cSIvan Khoronzhuk 
24737929a668SIvan Khoronzhuk static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
24747929a668SIvan Khoronzhuk {
24757929a668SIvan Khoronzhuk 	struct tc_mqprio_qopt_offload *mqprio = type_data;
24767929a668SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
24777929a668SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
24787929a668SIvan Khoronzhuk 	int fifo, num_tc, count, offset;
24797929a668SIvan Khoronzhuk 	struct cpsw_slave *slave;
24807929a668SIvan Khoronzhuk 	u32 tx_prio_map = 0;
24817929a668SIvan Khoronzhuk 	int i, tc, ret;
24827929a668SIvan Khoronzhuk 
24837929a668SIvan Khoronzhuk 	num_tc = mqprio->qopt.num_tc;
24847929a668SIvan Khoronzhuk 	if (num_tc > CPSW_TC_NUM)
24857929a668SIvan Khoronzhuk 		return -EINVAL;
24867929a668SIvan Khoronzhuk 
24877929a668SIvan Khoronzhuk 	if (mqprio->mode != TC_MQPRIO_MODE_DCB)
24887929a668SIvan Khoronzhuk 		return -EINVAL;
24897929a668SIvan Khoronzhuk 
24907929a668SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
24917929a668SIvan Khoronzhuk 	if (ret < 0) {
24927929a668SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
24937929a668SIvan Khoronzhuk 		return ret;
24947929a668SIvan Khoronzhuk 	}
24957929a668SIvan Khoronzhuk 
24967929a668SIvan Khoronzhuk 	if (num_tc) {
24977929a668SIvan Khoronzhuk 		for (i = 0; i < 8; i++) {
24987929a668SIvan Khoronzhuk 			tc = mqprio->qopt.prio_tc_map[i];
24997929a668SIvan Khoronzhuk 			fifo = cpsw_tc_to_fifo(tc, num_tc);
25007929a668SIvan Khoronzhuk 			tx_prio_map |= fifo << (4 * i);
25017929a668SIvan Khoronzhuk 		}
25027929a668SIvan Khoronzhuk 
25037929a668SIvan Khoronzhuk 		netdev_set_num_tc(ndev, num_tc);
25047929a668SIvan Khoronzhuk 		for (i = 0; i < num_tc; i++) {
25057929a668SIvan Khoronzhuk 			count = mqprio->qopt.count[i];
25067929a668SIvan Khoronzhuk 			offset = mqprio->qopt.offset[i];
25077929a668SIvan Khoronzhuk 			netdev_set_tc_queue(ndev, i, count, offset);
25087929a668SIvan Khoronzhuk 		}
25097929a668SIvan Khoronzhuk 	}
25107929a668SIvan Khoronzhuk 
25117929a668SIvan Khoronzhuk 	if (!mqprio->qopt.hw) {
25127929a668SIvan Khoronzhuk 		/* restore default configuration */
25137929a668SIvan Khoronzhuk 		netdev_reset_tc(ndev);
25147929a668SIvan Khoronzhuk 		tx_prio_map = TX_PRIORITY_MAPPING;
25157929a668SIvan Khoronzhuk 	}
25167929a668SIvan Khoronzhuk 
25177929a668SIvan Khoronzhuk 	priv->mqprio_hw = mqprio->qopt.hw;
25187929a668SIvan Khoronzhuk 
25197929a668SIvan Khoronzhuk 	offset = cpsw->version == CPSW_VERSION_1 ?
25207929a668SIvan Khoronzhuk 		 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
25217929a668SIvan Khoronzhuk 
25227929a668SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
25237929a668SIvan Khoronzhuk 	slave_write(slave, tx_prio_map, offset);
25247929a668SIvan Khoronzhuk 
25257929a668SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
25267929a668SIvan Khoronzhuk 
25277929a668SIvan Khoronzhuk 	return 0;
25287929a668SIvan Khoronzhuk }
25297929a668SIvan Khoronzhuk 
25307929a668SIvan Khoronzhuk static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
25317929a668SIvan Khoronzhuk 			     void *type_data)
25327929a668SIvan Khoronzhuk {
25337929a668SIvan Khoronzhuk 	switch (type) {
253457d90148SIvan Khoronzhuk 	case TC_SETUP_QDISC_CBS:
253557d90148SIvan Khoronzhuk 		return cpsw_set_cbs(ndev, type_data);
253657d90148SIvan Khoronzhuk 
25377929a668SIvan Khoronzhuk 	case TC_SETUP_QDISC_MQPRIO:
25387929a668SIvan Khoronzhuk 		return cpsw_set_mqprio(ndev, type_data);
25397929a668SIvan Khoronzhuk 
25407929a668SIvan Khoronzhuk 	default:
25417929a668SIvan Khoronzhuk 		return -EOPNOTSUPP;
25427929a668SIvan Khoronzhuk 	}
25437929a668SIvan Khoronzhuk }
25447929a668SIvan Khoronzhuk 
2545df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
2546df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
2547df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
2548df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
2549dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
25502e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
2551df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
2552df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
25535c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
255483fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
2555df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2556df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
2557df828598SMugunthan V N #endif
25583b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
25593b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
25607929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
2561df828598SMugunthan V N };
2562df828598SMugunthan V N 
256352c4f0ecSMugunthan V N static int cpsw_get_regs_len(struct net_device *ndev)
256452c4f0ecSMugunthan V N {
2565606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
256652c4f0ecSMugunthan V N 
2567606f3993SIvan Khoronzhuk 	return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
256852c4f0ecSMugunthan V N }
256952c4f0ecSMugunthan V N 
257052c4f0ecSMugunthan V N static void cpsw_get_regs(struct net_device *ndev,
257152c4f0ecSMugunthan V N 			  struct ethtool_regs *regs, void *p)
257252c4f0ecSMugunthan V N {
257352c4f0ecSMugunthan V N 	u32 *reg = p;
25742a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
257552c4f0ecSMugunthan V N 
257652c4f0ecSMugunthan V N 	/* update CPSW IP version */
25772a05a622SIvan Khoronzhuk 	regs->version = cpsw->version;
257852c4f0ecSMugunthan V N 
25792a05a622SIvan Khoronzhuk 	cpsw_ale_dump(cpsw->ale, reg);
258052c4f0ecSMugunthan V N }
258152c4f0ecSMugunthan V N 
2582df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
2583df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
2584df828598SMugunthan V N {
2585649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
258656e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
25877826d43fSJiri Pirko 
258852c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
25897826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
259056e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2591df828598SMugunthan V N }
2592df828598SMugunthan V N 
2593df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev)
2594df828598SMugunthan V N {
2595df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2596df828598SMugunthan V N 	return priv->msg_enable;
2597df828598SMugunthan V N }
2598df828598SMugunthan V N 
2599df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2600df828598SMugunthan V N {
2601df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2602df828598SMugunthan V N 	priv->msg_enable = value;
2603df828598SMugunthan V N }
2604df828598SMugunthan V N 
2605c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
26062e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev,
26072e5b38abSRichard Cochran 			    struct ethtool_ts_info *info)
26082e5b38abSRichard Cochran {
26092a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
26102e5b38abSRichard Cochran 
26112e5b38abSRichard Cochran 	info->so_timestamping =
26122e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_HARDWARE |
26132e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
26142e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_HARDWARE |
26152e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
26162e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE |
26172e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RAW_HARDWARE;
26182a05a622SIvan Khoronzhuk 	info->phc_index = cpsw->cpts->phc_index;
26192e5b38abSRichard Cochran 	info->tx_types =
26202e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_OFF) |
26212e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_ON);
26222e5b38abSRichard Cochran 	info->rx_filters =
26232e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_NONE) |
2624e9523a5aSGrygorii Strashko 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
26252e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2626c8395d4eSGrygorii Strashko 	return 0;
2627c8395d4eSGrygorii Strashko }
26282e5b38abSRichard Cochran #else
2629c8395d4eSGrygorii Strashko static int cpsw_get_ts_info(struct net_device *ndev,
2630c8395d4eSGrygorii Strashko 			    struct ethtool_ts_info *info)
2631c8395d4eSGrygorii Strashko {
26322e5b38abSRichard Cochran 	info->so_timestamping =
26332e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
26342e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
26352e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE;
26362e5b38abSRichard Cochran 	info->phc_index = -1;
26372e5b38abSRichard Cochran 	info->tx_types = 0;
26382e5b38abSRichard Cochran 	info->rx_filters = 0;
26392e5b38abSRichard Cochran 	return 0;
26402e5b38abSRichard Cochran }
2641c8395d4eSGrygorii Strashko #endif
26422e5b38abSRichard Cochran 
26432479876dSPhilippe Reynes static int cpsw_get_link_ksettings(struct net_device *ndev,
26442479876dSPhilippe Reynes 				   struct ethtool_link_ksettings *ecmd)
2645d3bb9c58SMugunthan V N {
2646d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2647606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2648606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2649d3bb9c58SMugunthan V N 
26505514174fSyuval.shaia@oracle.com 	if (!cpsw->slaves[slave_no].phy)
2651d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
26525514174fSyuval.shaia@oracle.com 
26535514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
26545514174fSyuval.shaia@oracle.com 	return 0;
2655d3bb9c58SMugunthan V N }
2656d3bb9c58SMugunthan V N 
26572479876dSPhilippe Reynes static int cpsw_set_link_ksettings(struct net_device *ndev,
26582479876dSPhilippe Reynes 				   const struct ethtool_link_ksettings *ecmd)
2659d3bb9c58SMugunthan V N {
2660d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2661606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2662606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2663d3bb9c58SMugunthan V N 
2664606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
26652479876dSPhilippe Reynes 		return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
26662479876dSPhilippe Reynes 						 ecmd);
2667d3bb9c58SMugunthan V N 	else
2668d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
2669d3bb9c58SMugunthan V N }
2670d3bb9c58SMugunthan V N 
2671d8a64420SMatus Ujhelyi static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2672d8a64420SMatus Ujhelyi {
2673d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2674606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2675606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2676d8a64420SMatus Ujhelyi 
2677d8a64420SMatus Ujhelyi 	wol->supported = 0;
2678d8a64420SMatus Ujhelyi 	wol->wolopts = 0;
2679d8a64420SMatus Ujhelyi 
2680606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2681606f3993SIvan Khoronzhuk 		phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2682d8a64420SMatus Ujhelyi }
2683d8a64420SMatus Ujhelyi 
2684d8a64420SMatus Ujhelyi static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2685d8a64420SMatus Ujhelyi {
2686d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2687606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2688606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2689d8a64420SMatus Ujhelyi 
2690606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2691606f3993SIvan Khoronzhuk 		return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2692d8a64420SMatus Ujhelyi 	else
2693d8a64420SMatus Ujhelyi 		return -EOPNOTSUPP;
2694d8a64420SMatus Ujhelyi }
2695d8a64420SMatus Ujhelyi 
26961923d6e4SMugunthan V N static void cpsw_get_pauseparam(struct net_device *ndev,
26971923d6e4SMugunthan V N 				struct ethtool_pauseparam *pause)
26981923d6e4SMugunthan V N {
26991923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
27001923d6e4SMugunthan V N 
27011923d6e4SMugunthan V N 	pause->autoneg = AUTONEG_DISABLE;
27021923d6e4SMugunthan V N 	pause->rx_pause = priv->rx_pause ? true : false;
27031923d6e4SMugunthan V N 	pause->tx_pause = priv->tx_pause ? true : false;
27041923d6e4SMugunthan V N }
27051923d6e4SMugunthan V N 
27061923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
27071923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
27081923d6e4SMugunthan V N {
27091923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
27101923d6e4SMugunthan V N 	bool link;
27111923d6e4SMugunthan V N 
27121923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
27131923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
27141923d6e4SMugunthan V N 
27151923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
27161923d6e4SMugunthan V N 	return 0;
27171923d6e4SMugunthan V N }
27181923d6e4SMugunthan V N 
27197898b1daSGrygorii Strashko static int cpsw_ethtool_op_begin(struct net_device *ndev)
27207898b1daSGrygorii Strashko {
27217898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2722649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
27237898b1daSGrygorii Strashko 	int ret;
27247898b1daSGrygorii Strashko 
272556e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
27267898b1daSGrygorii Strashko 	if (ret < 0) {
27277898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
272856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
27297898b1daSGrygorii Strashko 	}
27307898b1daSGrygorii Strashko 
27317898b1daSGrygorii Strashko 	return ret;
27327898b1daSGrygorii Strashko }
27337898b1daSGrygorii Strashko 
27347898b1daSGrygorii Strashko static void cpsw_ethtool_op_complete(struct net_device *ndev)
27357898b1daSGrygorii Strashko {
27367898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
27377898b1daSGrygorii Strashko 	int ret;
27387898b1daSGrygorii Strashko 
273956e31bd8SIvan Khoronzhuk 	ret = pm_runtime_put(priv->cpsw->dev);
27407898b1daSGrygorii Strashko 	if (ret < 0)
27417898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
27427898b1daSGrygorii Strashko }
27437898b1daSGrygorii Strashko 
2744ce52c744SIvan Khoronzhuk static void cpsw_get_channels(struct net_device *ndev,
2745ce52c744SIvan Khoronzhuk 			      struct ethtool_channels *ch)
2746ce52c744SIvan Khoronzhuk {
2747ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2748ce52c744SIvan Khoronzhuk 
27499611d6d6SIvan Khoronzhuk 	ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
27509611d6d6SIvan Khoronzhuk 	ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2751ce52c744SIvan Khoronzhuk 	ch->max_combined = 0;
2752ce52c744SIvan Khoronzhuk 	ch->max_other = 0;
2753ce52c744SIvan Khoronzhuk 	ch->other_count = 0;
2754ce52c744SIvan Khoronzhuk 	ch->rx_count = cpsw->rx_ch_num;
2755ce52c744SIvan Khoronzhuk 	ch->tx_count = cpsw->tx_ch_num;
2756ce52c744SIvan Khoronzhuk 	ch->combined_count = 0;
2757ce52c744SIvan Khoronzhuk }
2758ce52c744SIvan Khoronzhuk 
2759ce52c744SIvan Khoronzhuk static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2760ce52c744SIvan Khoronzhuk 				  struct ethtool_channels *ch)
2761ce52c744SIvan Khoronzhuk {
27629611d6d6SIvan Khoronzhuk 	if (cpsw->quirk_irq) {
27639611d6d6SIvan Khoronzhuk 		dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
27649611d6d6SIvan Khoronzhuk 		return -EOPNOTSUPP;
27659611d6d6SIvan Khoronzhuk 	}
27669611d6d6SIvan Khoronzhuk 
2767ce52c744SIvan Khoronzhuk 	if (ch->combined_count)
2768ce52c744SIvan Khoronzhuk 		return -EINVAL;
2769ce52c744SIvan Khoronzhuk 
2770ce52c744SIvan Khoronzhuk 	/* verify we have at least one channel in each direction */
2771ce52c744SIvan Khoronzhuk 	if (!ch->rx_count || !ch->tx_count)
2772ce52c744SIvan Khoronzhuk 		return -EINVAL;
2773ce52c744SIvan Khoronzhuk 
2774ce52c744SIvan Khoronzhuk 	if (ch->rx_count > cpsw->data.channels ||
2775ce52c744SIvan Khoronzhuk 	    ch->tx_count > cpsw->data.channels)
2776ce52c744SIvan Khoronzhuk 		return -EINVAL;
2777ce52c744SIvan Khoronzhuk 
2778ce52c744SIvan Khoronzhuk 	return 0;
2779ce52c744SIvan Khoronzhuk }
2780ce52c744SIvan Khoronzhuk 
2781ce52c744SIvan Khoronzhuk static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2782ce52c744SIvan Khoronzhuk {
2783ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2784ce52c744SIvan Khoronzhuk 	void (*handler)(void *, int, int);
278583fcad0cSIvan Khoronzhuk 	struct netdev_queue *queue;
27868feb0a19SIvan Khoronzhuk 	struct cpsw_vector *vec;
278779b3325dSIvan Khoronzhuk 	int ret, *ch, vch;
2788ce52c744SIvan Khoronzhuk 
2789ce52c744SIvan Khoronzhuk 	if (rx) {
2790ce52c744SIvan Khoronzhuk 		ch = &cpsw->rx_ch_num;
27918feb0a19SIvan Khoronzhuk 		vec = cpsw->rxv;
2792ce52c744SIvan Khoronzhuk 		handler = cpsw_rx_handler;
2793ce52c744SIvan Khoronzhuk 	} else {
2794ce52c744SIvan Khoronzhuk 		ch = &cpsw->tx_ch_num;
27958feb0a19SIvan Khoronzhuk 		vec = cpsw->txv;
2796ce52c744SIvan Khoronzhuk 		handler = cpsw_tx_handler;
2797ce52c744SIvan Khoronzhuk 	}
2798ce52c744SIvan Khoronzhuk 
2799ce52c744SIvan Khoronzhuk 	while (*ch < ch_num) {
280079b3325dSIvan Khoronzhuk 		vch = rx ? *ch : 7 - *ch;
280179b3325dSIvan Khoronzhuk 		vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
280283fcad0cSIvan Khoronzhuk 		queue = netdev_get_tx_queue(priv->ndev, *ch);
280383fcad0cSIvan Khoronzhuk 		queue->tx_maxrate = 0;
2804ce52c744SIvan Khoronzhuk 
28058feb0a19SIvan Khoronzhuk 		if (IS_ERR(vec[*ch].ch))
28068feb0a19SIvan Khoronzhuk 			return PTR_ERR(vec[*ch].ch);
2807ce52c744SIvan Khoronzhuk 
28088feb0a19SIvan Khoronzhuk 		if (!vec[*ch].ch)
2809ce52c744SIvan Khoronzhuk 			return -EINVAL;
2810ce52c744SIvan Khoronzhuk 
2811ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2812ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2813ce52c744SIvan Khoronzhuk 		(*ch)++;
2814ce52c744SIvan Khoronzhuk 	}
2815ce52c744SIvan Khoronzhuk 
2816ce52c744SIvan Khoronzhuk 	while (*ch > ch_num) {
2817ce52c744SIvan Khoronzhuk 		(*ch)--;
2818ce52c744SIvan Khoronzhuk 
28198feb0a19SIvan Khoronzhuk 		ret = cpdma_chan_destroy(vec[*ch].ch);
2820ce52c744SIvan Khoronzhuk 		if (ret)
2821ce52c744SIvan Khoronzhuk 			return ret;
2822ce52c744SIvan Khoronzhuk 
2823ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2824ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2825ce52c744SIvan Khoronzhuk 	}
2826ce52c744SIvan Khoronzhuk 
2827ce52c744SIvan Khoronzhuk 	return 0;
2828ce52c744SIvan Khoronzhuk }
2829ce52c744SIvan Khoronzhuk 
2830ce52c744SIvan Khoronzhuk static int cpsw_update_channels(struct cpsw_priv *priv,
2831ce52c744SIvan Khoronzhuk 				struct ethtool_channels *ch)
2832ce52c744SIvan Khoronzhuk {
2833ce52c744SIvan Khoronzhuk 	int ret;
2834ce52c744SIvan Khoronzhuk 
2835ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2836ce52c744SIvan Khoronzhuk 	if (ret)
2837ce52c744SIvan Khoronzhuk 		return ret;
2838ce52c744SIvan Khoronzhuk 
2839ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2840ce52c744SIvan Khoronzhuk 	if (ret)
2841ce52c744SIvan Khoronzhuk 		return ret;
2842ce52c744SIvan Khoronzhuk 
2843ce52c744SIvan Khoronzhuk 	return 0;
2844ce52c744SIvan Khoronzhuk }
2845ce52c744SIvan Khoronzhuk 
2846022d7ad7SIvan Khoronzhuk static void cpsw_suspend_data_pass(struct net_device *ndev)
2847ce52c744SIvan Khoronzhuk {
2848022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2849ce52c744SIvan Khoronzhuk 	struct cpsw_slave *slave;
2850022d7ad7SIvan Khoronzhuk 	int i;
2851ce52c744SIvan Khoronzhuk 
2852ce52c744SIvan Khoronzhuk 	/* Disable NAPI scheduling */
2853ce52c744SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2854ce52c744SIvan Khoronzhuk 
2855ce52c744SIvan Khoronzhuk 	/* Stop all transmit queues for every network device.
2856ce52c744SIvan Khoronzhuk 	 * Disable re-using rx descriptors with dormant_on.
2857ce52c744SIvan Khoronzhuk 	 */
2858ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2859ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2860ce52c744SIvan Khoronzhuk 			continue;
2861ce52c744SIvan Khoronzhuk 
2862ce52c744SIvan Khoronzhuk 		netif_tx_stop_all_queues(slave->ndev);
2863ce52c744SIvan Khoronzhuk 		netif_dormant_on(slave->ndev);
2864ce52c744SIvan Khoronzhuk 	}
2865ce52c744SIvan Khoronzhuk 
2866ce52c744SIvan Khoronzhuk 	/* Handle rest of tx packets and stop cpdma channels */
2867ce52c744SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
2868022d7ad7SIvan Khoronzhuk }
2869022d7ad7SIvan Khoronzhuk 
2870022d7ad7SIvan Khoronzhuk static int cpsw_resume_data_pass(struct net_device *ndev)
2871022d7ad7SIvan Khoronzhuk {
2872022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2873022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2874022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2875022d7ad7SIvan Khoronzhuk 	int i, ret;
2876022d7ad7SIvan Khoronzhuk 
2877022d7ad7SIvan Khoronzhuk 	/* Allow rx packets handling */
2878022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2879022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2880022d7ad7SIvan Khoronzhuk 			netif_dormant_off(slave->ndev);
2881022d7ad7SIvan Khoronzhuk 
2882022d7ad7SIvan Khoronzhuk 	/* After this receive is started */
2883d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count) {
2884022d7ad7SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
2885022d7ad7SIvan Khoronzhuk 		if (ret)
2886022d7ad7SIvan Khoronzhuk 			return ret;
2887022d7ad7SIvan Khoronzhuk 
2888022d7ad7SIvan Khoronzhuk 		cpdma_ctlr_start(cpsw->dma);
2889022d7ad7SIvan Khoronzhuk 		cpsw_intr_enable(cpsw);
2890022d7ad7SIvan Khoronzhuk 	}
2891022d7ad7SIvan Khoronzhuk 
2892022d7ad7SIvan Khoronzhuk 	/* Resume transmit for every affected interface */
2893022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2894022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2895022d7ad7SIvan Khoronzhuk 			netif_tx_start_all_queues(slave->ndev);
2896022d7ad7SIvan Khoronzhuk 
2897022d7ad7SIvan Khoronzhuk 	return 0;
2898022d7ad7SIvan Khoronzhuk }
2899022d7ad7SIvan Khoronzhuk 
2900022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
2901022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
2902022d7ad7SIvan Khoronzhuk {
2903022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2904022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2905022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2906022d7ad7SIvan Khoronzhuk 	int i, ret;
2907022d7ad7SIvan Khoronzhuk 
2908022d7ad7SIvan Khoronzhuk 	ret = cpsw_check_ch_settings(cpsw, chs);
2909022d7ad7SIvan Khoronzhuk 	if (ret < 0)
2910022d7ad7SIvan Khoronzhuk 		return ret;
2911022d7ad7SIvan Khoronzhuk 
2912022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
2913ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels(priv, chs);
2914ce52c744SIvan Khoronzhuk 	if (ret)
2915ce52c744SIvan Khoronzhuk 		goto err;
2916ce52c744SIvan Khoronzhuk 
2917ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2918ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2919ce52c744SIvan Khoronzhuk 			continue;
2920ce52c744SIvan Khoronzhuk 
2921ce52c744SIvan Khoronzhuk 		/* Inform stack about new count of queues */
2922ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_tx_queues(slave->ndev,
2923ce52c744SIvan Khoronzhuk 						   cpsw->tx_ch_num);
2924ce52c744SIvan Khoronzhuk 		if (ret) {
2925ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of tx queues\n");
2926ce52c744SIvan Khoronzhuk 			goto err;
2927ce52c744SIvan Khoronzhuk 		}
2928ce52c744SIvan Khoronzhuk 
2929ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_rx_queues(slave->ndev,
2930ce52c744SIvan Khoronzhuk 						   cpsw->rx_ch_num);
2931ce52c744SIvan Khoronzhuk 		if (ret) {
2932ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of rx queues\n");
2933ce52c744SIvan Khoronzhuk 			goto err;
2934ce52c744SIvan Khoronzhuk 		}
2935ce52c744SIvan Khoronzhuk 	}
2936ce52c744SIvan Khoronzhuk 
2937d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
293832b78d85SIvan Khoronzhuk 		cpsw_split_res(ndev);
29398feb0a19SIvan Khoronzhuk 
2940022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
2941022d7ad7SIvan Khoronzhuk 	if (!ret)
2942ce52c744SIvan Khoronzhuk 		return 0;
2943ce52c744SIvan Khoronzhuk err:
2944ce52c744SIvan Khoronzhuk 	dev_err(priv->dev, "cannot update channels number, closing device\n");
2945ce52c744SIvan Khoronzhuk 	dev_close(ndev);
2946ce52c744SIvan Khoronzhuk 	return ret;
2947ce52c744SIvan Khoronzhuk }
2948ce52c744SIvan Khoronzhuk 
2949a0909949SYegor Yefremov static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2950a0909949SYegor Yefremov {
2951a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2952a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2953a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2954a0909949SYegor Yefremov 
2955a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2956a0909949SYegor Yefremov 		return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2957a0909949SYegor Yefremov 	else
2958a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2959a0909949SYegor Yefremov }
2960a0909949SYegor Yefremov 
2961a0909949SYegor Yefremov static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2962a0909949SYegor Yefremov {
2963a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2964a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2965a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2966a0909949SYegor Yefremov 
2967a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2968a0909949SYegor Yefremov 		return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2969a0909949SYegor Yefremov 	else
2970a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2971a0909949SYegor Yefremov }
2972a0909949SYegor Yefremov 
29736bb10c2bSYegor Yefremov static int cpsw_nway_reset(struct net_device *ndev)
29746bb10c2bSYegor Yefremov {
29756bb10c2bSYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
29766bb10c2bSYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
29776bb10c2bSYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
29786bb10c2bSYegor Yefremov 
29796bb10c2bSYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
29806bb10c2bSYegor Yefremov 		return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
29816bb10c2bSYegor Yefremov 	else
29826bb10c2bSYegor Yefremov 		return -EOPNOTSUPP;
29836bb10c2bSYegor Yefremov }
29846bb10c2bSYegor Yefremov 
2985be034fc1SGrygorii Strashko static void cpsw_get_ringparam(struct net_device *ndev,
2986be034fc1SGrygorii Strashko 			       struct ethtool_ringparam *ering)
2987be034fc1SGrygorii Strashko {
2988be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2989be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
2990be034fc1SGrygorii Strashko 
2991be034fc1SGrygorii Strashko 	/* not supported */
2992be034fc1SGrygorii Strashko 	ering->tx_max_pending = 0;
2993be034fc1SGrygorii Strashko 	ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
2994f89d21b9SIvan Khoronzhuk 	ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
2995be034fc1SGrygorii Strashko 	ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2996be034fc1SGrygorii Strashko }
2997be034fc1SGrygorii Strashko 
2998be034fc1SGrygorii Strashko static int cpsw_set_ringparam(struct net_device *ndev,
2999be034fc1SGrygorii Strashko 			      struct ethtool_ringparam *ering)
3000be034fc1SGrygorii Strashko {
3001be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
3002be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
3003022d7ad7SIvan Khoronzhuk 	int ret;
3004be034fc1SGrygorii Strashko 
3005be034fc1SGrygorii Strashko 	/* ignore ering->tx_pending - only rx_pending adjustment is supported */
3006be034fc1SGrygorii Strashko 
3007be034fc1SGrygorii Strashko 	if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
3008f89d21b9SIvan Khoronzhuk 	    ering->rx_pending < CPSW_MAX_QUEUES ||
3009f89d21b9SIvan Khoronzhuk 	    ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
3010be034fc1SGrygorii Strashko 		return -EINVAL;
3011be034fc1SGrygorii Strashko 
3012be034fc1SGrygorii Strashko 	if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
3013be034fc1SGrygorii Strashko 		return 0;
3014be034fc1SGrygorii Strashko 
3015022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
3016be034fc1SGrygorii Strashko 
3017be034fc1SGrygorii Strashko 	cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
3018be034fc1SGrygorii Strashko 
3019d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
3020be034fc1SGrygorii Strashko 		cpdma_chan_split_pool(cpsw->dma);
3021be034fc1SGrygorii Strashko 
3022022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
3023022d7ad7SIvan Khoronzhuk 	if (!ret)
3024be034fc1SGrygorii Strashko 		return 0;
3025022d7ad7SIvan Khoronzhuk 
3026022d7ad7SIvan Khoronzhuk 	dev_err(&ndev->dev, "cannot set ring params, closing device\n");
3027be034fc1SGrygorii Strashko 	dev_close(ndev);
3028be034fc1SGrygorii Strashko 	return ret;
3029be034fc1SGrygorii Strashko }
3030be034fc1SGrygorii Strashko 
3031df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
3032df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
3033df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
3034df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
3035df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
30362e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
3037ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
3038ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
3039d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
3040d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
3041d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
30421923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
30431923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
3044d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
3045d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
304652c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
304752c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
30487898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
30497898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
3050ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
3051ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
30522479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
30532479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
3054a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
3055a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
30566bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
3057be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
3058be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
3059df828598SMugunthan V N };
3060df828598SMugunthan V N 
3061606f3993SIvan Khoronzhuk static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
3062549985eeSRichard Cochran 			    u32 slave_reg_ofs, u32 sliver_reg_ofs)
3063df828598SMugunthan V N {
30645d8d0d4dSIvan Khoronzhuk 	void __iomem		*regs = cpsw->regs;
3065df828598SMugunthan V N 	int			slave_num = slave->slave_num;
3066606f3993SIvan Khoronzhuk 	struct cpsw_slave_data	*data = cpsw->data.slave_data + slave_num;
3067df828598SMugunthan V N 
3068df828598SMugunthan V N 	slave->data	= data;
3069549985eeSRichard Cochran 	slave->regs	= regs + slave_reg_ofs;
3070549985eeSRichard Cochran 	slave->sliver	= regs + sliver_reg_ofs;
3071d9ba8f9eSMugunthan V N 	slave->port_vlan = data->dual_emac_res_vlan;
3072df828598SMugunthan V N }
3073df828598SMugunthan V N 
3074552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
30752eb32b0aSMugunthan V N 			 struct platform_device *pdev)
30762eb32b0aSMugunthan V N {
30772eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
30782eb32b0aSMugunthan V N 	struct device_node *slave_node;
30792eb32b0aSMugunthan V N 	int i = 0, ret;
30802eb32b0aSMugunthan V N 	u32 prop;
30812eb32b0aSMugunthan V N 
30822eb32b0aSMugunthan V N 	if (!node)
30832eb32b0aSMugunthan V N 		return -EINVAL;
30842eb32b0aSMugunthan V N 
30852eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
308688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
30872eb32b0aSMugunthan V N 		return -EINVAL;
30882eb32b0aSMugunthan V N 	}
30892eb32b0aSMugunthan V N 	data->slaves = prop;
30902eb32b0aSMugunthan V N 
3091e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
309288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
3093aa1a15e2SDaniel Mack 		return -EINVAL;
309478ca0b28SRichard Cochran 	}
3095e86ac13bSMugunthan V N 	data->active_slave = prop;
309678ca0b28SRichard Cochran 
3097a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
3098a86854d0SKees Cook 					data->slaves,
3099a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
3100b2adaca9SJoe Perches 					GFP_KERNEL);
3101b2adaca9SJoe Perches 	if (!data->slave_data)
3102aa1a15e2SDaniel Mack 		return -ENOMEM;
31032eb32b0aSMugunthan V N 
31042eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
310588c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
3106aa1a15e2SDaniel Mack 		return -EINVAL;
31072eb32b0aSMugunthan V N 	}
31082eb32b0aSMugunthan V N 	data->channels = prop;
31092eb32b0aSMugunthan V N 
31102eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
311188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
3112aa1a15e2SDaniel Mack 		return -EINVAL;
31132eb32b0aSMugunthan V N 	}
31142eb32b0aSMugunthan V N 	data->ale_entries = prop;
31152eb32b0aSMugunthan V N 
31162eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
311788c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
3118aa1a15e2SDaniel Mack 		return -EINVAL;
31192eb32b0aSMugunthan V N 	}
31202eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
31212eb32b0aSMugunthan V N 
31222eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
312388c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
3124aa1a15e2SDaniel Mack 		return -EINVAL;
31252eb32b0aSMugunthan V N 	}
31262eb32b0aSMugunthan V N 	data->mac_control = prop;
31272eb32b0aSMugunthan V N 
3128281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
3129281abd96SMarkus Pargmann 		data->dual_emac = 1;
3130d9ba8f9eSMugunthan V N 
31311fb19aa7SVaibhav Hiremath 	/*
31321fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
31331fb19aa7SVaibhav Hiremath 	 */
31341fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
31351fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
31361fb19aa7SVaibhav Hiremath 	if (ret)
313788c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
31381fb19aa7SVaibhav Hiremath 
31398658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
3140549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
3141549985eeSRichard Cochran 		const void *mac_addr = NULL;
3142549985eeSRichard Cochran 		int lenp;
3143549985eeSRichard Cochran 		const __be32 *parp;
3144549985eeSRichard Cochran 
3145f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
3146f468b10eSMarkus Pargmann 		if (strcmp(slave_node->name, "slave"))
3147f468b10eSMarkus Pargmann 			continue;
3148f468b10eSMarkus Pargmann 
3149552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
3150552165bcSDavid Rivshin 							"phy-handle", 0);
3151f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
3152ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
3153ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
3154f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
3155f7ce9103SRob Herring 				i, slave_data->phy_node);
3156ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
3157dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
3158dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
3159dfc0a6d3SDavid Rivshin 			 */
31601f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
316123a09873SJohan Hovold 			if (ret) {
316223a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
316323a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
31641f71e8c9SMarkus Brunner 				return ret;
316523a09873SJohan Hovold 			}
316606cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
3167f1eea5c1SDavid Rivshin 		} else if (parp) {
3168f1eea5c1SDavid Rivshin 			u32 phyid;
3169f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
3170f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
3171f1eea5c1SDavid Rivshin 
3172f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
3173f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
317447276fccSMugunthan V N 				goto no_phy_slave;
3175549985eeSRichard Cochran 			}
3176549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
3177549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
3178549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
317960e71ab5SJohan Hovold 			of_node_put(mdio_node);
31806954cc1fSJohan Hovold 			if (!mdio) {
318156fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
31826954cc1fSJohan Hovold 				return -EINVAL;
31836954cc1fSJohan Hovold 			}
3184549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
3185549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
318686e1d5adSJohan Hovold 			put_device(&mdio->dev);
3187f1eea5c1SDavid Rivshin 		} else {
3188ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
3189ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
3190ae092b5bSDavid Rivshin 				i);
3191f1eea5c1SDavid Rivshin 			goto no_phy_slave;
3192f1eea5c1SDavid Rivshin 		}
319347276fccSMugunthan V N 		slave_data->phy_if = of_get_phy_mode(slave_node);
319447276fccSMugunthan V N 		if (slave_data->phy_if < 0) {
319547276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
319647276fccSMugunthan V N 				i);
319747276fccSMugunthan V N 			return slave_data->phy_if;
319847276fccSMugunthan V N 		}
319947276fccSMugunthan V N 
320047276fccSMugunthan V N no_phy_slave:
3201549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
32020ba517b1SMarkus Pargmann 		if (mac_addr) {
3203549985eeSRichard Cochran 			memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
32040ba517b1SMarkus Pargmann 		} else {
3205b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
32060ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
32070ba517b1SMarkus Pargmann 			if (ret)
32080ba517b1SMarkus Pargmann 				return ret;
32090ba517b1SMarkus Pargmann 		}
3210d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
321191c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
3212d9ba8f9eSMugunthan V N 						 &prop)) {
321388c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
3214d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
321588c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
3216d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
3217d9ba8f9eSMugunthan V N 			} else {
3218d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
3219d9ba8f9eSMugunthan V N 			}
3220d9ba8f9eSMugunthan V N 		}
3221d9ba8f9eSMugunthan V N 
3222549985eeSRichard Cochran 		i++;
32233a27bfacSMugunthan V N 		if (i == data->slaves)
32243a27bfacSMugunthan V N 			break;
3225549985eeSRichard Cochran 	}
3226549985eeSRichard Cochran 
32272eb32b0aSMugunthan V N 	return 0;
32282eb32b0aSMugunthan V N }
32292eb32b0aSMugunthan V N 
3230a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
3231a4e32b0dSJohan Hovold {
32328cbcc466SJohan Hovold 	struct net_device *ndev = platform_get_drvdata(pdev);
32338cbcc466SJohan Hovold 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
32348cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
32358cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
32368cbcc466SJohan Hovold 	struct device_node *slave_node;
32378cbcc466SJohan Hovold 	int i = 0;
32388cbcc466SJohan Hovold 
32398cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
32408cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
32418cbcc466SJohan Hovold 
32428cbcc466SJohan Hovold 		if (strcmp(slave_node->name, "slave"))
32438cbcc466SJohan Hovold 			continue;
32448cbcc466SJohan Hovold 
32453f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
32463f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
32478cbcc466SJohan Hovold 
32488cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
32498cbcc466SJohan Hovold 
32508cbcc466SJohan Hovold 		i++;
32518cbcc466SJohan Hovold 		if (i == data->slaves)
32528cbcc466SJohan Hovold 			break;
32538cbcc466SJohan Hovold 	}
32548cbcc466SJohan Hovold 
3255a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
3256a4e32b0dSJohan Hovold }
3257a4e32b0dSJohan Hovold 
325856e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
3259d9ba8f9eSMugunthan V N {
3260606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
3261606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
3262d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
3263d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
3264e38b5a3dSIvan Khoronzhuk 	int ret = 0;
3265d9ba8f9eSMugunthan V N 
3266e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3267d9ba8f9eSMugunthan V N 	if (!ndev) {
326856e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
3269d9ba8f9eSMugunthan V N 		return -ENOMEM;
3270d9ba8f9eSMugunthan V N 	}
3271d9ba8f9eSMugunthan V N 
3272d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
3273606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
3274d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
3275d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
3276d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3277d9ba8f9eSMugunthan V N 
3278d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3279d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3280d9ba8f9eSMugunthan V N 			ETH_ALEN);
328156e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
328256e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3283d9ba8f9eSMugunthan V N 	} else {
32846c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
328556e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
328656e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3287d9ba8f9eSMugunthan V N 	}
3288d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3289d9ba8f9eSMugunthan V N 
3290d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
3291606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
3292193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3293d9ba8f9eSMugunthan V N 
3294d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
32957ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
3296d9ba8f9eSMugunthan V N 
3297d9ba8f9eSMugunthan V N 	/* register the network device */
329856e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
3299d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
3300d9ba8f9eSMugunthan V N 	if (ret) {
330156e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
3302d9ba8f9eSMugunthan V N 		free_netdev(ndev);
3303d9ba8f9eSMugunthan V N 		ret = -ENODEV;
3304d9ba8f9eSMugunthan V N 	}
3305d9ba8f9eSMugunthan V N 
3306d9ba8f9eSMugunthan V N 	return ret;
3307d9ba8f9eSMugunthan V N }
3308d9ba8f9eSMugunthan V N 
33097da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
33109611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
33119611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
33129611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
33139611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
33147da11600SMugunthan V N 	{ /* sentinel */ },
33157da11600SMugunthan V N };
33167da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
33177da11600SMugunthan V N 
33189611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
33199611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
33209611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
33219611d6d6SIvan Khoronzhuk };
33229611d6d6SIvan Khoronzhuk 
3323663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
3324df828598SMugunthan V N {
3325ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
3326d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
3327df828598SMugunthan V N 	struct net_device		*ndev;
3328df828598SMugunthan V N 	struct cpsw_priv		*priv;
3329df828598SMugunthan V N 	struct cpdma_params		dma_params;
3330df828598SMugunthan V N 	struct cpsw_ale_params		ale_params;
3331aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
33328a2c9a5aSGrygorii Strashko 	void __iomem			*cpts_regs;
3333aa1a15e2SDaniel Mack 	struct resource			*res, *ss_res;
33341d147ccbSMugunthan V N 	struct gpio_descs		*mode;
3335549985eeSRichard Cochran 	u32 slave_offset, sliver_offset, slave_size;
33369611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
3337649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
333879b3325dSIvan Khoronzhuk 	int ret = 0, i, ch;
33395087b915SFelipe Balbi 	int irq;
3340df828598SMugunthan V N 
3341649a1688SIvan Khoronzhuk 	cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
33423420ea88SJohan Hovold 	if (!cpsw)
33433420ea88SJohan Hovold 		return -ENOMEM;
33443420ea88SJohan Hovold 
334556e31bd8SIvan Khoronzhuk 	cpsw->dev = &pdev->dev;
3346649a1688SIvan Khoronzhuk 
3347e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3348df828598SMugunthan V N 	if (!ndev) {
334988c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "error allocating net_device\n");
3350df828598SMugunthan V N 		return -ENOMEM;
3351df828598SMugunthan V N 	}
3352df828598SMugunthan V N 
3353df828598SMugunthan V N 	platform_set_drvdata(pdev, ndev);
3354df828598SMugunthan V N 	priv = netdev_priv(ndev);
3355649a1688SIvan Khoronzhuk 	priv->cpsw = cpsw;
3356df828598SMugunthan V N 	priv->ndev = ndev;
3357df828598SMugunthan V N 	priv->dev  = &ndev->dev;
3358df828598SMugunthan V N 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
33592a05a622SIvan Khoronzhuk 	cpsw->rx_packet_max = max(rx_packet_max, 128);
3360df828598SMugunthan V N 
33611d147ccbSMugunthan V N 	mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
33621d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
33631d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
33641d147ccbSMugunthan V N 		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
33651d147ccbSMugunthan V N 		goto clean_ndev_ret;
33661d147ccbSMugunthan V N 	}
33671d147ccbSMugunthan V N 
33681fb19aa7SVaibhav Hiremath 	/*
33691fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
33701fb19aa7SVaibhav Hiremath 	 */
33711fb19aa7SVaibhav Hiremath 	pm_runtime_enable(&pdev->dev);
33721fb19aa7SVaibhav Hiremath 
3373739683b4SMugunthan V N 	/* Select default pin state */
3374739683b4SMugunthan V N 	pinctrl_pm_select_default_state(&pdev->dev);
3375739683b4SMugunthan V N 
3376a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
3377a4e32b0dSJohan Hovold 	 * registers
3378a4e32b0dSJohan Hovold 	 */
3379a4e32b0dSJohan Hovold 	ret = pm_runtime_get_sync(&pdev->dev);
3380a4e32b0dSJohan Hovold 	if (ret < 0) {
3381a4e32b0dSJohan Hovold 		pm_runtime_put_noidle(&pdev->dev);
3382aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
33832eb32b0aSMugunthan V N 	}
3384a4e32b0dSJohan Hovold 
338523a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
338623a09873SJohan Hovold 	if (ret)
3387a4e32b0dSJohan Hovold 		goto clean_dt_ret;
338823a09873SJohan Hovold 
3389606f3993SIvan Khoronzhuk 	data = &cpsw->data;
3390e05107e6SIvan Khoronzhuk 	cpsw->rx_ch_num = 1;
3391e05107e6SIvan Khoronzhuk 	cpsw->tx_ch_num = 1;
33922eb32b0aSMugunthan V N 
3393df828598SMugunthan V N 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3394df828598SMugunthan V N 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
339588c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3396df828598SMugunthan V N 	} else {
33977efd26d0SJoe Perches 		eth_random_addr(priv->mac_addr);
339888c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3399df828598SMugunthan V N 	}
3400df828598SMugunthan V N 
3401df828598SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3402df828598SMugunthan V N 
3403a86854d0SKees Cook 	cpsw->slaves = devm_kcalloc(&pdev->dev,
3404a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
3405df828598SMugunthan V N 				    GFP_KERNEL);
3406606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
3407aa1a15e2SDaniel Mack 		ret = -ENOMEM;
3408a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3409df828598SMugunthan V N 	}
3410df828598SMugunthan V N 	for (i = 0; i < data->slaves; i++)
3411606f3993SIvan Khoronzhuk 		cpsw->slaves[i].slave_num = i;
3412df828598SMugunthan V N 
3413606f3993SIvan Khoronzhuk 	cpsw->slaves[0].ndev = ndev;
3414d9ba8f9eSMugunthan V N 	priv->emac_port = 0;
3415d9ba8f9eSMugunthan V N 
3416ef4183a1SIvan Khoronzhuk 	clk = devm_clk_get(&pdev->dev, "fck");
3417ef4183a1SIvan Khoronzhuk 	if (IS_ERR(clk)) {
3418aa1a15e2SDaniel Mack 		dev_err(priv->dev, "fck is not found\n");
3419f150bd7fSMugunthan V N 		ret = -ENODEV;
3420a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3421df828598SMugunthan V N 	}
34222a05a622SIvan Khoronzhuk 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3423df828598SMugunthan V N 
3424aa1a15e2SDaniel Mack 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3425aa1a15e2SDaniel Mack 	ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3426aa1a15e2SDaniel Mack 	if (IS_ERR(ss_regs)) {
3427aa1a15e2SDaniel Mack 		ret = PTR_ERR(ss_regs);
3428a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3429df828598SMugunthan V N 	}
34305d8d0d4dSIvan Khoronzhuk 	cpsw->regs = ss_regs;
3431df828598SMugunthan V N 
34322a05a622SIvan Khoronzhuk 	cpsw->version = readl(&cpsw->regs->id_ver);
3433f280e89aSMugunthan V N 
3434aa1a15e2SDaniel Mack 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
34355d8d0d4dSIvan Khoronzhuk 	cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
34365d8d0d4dSIvan Khoronzhuk 	if (IS_ERR(cpsw->wr_regs)) {
34375d8d0d4dSIvan Khoronzhuk 		ret = PTR_ERR(cpsw->wr_regs);
3438a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3439df828598SMugunthan V N 	}
3440df828598SMugunthan V N 
3441df828598SMugunthan V N 	memset(&dma_params, 0, sizeof(dma_params));
3442549985eeSRichard Cochran 	memset(&ale_params, 0, sizeof(ale_params));
3443549985eeSRichard Cochran 
34442a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
3445549985eeSRichard Cochran 	case CPSW_VERSION_1:
34465d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
34478a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW1_CPTS_OFFSET;
34485d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
3449549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
3450549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
3451549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
3452549985eeSRichard Cochran 		slave_offset         = CPSW1_SLAVE_OFFSET;
3453549985eeSRichard Cochran 		slave_size           = CPSW1_SLAVE_SIZE;
3454549985eeSRichard Cochran 		sliver_offset        = CPSW1_SLIVER_OFFSET;
3455549985eeSRichard Cochran 		dma_params.desc_mem_phys = 0;
3456549985eeSRichard Cochran 		break;
3457549985eeSRichard Cochran 	case CPSW_VERSION_2:
3458c193f365SMugunthan V N 	case CPSW_VERSION_3:
3459926489beSMugunthan V N 	case CPSW_VERSION_4:
34605d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
34618a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW2_CPTS_OFFSET;
34625d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
3463549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
3464549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
3465549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
3466549985eeSRichard Cochran 		slave_offset         = CPSW2_SLAVE_OFFSET;
3467549985eeSRichard Cochran 		slave_size           = CPSW2_SLAVE_SIZE;
3468549985eeSRichard Cochran 		sliver_offset        = CPSW2_SLIVER_OFFSET;
3469549985eeSRichard Cochran 		dma_params.desc_mem_phys =
3470aa1a15e2SDaniel Mack 			(u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3471549985eeSRichard Cochran 		break;
3472549985eeSRichard Cochran 	default:
34732a05a622SIvan Khoronzhuk 		dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3474549985eeSRichard Cochran 		ret = -ENODEV;
3475a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3476549985eeSRichard Cochran 	}
3477606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
3478606f3993SIvan Khoronzhuk 		struct cpsw_slave *slave = &cpsw->slaves[i];
3479606f3993SIvan Khoronzhuk 
3480606f3993SIvan Khoronzhuk 		cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3481549985eeSRichard Cochran 		slave_offset  += slave_size;
3482549985eeSRichard Cochran 		sliver_offset += SLIVER_SIZE;
3483549985eeSRichard Cochran 	}
3484549985eeSRichard Cochran 
3485df828598SMugunthan V N 	dma_params.dev		= &pdev->dev;
3486549985eeSRichard Cochran 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
3487549985eeSRichard Cochran 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
3488549985eeSRichard Cochran 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
3489549985eeSRichard Cochran 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
3490549985eeSRichard Cochran 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
3491df828598SMugunthan V N 
3492df828598SMugunthan V N 	dma_params.num_chan		= data->channels;
3493df828598SMugunthan V N 	dma_params.has_soft_reset	= true;
3494df828598SMugunthan V N 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
3495df828598SMugunthan V N 	dma_params.desc_mem_size	= data->bd_ram_size;
3496df828598SMugunthan V N 	dma_params.desc_align		= 16;
3497df828598SMugunthan V N 	dma_params.has_ext_regs		= true;
3498549985eeSRichard Cochran 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
349983fcad0cSIvan Khoronzhuk 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
350090225bf0SGrygorii Strashko 	dma_params.descs_pool_size	= descs_pool_size;
3501df828598SMugunthan V N 
35022c836bd9SIvan Khoronzhuk 	cpsw->dma = cpdma_ctlr_create(&dma_params);
35032c836bd9SIvan Khoronzhuk 	if (!cpsw->dma) {
3504df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma\n");
3505df828598SMugunthan V N 		ret = -ENOMEM;
3506a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3507df828598SMugunthan V N 	}
3508df828598SMugunthan V N 
35099611d6d6SIvan Khoronzhuk 	soc = soc_device_match(cpsw_soc_devices);
35109611d6d6SIvan Khoronzhuk 	if (soc)
35119611d6d6SIvan Khoronzhuk 		cpsw->quirk_irq = 1;
35129611d6d6SIvan Khoronzhuk 
351379b3325dSIvan Khoronzhuk 	ch = cpsw->quirk_irq ? 0 : 7;
351479b3325dSIvan Khoronzhuk 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
35158a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->txv[0].ch)) {
35168a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing tx dma channel\n");
35178a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->txv[0].ch);
35188a83c5d7SIvan Khoronzhuk 		goto clean_dma_ret;
35198a83c5d7SIvan Khoronzhuk 	}
35208a83c5d7SIvan Khoronzhuk 
35218feb0a19SIvan Khoronzhuk 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
35228a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->rxv[0].ch)) {
35238a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing rx dma channel\n");
35248a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->rxv[0].ch);
3525df828598SMugunthan V N 		goto clean_dma_ret;
3526df828598SMugunthan V N 	}
3527df828598SMugunthan V N 
35289fe9aa0bSIvan Khoronzhuk 	ale_params.dev			= &pdev->dev;
3529df828598SMugunthan V N 	ale_params.ale_ageout		= ale_ageout;
3530df828598SMugunthan V N 	ale_params.ale_entries		= data->ale_entries;
3531c6395f12SGrygorii Strashko 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
3532df828598SMugunthan V N 
35332a05a622SIvan Khoronzhuk 	cpsw->ale = cpsw_ale_create(&ale_params);
35342a05a622SIvan Khoronzhuk 	if (!cpsw->ale) {
3535df828598SMugunthan V N 		dev_err(priv->dev, "error initializing ale engine\n");
3536df828598SMugunthan V N 		ret = -ENODEV;
3537df828598SMugunthan V N 		goto clean_dma_ret;
3538df828598SMugunthan V N 	}
3539df828598SMugunthan V N 
35404a88fb95SGrygorii Strashko 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
35418a2c9a5aSGrygorii Strashko 	if (IS_ERR(cpsw->cpts)) {
35428a2c9a5aSGrygorii Strashko 		ret = PTR_ERR(cpsw->cpts);
35431971ab58SGrygorii Strashko 		goto clean_dma_ret;
35448a2c9a5aSGrygorii Strashko 	}
35458a2c9a5aSGrygorii Strashko 
3546c03abd84SFelipe Balbi 	ndev->irq = platform_get_irq(pdev, 1);
3547df828598SMugunthan V N 	if (ndev->irq < 0) {
3548df828598SMugunthan V N 		dev_err(priv->dev, "error getting irq resource\n");
3549c1e3334fSJulia Lawall 		ret = ndev->irq;
35501971ab58SGrygorii Strashko 		goto clean_dma_ret;
3551df828598SMugunthan V N 	}
3552df828598SMugunthan V N 
3553a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3554070f9c65SKeerthy 
3555070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
3556070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
35579611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
35589611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
35599611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
35609611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
35619611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
35629611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
3563070f9c65SKeerthy 	cpsw_split_res(ndev);
3564070f9c65SKeerthy 
3565070f9c65SKeerthy 	/* register the network device */
3566070f9c65SKeerthy 	SET_NETDEV_DEV(ndev, &pdev->dev);
3567070f9c65SKeerthy 	ret = register_netdev(ndev);
3568070f9c65SKeerthy 	if (ret) {
3569070f9c65SKeerthy 		dev_err(priv->dev, "error registering net device\n");
3570070f9c65SKeerthy 		ret = -ENODEV;
35711971ab58SGrygorii Strashko 		goto clean_dma_ret;
3572070f9c65SKeerthy 	}
3573070f9c65SKeerthy 
3574070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
3575070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
3576070f9c65SKeerthy 		if (ret) {
3577070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3578070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
3579070f9c65SKeerthy 		}
3580070f9c65SKeerthy 	}
3581070f9c65SKeerthy 
3582c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3583c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
3584c03abd84SFelipe Balbi 	 * we will not request them.
3585c03abd84SFelipe Balbi 	 *
3586c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
3587c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
3588c03abd84SFelipe Balbi 	 */
3589c2b32e58SDaniel Mack 
3590c03abd84SFelipe Balbi 	/* RX IRQ */
35915087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 1);
3592c1e3334fSJulia Lawall 	if (irq < 0) {
3593c1e3334fSJulia Lawall 		ret = irq;
35941971ab58SGrygorii Strashko 		goto clean_dma_ret;
3595c1e3334fSJulia Lawall 	}
35965087b915SFelipe Balbi 
3597e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[0] = irq;
3598c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3599dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
36005087b915SFelipe Balbi 	if (ret < 0) {
36015087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
36021971ab58SGrygorii Strashko 		goto clean_dma_ret;
3603df828598SMugunthan V N 	}
3604df828598SMugunthan V N 
3605c03abd84SFelipe Balbi 	/* TX IRQ */
36065087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 2);
3607c1e3334fSJulia Lawall 	if (irq < 0) {
3608c1e3334fSJulia Lawall 		ret = irq;
36091971ab58SGrygorii Strashko 		goto clean_dma_ret;
3610c1e3334fSJulia Lawall 	}
36115087b915SFelipe Balbi 
3612e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[1] = irq;
3613c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3614dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
36155087b915SFelipe Balbi 	if (ret < 0) {
36165087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
36171971ab58SGrygorii Strashko 		goto clean_dma_ret;
36185087b915SFelipe Balbi 	}
3619c2b32e58SDaniel Mack 
362090225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
362190225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
362290225bf0SGrygorii Strashko 		    &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3623d9ba8f9eSMugunthan V N 
3624c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
3625c46ab7e0SJohan Hovold 
3626df828598SMugunthan V N 	return 0;
3627df828598SMugunthan V N 
3628a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
3629a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
3630df828598SMugunthan V N clean_dma_ret:
36312c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3632a4e32b0dSJohan Hovold clean_dt_ret:
3633a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
3634c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
3635aa1a15e2SDaniel Mack clean_runtime_disable_ret:
3636f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
3637df828598SMugunthan V N clean_ndev_ret:
3638d1bd9acfSSebastian Siewior 	free_netdev(priv->ndev);
3639df828598SMugunthan V N 	return ret;
3640df828598SMugunthan V N }
3641df828598SMugunthan V N 
3642663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
3643df828598SMugunthan V N {
3644df828598SMugunthan V N 	struct net_device *ndev = platform_get_drvdata(pdev);
36452a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
36468a0b6dc9SGrygorii Strashko 	int ret;
36478a0b6dc9SGrygorii Strashko 
36488a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
36498a0b6dc9SGrygorii Strashko 	if (ret < 0) {
36508a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
36518a0b6dc9SGrygorii Strashko 		return ret;
36528a0b6dc9SGrygorii Strashko 	}
3653df828598SMugunthan V N 
3654606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3655606f3993SIvan Khoronzhuk 		unregister_netdev(cpsw->slaves[1].ndev);
3656d1bd9acfSSebastian Siewior 	unregister_netdev(ndev);
3657df828598SMugunthan V N 
36588a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
36592c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3660a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
36618a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
36628a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
3663606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3664606f3993SIvan Khoronzhuk 		free_netdev(cpsw->slaves[1].ndev);
3665df828598SMugunthan V N 	free_netdev(ndev);
3666df828598SMugunthan V N 	return 0;
3667df828598SMugunthan V N }
3668df828598SMugunthan V N 
36698963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
3670df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
3671df828598SMugunthan V N {
3672df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3673df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3674606f3993SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3675df828598SMugunthan V N 
3676606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3677618073e3SMugunthan V N 		int i;
3678618073e3SMugunthan V N 
3679606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3680606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3681606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
3682618073e3SMugunthan V N 		}
3683618073e3SMugunthan V N 	} else {
3684df828598SMugunthan V N 		if (netif_running(ndev))
3685df828598SMugunthan V N 			cpsw_ndo_stop(ndev);
3686618073e3SMugunthan V N 	}
36871e7a2e21SDaniel Mack 
3688739683b4SMugunthan V N 	/* Select sleep pin state */
368956e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
3690739683b4SMugunthan V N 
3691df828598SMugunthan V N 	return 0;
3692df828598SMugunthan V N }
3693df828598SMugunthan V N 
3694df828598SMugunthan V N static int cpsw_resume(struct device *dev)
3695df828598SMugunthan V N {
3696df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3697df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3698a60ced99SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3699df828598SMugunthan V N 
3700739683b4SMugunthan V N 	/* Select default pin state */
370156e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
3702739683b4SMugunthan V N 
37034ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
37044ccfd638SGrygorii Strashko 	rtnl_lock();
3705606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3706618073e3SMugunthan V N 		int i;
3707618073e3SMugunthan V N 
3708606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3709606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3710606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
3711618073e3SMugunthan V N 		}
3712618073e3SMugunthan V N 	} else {
3713df828598SMugunthan V N 		if (netif_running(ndev))
3714df828598SMugunthan V N 			cpsw_ndo_open(ndev);
3715618073e3SMugunthan V N 	}
37164ccfd638SGrygorii Strashko 	rtnl_unlock();
37174ccfd638SGrygorii Strashko 
3718df828598SMugunthan V N 	return 0;
3719df828598SMugunthan V N }
37208963a504SGrygorii Strashko #endif
3721df828598SMugunthan V N 
37228963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3723df828598SMugunthan V N 
3724df828598SMugunthan V N static struct platform_driver cpsw_driver = {
3725df828598SMugunthan V N 	.driver = {
3726df828598SMugunthan V N 		.name	 = "cpsw",
3727df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
37281e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
3729df828598SMugunthan V N 	},
3730df828598SMugunthan V N 	.probe = cpsw_probe,
3731663e12e6SBill Pemberton 	.remove = cpsw_remove,
3732df828598SMugunthan V N };
3733df828598SMugunthan V N 
37346fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
3735df828598SMugunthan V N 
3736df828598SMugunthan V N MODULE_LICENSE("GPL");
3737df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3738df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3739df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
3740