xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
1 /*
2  * Texas Instruments Ethernet Switch Driver
3  *
4  * Copyright (C) 2012 Texas Instruments
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/io.h>
18 #include <linux/clk.h>
19 #include <linux/timer.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/irqreturn.h>
23 #include <linux/interrupt.h>
24 #include <linux/if_ether.h>
25 #include <linux/etherdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/net_tstamp.h>
28 #include <linux/phy.h>
29 #include <linux/workqueue.h>
30 #include <linux/delay.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/gpio.h>
33 #include <linux/of.h>
34 #include <linux/of_mdio.h>
35 #include <linux/of_net.h>
36 #include <linux/of_device.h>
37 #include <linux/if_vlan.h>
38 
39 #include <linux/pinctrl/consumer.h>
40 
41 #include "cpsw.h"
42 #include "cpsw_ale.h"
43 #include "cpts.h"
44 #include "davinci_cpdma.h"
45 
46 #define CPSW_DEBUG	(NETIF_MSG_HW		| NETIF_MSG_WOL		| \
47 			 NETIF_MSG_DRV		| NETIF_MSG_LINK	| \
48 			 NETIF_MSG_IFUP		| NETIF_MSG_INTR	| \
49 			 NETIF_MSG_PROBE	| NETIF_MSG_TIMER	| \
50 			 NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	| \
51 			 NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	| \
52 			 NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	| \
53 			 NETIF_MSG_RX_STATUS)
54 
55 #define cpsw_info(priv, type, format, ...)		\
56 do {								\
57 	if (netif_msg_##type(priv) && net_ratelimit())		\
58 		dev_info(priv->dev, format, ## __VA_ARGS__);	\
59 } while (0)
60 
61 #define cpsw_err(priv, type, format, ...)		\
62 do {								\
63 	if (netif_msg_##type(priv) && net_ratelimit())		\
64 		dev_err(priv->dev, format, ## __VA_ARGS__);	\
65 } while (0)
66 
67 #define cpsw_dbg(priv, type, format, ...)		\
68 do {								\
69 	if (netif_msg_##type(priv) && net_ratelimit())		\
70 		dev_dbg(priv->dev, format, ## __VA_ARGS__);	\
71 } while (0)
72 
73 #define cpsw_notice(priv, type, format, ...)		\
74 do {								\
75 	if (netif_msg_##type(priv) && net_ratelimit())		\
76 		dev_notice(priv->dev, format, ## __VA_ARGS__);	\
77 } while (0)
78 
79 #define ALE_ALL_PORTS		0x7
80 
81 #define CPSW_MAJOR_VERSION(reg)		(reg >> 8 & 0x7)
82 #define CPSW_MINOR_VERSION(reg)		(reg & 0xff)
83 #define CPSW_RTL_VERSION(reg)		((reg >> 11) & 0x1f)
84 
85 #define CPSW_VERSION_1		0x19010a
86 #define CPSW_VERSION_2		0x19010c
87 #define CPSW_VERSION_3		0x19010f
88 #define CPSW_VERSION_4		0x190112
89 
90 #define HOST_PORT_NUM		0
91 #define CPSW_ALE_PORTS_NUM	3
92 #define SLIVER_SIZE		0x40
93 
94 #define CPSW1_HOST_PORT_OFFSET	0x028
95 #define CPSW1_SLAVE_OFFSET	0x050
96 #define CPSW1_SLAVE_SIZE	0x040
97 #define CPSW1_CPDMA_OFFSET	0x100
98 #define CPSW1_STATERAM_OFFSET	0x200
99 #define CPSW1_HW_STATS		0x400
100 #define CPSW1_CPTS_OFFSET	0x500
101 #define CPSW1_ALE_OFFSET	0x600
102 #define CPSW1_SLIVER_OFFSET	0x700
103 
104 #define CPSW2_HOST_PORT_OFFSET	0x108
105 #define CPSW2_SLAVE_OFFSET	0x200
106 #define CPSW2_SLAVE_SIZE	0x100
107 #define CPSW2_CPDMA_OFFSET	0x800
108 #define CPSW2_HW_STATS		0x900
109 #define CPSW2_STATERAM_OFFSET	0xa00
110 #define CPSW2_CPTS_OFFSET	0xc00
111 #define CPSW2_ALE_OFFSET	0xd00
112 #define CPSW2_SLIVER_OFFSET	0xd80
113 #define CPSW2_BD_OFFSET		0x2000
114 
115 #define CPDMA_RXTHRESH		0x0c0
116 #define CPDMA_RXFREE		0x0e0
117 #define CPDMA_TXHDP		0x00
118 #define CPDMA_RXHDP		0x20
119 #define CPDMA_TXCP		0x40
120 #define CPDMA_RXCP		0x60
121 
122 #define CPSW_POLL_WEIGHT	64
123 #define CPSW_RX_VLAN_ENCAP_HDR_SIZE		4
124 #define CPSW_MIN_PACKET_SIZE	(VLAN_ETH_ZLEN)
125 #define CPSW_MAX_PACKET_SIZE	(VLAN_ETH_FRAME_LEN +\
126 				 ETH_FCS_LEN +\
127 				 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
128 
129 #define RX_PRIORITY_MAPPING	0x76543210
130 #define TX_PRIORITY_MAPPING	0x33221100
131 #define CPDMA_TX_PRIORITY_MAP	0x01234567
132 
133 #define CPSW_VLAN_AWARE		BIT(1)
134 #define CPSW_RX_VLAN_ENCAP	BIT(2)
135 #define CPSW_ALE_VLAN_AWARE	1
136 
137 #define CPSW_FIFO_NORMAL_MODE		(0 << 16)
138 #define CPSW_FIFO_DUAL_MAC_MODE		(1 << 16)
139 #define CPSW_FIFO_RATE_LIMIT_MODE	(2 << 16)
140 
141 #define CPSW_INTPACEEN		(0x3f << 16)
142 #define CPSW_INTPRESCALE_MASK	(0x7FF << 0)
143 #define CPSW_CMINTMAX_CNT	63
144 #define CPSW_CMINTMIN_CNT	2
145 #define CPSW_CMINTMAX_INTVL	(1000 / CPSW_CMINTMIN_CNT)
146 #define CPSW_CMINTMIN_INTVL	((1000 / CPSW_CMINTMAX_CNT) + 1)
147 
148 #define cpsw_slave_index(cpsw, priv)				\
149 		((cpsw->data.dual_emac) ? priv->emac_port :	\
150 		cpsw->data.active_slave)
151 #define IRQ_NUM			2
152 #define CPSW_MAX_QUEUES		8
153 #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
154 
155 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT	29
156 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK		GENMASK(2, 0)
157 #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT	16
158 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT	8
159 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK	GENMASK(1, 0)
160 enum {
161 	CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
162 	CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
163 	CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
164 	CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
165 };
166 
167 static int debug_level;
168 module_param(debug_level, int, 0);
169 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
170 
171 static int ale_ageout = 10;
172 module_param(ale_ageout, int, 0);
173 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
174 
175 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
176 module_param(rx_packet_max, int, 0);
177 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
178 
179 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
180 module_param(descs_pool_size, int, 0444);
181 MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
182 
183 struct cpsw_wr_regs {
184 	u32	id_ver;
185 	u32	soft_reset;
186 	u32	control;
187 	u32	int_control;
188 	u32	rx_thresh_en;
189 	u32	rx_en;
190 	u32	tx_en;
191 	u32	misc_en;
192 	u32	mem_allign1[8];
193 	u32	rx_thresh_stat;
194 	u32	rx_stat;
195 	u32	tx_stat;
196 	u32	misc_stat;
197 	u32	mem_allign2[8];
198 	u32	rx_imax;
199 	u32	tx_imax;
200 
201 };
202 
203 struct cpsw_ss_regs {
204 	u32	id_ver;
205 	u32	control;
206 	u32	soft_reset;
207 	u32	stat_port_en;
208 	u32	ptype;
209 	u32	soft_idle;
210 	u32	thru_rate;
211 	u32	gap_thresh;
212 	u32	tx_start_wds;
213 	u32	flow_control;
214 	u32	vlan_ltype;
215 	u32	ts_ltype;
216 	u32	dlr_ltype;
217 };
218 
219 /* CPSW_PORT_V1 */
220 #define CPSW1_MAX_BLKS      0x00 /* Maximum FIFO Blocks */
221 #define CPSW1_BLK_CNT       0x04 /* FIFO Block Usage Count (Read Only) */
222 #define CPSW1_TX_IN_CTL     0x08 /* Transmit FIFO Control */
223 #define CPSW1_PORT_VLAN     0x0c /* VLAN Register */
224 #define CPSW1_TX_PRI_MAP    0x10 /* Tx Header Priority to Switch Pri Mapping */
225 #define CPSW1_TS_CTL        0x14 /* Time Sync Control */
226 #define CPSW1_TS_SEQ_LTYPE  0x18 /* Time Sync Sequence ID Offset and Msg Type */
227 #define CPSW1_TS_VLAN       0x1c /* Time Sync VLAN1 and VLAN2 */
228 
229 /* CPSW_PORT_V2 */
230 #define CPSW2_CONTROL       0x00 /* Control Register */
231 #define CPSW2_MAX_BLKS      0x08 /* Maximum FIFO Blocks */
232 #define CPSW2_BLK_CNT       0x0c /* FIFO Block Usage Count (Read Only) */
233 #define CPSW2_TX_IN_CTL     0x10 /* Transmit FIFO Control */
234 #define CPSW2_PORT_VLAN     0x14 /* VLAN Register */
235 #define CPSW2_TX_PRI_MAP    0x18 /* Tx Header Priority to Switch Pri Mapping */
236 #define CPSW2_TS_SEQ_MTYPE  0x1c /* Time Sync Sequence ID Offset and Msg Type */
237 
238 /* CPSW_PORT_V1 and V2 */
239 #define SA_LO               0x20 /* CPGMAC_SL Source Address Low */
240 #define SA_HI               0x24 /* CPGMAC_SL Source Address High */
241 #define SEND_PERCENT        0x28 /* Transmit Queue Send Percentages */
242 
243 /* CPSW_PORT_V2 only */
244 #define RX_DSCP_PRI_MAP0    0x30 /* Rx DSCP Priority to Rx Packet Mapping */
245 #define RX_DSCP_PRI_MAP1    0x34 /* Rx DSCP Priority to Rx Packet Mapping */
246 #define RX_DSCP_PRI_MAP2    0x38 /* Rx DSCP Priority to Rx Packet Mapping */
247 #define RX_DSCP_PRI_MAP3    0x3c /* Rx DSCP Priority to Rx Packet Mapping */
248 #define RX_DSCP_PRI_MAP4    0x40 /* Rx DSCP Priority to Rx Packet Mapping */
249 #define RX_DSCP_PRI_MAP5    0x44 /* Rx DSCP Priority to Rx Packet Mapping */
250 #define RX_DSCP_PRI_MAP6    0x48 /* Rx DSCP Priority to Rx Packet Mapping */
251 #define RX_DSCP_PRI_MAP7    0x4c /* Rx DSCP Priority to Rx Packet Mapping */
252 
253 /* Bit definitions for the CPSW2_CONTROL register */
254 #define PASS_PRI_TAGGED     (1<<24) /* Pass Priority Tagged */
255 #define VLAN_LTYPE2_EN      (1<<21) /* VLAN LTYPE 2 enable */
256 #define VLAN_LTYPE1_EN      (1<<20) /* VLAN LTYPE 1 enable */
257 #define DSCP_PRI_EN         (1<<16) /* DSCP Priority Enable */
258 #define TS_320              (1<<14) /* Time Sync Dest Port 320 enable */
259 #define TS_319              (1<<13) /* Time Sync Dest Port 319 enable */
260 #define TS_132              (1<<12) /* Time Sync Dest IP Addr 132 enable */
261 #define TS_131              (1<<11) /* Time Sync Dest IP Addr 131 enable */
262 #define TS_130              (1<<10) /* Time Sync Dest IP Addr 130 enable */
263 #define TS_129              (1<<9)  /* Time Sync Dest IP Addr 129 enable */
264 #define TS_TTL_NONZERO      (1<<8)  /* Time Sync Time To Live Non-zero enable */
265 #define TS_ANNEX_F_EN       (1<<6)  /* Time Sync Annex F enable */
266 #define TS_ANNEX_D_EN       (1<<4)  /* Time Sync Annex D enable */
267 #define TS_LTYPE2_EN        (1<<3)  /* Time Sync LTYPE 2 enable */
268 #define TS_LTYPE1_EN        (1<<2)  /* Time Sync LTYPE 1 enable */
269 #define TS_TX_EN            (1<<1)  /* Time Sync Transmit Enable */
270 #define TS_RX_EN            (1<<0)  /* Time Sync Receive Enable */
271 
272 #define CTRL_V2_TS_BITS \
273 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
274 	 TS_TTL_NONZERO  | TS_ANNEX_D_EN | TS_LTYPE1_EN)
275 
276 #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
277 #define CTRL_V2_TX_TS_BITS  (CTRL_V2_TS_BITS | TS_TX_EN)
278 #define CTRL_V2_RX_TS_BITS  (CTRL_V2_TS_BITS | TS_RX_EN)
279 
280 
281 #define CTRL_V3_TS_BITS \
282 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
283 	 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
284 	 TS_LTYPE1_EN)
285 
286 #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
287 #define CTRL_V3_TX_TS_BITS  (CTRL_V3_TS_BITS | TS_TX_EN)
288 #define CTRL_V3_RX_TS_BITS  (CTRL_V3_TS_BITS | TS_RX_EN)
289 
290 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
291 #define TS_SEQ_ID_OFFSET_SHIFT   (16)    /* Time Sync Sequence ID Offset */
292 #define TS_SEQ_ID_OFFSET_MASK    (0x3f)
293 #define TS_MSG_TYPE_EN_SHIFT     (0)     /* Time Sync Message Type Enable */
294 #define TS_MSG_TYPE_EN_MASK      (0xffff)
295 
296 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
297 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
298 
299 /* Bit definitions for the CPSW1_TS_CTL register */
300 #define CPSW_V1_TS_RX_EN		BIT(0)
301 #define CPSW_V1_TS_TX_EN		BIT(4)
302 #define CPSW_V1_MSG_TYPE_OFS		16
303 
304 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
305 #define CPSW_V1_SEQ_ID_OFS_SHIFT	16
306 
307 #define CPSW_MAX_BLKS_TX		15
308 #define CPSW_MAX_BLKS_TX_SHIFT		4
309 #define CPSW_MAX_BLKS_RX		5
310 
311 struct cpsw_host_regs {
312 	u32	max_blks;
313 	u32	blk_cnt;
314 	u32	tx_in_ctl;
315 	u32	port_vlan;
316 	u32	tx_pri_map;
317 	u32	cpdma_tx_pri_map;
318 	u32	cpdma_rx_chan_map;
319 };
320 
321 struct cpsw_sliver_regs {
322 	u32	id_ver;
323 	u32	mac_control;
324 	u32	mac_status;
325 	u32	soft_reset;
326 	u32	rx_maxlen;
327 	u32	__reserved_0;
328 	u32	rx_pause;
329 	u32	tx_pause;
330 	u32	__reserved_1;
331 	u32	rx_pri_map;
332 };
333 
334 struct cpsw_hw_stats {
335 	u32	rxgoodframes;
336 	u32	rxbroadcastframes;
337 	u32	rxmulticastframes;
338 	u32	rxpauseframes;
339 	u32	rxcrcerrors;
340 	u32	rxaligncodeerrors;
341 	u32	rxoversizedframes;
342 	u32	rxjabberframes;
343 	u32	rxundersizedframes;
344 	u32	rxfragments;
345 	u32	__pad_0[2];
346 	u32	rxoctets;
347 	u32	txgoodframes;
348 	u32	txbroadcastframes;
349 	u32	txmulticastframes;
350 	u32	txpauseframes;
351 	u32	txdeferredframes;
352 	u32	txcollisionframes;
353 	u32	txsinglecollframes;
354 	u32	txmultcollframes;
355 	u32	txexcessivecollisions;
356 	u32	txlatecollisions;
357 	u32	txunderrun;
358 	u32	txcarriersenseerrors;
359 	u32	txoctets;
360 	u32	octetframes64;
361 	u32	octetframes65t127;
362 	u32	octetframes128t255;
363 	u32	octetframes256t511;
364 	u32	octetframes512t1023;
365 	u32	octetframes1024tup;
366 	u32	netoctets;
367 	u32	rxsofoverruns;
368 	u32	rxmofoverruns;
369 	u32	rxdmaoverruns;
370 };
371 
372 struct cpsw_slave_data {
373 	struct device_node *phy_node;
374 	char		phy_id[MII_BUS_ID_SIZE];
375 	int		phy_if;
376 	u8		mac_addr[ETH_ALEN];
377 	u16		dual_emac_res_vlan;	/* Reserved VLAN for DualEMAC */
378 };
379 
380 struct cpsw_platform_data {
381 	struct cpsw_slave_data	*slave_data;
382 	u32	ss_reg_ofs;	/* Subsystem control register offset */
383 	u32	channels;	/* number of cpdma channels (symmetric) */
384 	u32	slaves;		/* number of slave cpgmac ports */
385 	u32	active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
386 	u32	ale_entries;	/* ale table size */
387 	u32	bd_ram_size;  /*buffer descriptor ram size */
388 	u32	mac_control;	/* Mac control register */
389 	u16	default_vlan;	/* Def VLAN for ALE lookup in VLAN aware mode*/
390 	bool	dual_emac;	/* Enable Dual EMAC mode */
391 };
392 
393 struct cpsw_slave {
394 	void __iomem			*regs;
395 	struct cpsw_sliver_regs __iomem	*sliver;
396 	int				slave_num;
397 	u32				mac_control;
398 	struct cpsw_slave_data		*data;
399 	struct phy_device		*phy;
400 	struct net_device		*ndev;
401 	u32				port_vlan;
402 };
403 
404 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
405 {
406 	return readl_relaxed(slave->regs + offset);
407 }
408 
409 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
410 {
411 	writel_relaxed(val, slave->regs + offset);
412 }
413 
414 struct cpsw_vector {
415 	struct cpdma_chan *ch;
416 	int budget;
417 };
418 
419 struct cpsw_common {
420 	struct device			*dev;
421 	struct cpsw_platform_data	data;
422 	struct napi_struct		napi_rx;
423 	struct napi_struct		napi_tx;
424 	struct cpsw_ss_regs __iomem	*regs;
425 	struct cpsw_wr_regs __iomem	*wr_regs;
426 	u8 __iomem			*hw_stats;
427 	struct cpsw_host_regs __iomem	*host_port_regs;
428 	u32				version;
429 	u32				coal_intvl;
430 	u32				bus_freq_mhz;
431 	int				rx_packet_max;
432 	struct cpsw_slave		*slaves;
433 	struct cpdma_ctlr		*dma;
434 	struct cpsw_vector		txv[CPSW_MAX_QUEUES];
435 	struct cpsw_vector		rxv[CPSW_MAX_QUEUES];
436 	struct cpsw_ale			*ale;
437 	bool				quirk_irq;
438 	bool				rx_irq_disabled;
439 	bool				tx_irq_disabled;
440 	u32 irqs_table[IRQ_NUM];
441 	struct cpts			*cpts;
442 	int				rx_ch_num, tx_ch_num;
443 	int				speed;
444 	int				usage_count;
445 };
446 
447 struct cpsw_priv {
448 	struct net_device		*ndev;
449 	struct device			*dev;
450 	u32				msg_enable;
451 	u8				mac_addr[ETH_ALEN];
452 	bool				rx_pause;
453 	bool				tx_pause;
454 	u32 emac_port;
455 	struct cpsw_common *cpsw;
456 };
457 
458 struct cpsw_stats {
459 	char stat_string[ETH_GSTRING_LEN];
460 	int type;
461 	int sizeof_stat;
462 	int stat_offset;
463 };
464 
465 enum {
466 	CPSW_STATS,
467 	CPDMA_RX_STATS,
468 	CPDMA_TX_STATS,
469 };
470 
471 #define CPSW_STAT(m)		CPSW_STATS,				\
472 				sizeof(((struct cpsw_hw_stats *)0)->m), \
473 				offsetof(struct cpsw_hw_stats, m)
474 #define CPDMA_RX_STAT(m)	CPDMA_RX_STATS,				   \
475 				sizeof(((struct cpdma_chan_stats *)0)->m), \
476 				offsetof(struct cpdma_chan_stats, m)
477 #define CPDMA_TX_STAT(m)	CPDMA_TX_STATS,				   \
478 				sizeof(((struct cpdma_chan_stats *)0)->m), \
479 				offsetof(struct cpdma_chan_stats, m)
480 
481 static const struct cpsw_stats cpsw_gstrings_stats[] = {
482 	{ "Good Rx Frames", CPSW_STAT(rxgoodframes) },
483 	{ "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
484 	{ "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
485 	{ "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
486 	{ "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
487 	{ "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
488 	{ "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
489 	{ "Rx Jabbers", CPSW_STAT(rxjabberframes) },
490 	{ "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
491 	{ "Rx Fragments", CPSW_STAT(rxfragments) },
492 	{ "Rx Octets", CPSW_STAT(rxoctets) },
493 	{ "Good Tx Frames", CPSW_STAT(txgoodframes) },
494 	{ "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
495 	{ "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
496 	{ "Pause Tx Frames", CPSW_STAT(txpauseframes) },
497 	{ "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
498 	{ "Collisions", CPSW_STAT(txcollisionframes) },
499 	{ "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
500 	{ "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
501 	{ "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
502 	{ "Late Collisions", CPSW_STAT(txlatecollisions) },
503 	{ "Tx Underrun", CPSW_STAT(txunderrun) },
504 	{ "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
505 	{ "Tx Octets", CPSW_STAT(txoctets) },
506 	{ "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
507 	{ "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
508 	{ "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
509 	{ "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
510 	{ "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
511 	{ "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
512 	{ "Net Octets", CPSW_STAT(netoctets) },
513 	{ "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
514 	{ "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
515 	{ "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
516 };
517 
518 static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
519 	{ "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
520 	{ "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
521 	{ "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
522 	{ "misqueued", CPDMA_RX_STAT(misqueued) },
523 	{ "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
524 	{ "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
525 	{ "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
526 	{ "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
527 	{ "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
528 	{ "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
529 	{ "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
530 	{ "requeue", CPDMA_RX_STAT(requeue) },
531 	{ "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
532 };
533 
534 #define CPSW_STATS_COMMON_LEN	ARRAY_SIZE(cpsw_gstrings_stats)
535 #define CPSW_STATS_CH_LEN	ARRAY_SIZE(cpsw_gstrings_ch_stats)
536 
537 #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
538 #define napi_to_cpsw(napi)	container_of(napi, struct cpsw_common, napi)
539 #define for_each_slave(priv, func, arg...)				\
540 	do {								\
541 		struct cpsw_slave *slave;				\
542 		struct cpsw_common *cpsw = (priv)->cpsw;		\
543 		int n;							\
544 		if (cpsw->data.dual_emac)				\
545 			(func)((cpsw)->slaves + priv->emac_port, ##arg);\
546 		else							\
547 			for (n = cpsw->data.slaves,			\
548 					slave = cpsw->slaves;		\
549 					n; n--)				\
550 				(func)(slave++, ##arg);			\
551 	} while (0)
552 
553 #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb)		\
554 	do {								\
555 		if (!cpsw->data.dual_emac)				\
556 			break;						\
557 		if (CPDMA_RX_SOURCE_PORT(status) == 1) {		\
558 			ndev = cpsw->slaves[0].ndev;			\
559 			skb->dev = ndev;				\
560 		} else if (CPDMA_RX_SOURCE_PORT(status) == 2) {		\
561 			ndev = cpsw->slaves[1].ndev;			\
562 			skb->dev = ndev;				\
563 		}							\
564 	} while (0)
565 #define cpsw_add_mcast(cpsw, priv, addr)				\
566 	do {								\
567 		if (cpsw->data.dual_emac) {				\
568 			struct cpsw_slave *slave = cpsw->slaves +	\
569 						priv->emac_port;	\
570 			int slave_port = cpsw_get_slave_port(		\
571 						slave->slave_num);	\
572 			cpsw_ale_add_mcast(cpsw->ale, addr,		\
573 				1 << slave_port | ALE_PORT_HOST,	\
574 				ALE_VLAN, slave->port_vlan, 0);		\
575 		} else {						\
576 			cpsw_ale_add_mcast(cpsw->ale, addr,		\
577 				ALE_ALL_PORTS,				\
578 				0, 0, 0);				\
579 		}							\
580 	} while (0)
581 
582 static inline int cpsw_get_slave_port(u32 slave_num)
583 {
584 	return slave_num + 1;
585 }
586 
587 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
588 {
589 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
590 	struct cpsw_ale *ale = cpsw->ale;
591 	int i;
592 
593 	if (cpsw->data.dual_emac) {
594 		bool flag = false;
595 
596 		/* Enabling promiscuous mode for one interface will be
597 		 * common for both the interface as the interface shares
598 		 * the same hardware resource.
599 		 */
600 		for (i = 0; i < cpsw->data.slaves; i++)
601 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
602 				flag = true;
603 
604 		if (!enable && flag) {
605 			enable = true;
606 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
607 		}
608 
609 		if (enable) {
610 			/* Enable Bypass */
611 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
612 
613 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
614 		} else {
615 			/* Disable Bypass */
616 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
617 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
618 		}
619 	} else {
620 		if (enable) {
621 			unsigned long timeout = jiffies + HZ;
622 
623 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
624 			for (i = 0; i <= cpsw->data.slaves; i++) {
625 				cpsw_ale_control_set(ale, i,
626 						     ALE_PORT_NOLEARN, 1);
627 				cpsw_ale_control_set(ale, i,
628 						     ALE_PORT_NO_SA_UPDATE, 1);
629 			}
630 
631 			/* Clear All Untouched entries */
632 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
633 			do {
634 				cpu_relax();
635 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
636 					break;
637 			} while (time_after(timeout, jiffies));
638 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
639 
640 			/* Clear all mcast from ALE */
641 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
642 
643 			/* Flood All Unicast Packets to Host port */
644 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
645 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
646 		} else {
647 			/* Don't Flood All Unicast Packets to Host port */
648 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
649 
650 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
651 			for (i = 0; i <= cpsw->data.slaves; i++) {
652 				cpsw_ale_control_set(ale, i,
653 						     ALE_PORT_NOLEARN, 0);
654 				cpsw_ale_control_set(ale, i,
655 						     ALE_PORT_NO_SA_UPDATE, 0);
656 			}
657 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
658 		}
659 	}
660 }
661 
662 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
663 {
664 	struct cpsw_priv *priv = netdev_priv(ndev);
665 	struct cpsw_common *cpsw = priv->cpsw;
666 	int vid;
667 
668 	if (cpsw->data.dual_emac)
669 		vid = cpsw->slaves[priv->emac_port].port_vlan;
670 	else
671 		vid = cpsw->data.default_vlan;
672 
673 	if (ndev->flags & IFF_PROMISC) {
674 		/* Enable promiscuous mode */
675 		cpsw_set_promiscious(ndev, true);
676 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
677 		return;
678 	} else {
679 		/* Disable promiscuous mode */
680 		cpsw_set_promiscious(ndev, false);
681 	}
682 
683 	/* Restore allmulti on vlans if necessary */
684 	cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
685 
686 	/* Clear all mcast from ALE */
687 	cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
688 
689 	if (!netdev_mc_empty(ndev)) {
690 		struct netdev_hw_addr *ha;
691 
692 		/* program multicast address list into ALE register */
693 		netdev_for_each_mc_addr(ha, ndev) {
694 			cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
695 		}
696 	}
697 }
698 
699 static void cpsw_intr_enable(struct cpsw_common *cpsw)
700 {
701 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
702 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
703 
704 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
705 	return;
706 }
707 
708 static void cpsw_intr_disable(struct cpsw_common *cpsw)
709 {
710 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
711 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
712 
713 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
714 	return;
715 }
716 
717 static void cpsw_tx_handler(void *token, int len, int status)
718 {
719 	struct netdev_queue	*txq;
720 	struct sk_buff		*skb = token;
721 	struct net_device	*ndev = skb->dev;
722 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
723 
724 	/* Check whether the queue is stopped due to stalled tx dma, if the
725 	 * queue is stopped then start the queue as we have free desc for tx
726 	 */
727 	txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
728 	if (unlikely(netif_tx_queue_stopped(txq)))
729 		netif_tx_wake_queue(txq);
730 
731 	cpts_tx_timestamp(cpsw->cpts, skb);
732 	ndev->stats.tx_packets++;
733 	ndev->stats.tx_bytes += len;
734 	dev_kfree_skb_any(skb);
735 }
736 
737 static void cpsw_rx_vlan_encap(struct sk_buff *skb)
738 {
739 	struct cpsw_priv *priv = netdev_priv(skb->dev);
740 	struct cpsw_common *cpsw = priv->cpsw;
741 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
742 	u16 vtag, vid, prio, pkt_type;
743 
744 	/* Remove VLAN header encapsulation word */
745 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
746 
747 	pkt_type = (rx_vlan_encap_hdr >>
748 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
749 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
750 	/* Ignore unknown & Priority-tagged packets*/
751 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
752 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
753 		return;
754 
755 	vid = (rx_vlan_encap_hdr >>
756 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
757 	       VLAN_VID_MASK;
758 	/* Ignore vid 0 and pass packet as is */
759 	if (!vid)
760 		return;
761 	/* Ignore default vlans in dual mac mode */
762 	if (cpsw->data.dual_emac &&
763 	    vid == cpsw->slaves[priv->emac_port].port_vlan)
764 		return;
765 
766 	prio = (rx_vlan_encap_hdr >>
767 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
768 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
769 
770 	vtag = (prio << VLAN_PRIO_SHIFT) | vid;
771 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
772 
773 	/* strip vlan tag for VLAN-tagged packet */
774 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
775 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
776 		skb_pull(skb, VLAN_HLEN);
777 	}
778 }
779 
780 static void cpsw_rx_handler(void *token, int len, int status)
781 {
782 	struct cpdma_chan	*ch;
783 	struct sk_buff		*skb = token;
784 	struct sk_buff		*new_skb;
785 	struct net_device	*ndev = skb->dev;
786 	int			ret = 0;
787 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
788 
789 	cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
790 
791 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
792 		/* In dual emac mode check for all interfaces */
793 		if (cpsw->data.dual_emac && cpsw->usage_count &&
794 		    (status >= 0)) {
795 			/* The packet received is for the interface which
796 			 * is already down and the other interface is up
797 			 * and running, instead of freeing which results
798 			 * in reducing of the number of rx descriptor in
799 			 * DMA engine, requeue skb back to cpdma.
800 			 */
801 			new_skb = skb;
802 			goto requeue;
803 		}
804 
805 		/* the interface is going down, skbs are purged */
806 		dev_kfree_skb_any(skb);
807 		return;
808 	}
809 
810 	new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
811 	if (new_skb) {
812 		skb_copy_queue_mapping(new_skb, skb);
813 		skb_put(skb, len);
814 		if (status & CPDMA_RX_VLAN_ENCAP)
815 			cpsw_rx_vlan_encap(skb);
816 		cpts_rx_timestamp(cpsw->cpts, skb);
817 		skb->protocol = eth_type_trans(skb, ndev);
818 		netif_receive_skb(skb);
819 		ndev->stats.rx_bytes += len;
820 		ndev->stats.rx_packets++;
821 		kmemleak_not_leak(new_skb);
822 	} else {
823 		ndev->stats.rx_dropped++;
824 		new_skb = skb;
825 	}
826 
827 requeue:
828 	if (netif_dormant(ndev)) {
829 		dev_kfree_skb_any(new_skb);
830 		return;
831 	}
832 
833 	ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
834 	ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
835 				skb_tailroom(new_skb), 0);
836 	if (WARN_ON(ret < 0))
837 		dev_kfree_skb_any(new_skb);
838 }
839 
840 static void cpsw_split_res(struct net_device *ndev)
841 {
842 	struct cpsw_priv *priv = netdev_priv(ndev);
843 	u32 consumed_rate = 0, bigest_rate = 0;
844 	struct cpsw_common *cpsw = priv->cpsw;
845 	struct cpsw_vector *txv = cpsw->txv;
846 	int i, ch_weight, rlim_ch_num = 0;
847 	int budget, bigest_rate_ch = 0;
848 	u32 ch_rate, max_rate;
849 	int ch_budget = 0;
850 
851 	for (i = 0; i < cpsw->tx_ch_num; i++) {
852 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
853 		if (!ch_rate)
854 			continue;
855 
856 		rlim_ch_num++;
857 		consumed_rate += ch_rate;
858 	}
859 
860 	if (cpsw->tx_ch_num == rlim_ch_num) {
861 		max_rate = consumed_rate;
862 	} else if (!rlim_ch_num) {
863 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
864 		bigest_rate = 0;
865 		max_rate = consumed_rate;
866 	} else {
867 		max_rate = cpsw->speed * 1000;
868 
869 		/* if max_rate is less then expected due to reduced link speed,
870 		 * split proportionally according next potential max speed
871 		 */
872 		if (max_rate < consumed_rate)
873 			max_rate *= 10;
874 
875 		if (max_rate < consumed_rate)
876 			max_rate *= 10;
877 
878 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
879 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
880 			    (cpsw->tx_ch_num - rlim_ch_num);
881 		bigest_rate = (max_rate - consumed_rate) /
882 			      (cpsw->tx_ch_num - rlim_ch_num);
883 	}
884 
885 	/* split tx weight/budget */
886 	budget = CPSW_POLL_WEIGHT;
887 	for (i = 0; i < cpsw->tx_ch_num; i++) {
888 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
889 		if (ch_rate) {
890 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
891 			if (!txv[i].budget)
892 				txv[i].budget++;
893 			if (ch_rate > bigest_rate) {
894 				bigest_rate_ch = i;
895 				bigest_rate = ch_rate;
896 			}
897 
898 			ch_weight = (ch_rate * 100) / max_rate;
899 			if (!ch_weight)
900 				ch_weight++;
901 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
902 		} else {
903 			txv[i].budget = ch_budget;
904 			if (!bigest_rate_ch)
905 				bigest_rate_ch = i;
906 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
907 		}
908 
909 		budget -= txv[i].budget;
910 	}
911 
912 	if (budget)
913 		txv[bigest_rate_ch].budget += budget;
914 
915 	/* split rx budget */
916 	budget = CPSW_POLL_WEIGHT;
917 	ch_budget = budget / cpsw->rx_ch_num;
918 	for (i = 0; i < cpsw->rx_ch_num; i++) {
919 		cpsw->rxv[i].budget = ch_budget;
920 		budget -= ch_budget;
921 	}
922 
923 	if (budget)
924 		cpsw->rxv[0].budget += budget;
925 }
926 
927 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
928 {
929 	struct cpsw_common *cpsw = dev_id;
930 
931 	writel(0, &cpsw->wr_regs->tx_en);
932 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
933 
934 	if (cpsw->quirk_irq) {
935 		disable_irq_nosync(cpsw->irqs_table[1]);
936 		cpsw->tx_irq_disabled = true;
937 	}
938 
939 	napi_schedule(&cpsw->napi_tx);
940 	return IRQ_HANDLED;
941 }
942 
943 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
944 {
945 	struct cpsw_common *cpsw = dev_id;
946 
947 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
948 	writel(0, &cpsw->wr_regs->rx_en);
949 
950 	if (cpsw->quirk_irq) {
951 		disable_irq_nosync(cpsw->irqs_table[0]);
952 		cpsw->rx_irq_disabled = true;
953 	}
954 
955 	napi_schedule(&cpsw->napi_rx);
956 	return IRQ_HANDLED;
957 }
958 
959 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
960 {
961 	u32			ch_map;
962 	int			num_tx, cur_budget, ch;
963 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
964 	struct cpsw_vector	*txv;
965 
966 	/* process every unprocessed channel */
967 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
968 	for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
969 		if (!(ch_map & 0x01))
970 			continue;
971 
972 		txv = &cpsw->txv[ch];
973 		if (unlikely(txv->budget > budget - num_tx))
974 			cur_budget = budget - num_tx;
975 		else
976 			cur_budget = txv->budget;
977 
978 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
979 		if (num_tx >= budget)
980 			break;
981 	}
982 
983 	if (num_tx < budget) {
984 		napi_complete(napi_tx);
985 		writel(0xff, &cpsw->wr_regs->tx_en);
986 		if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
987 			cpsw->tx_irq_disabled = false;
988 			enable_irq(cpsw->irqs_table[1]);
989 		}
990 	}
991 
992 	return num_tx;
993 }
994 
995 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
996 {
997 	u32			ch_map;
998 	int			num_rx, cur_budget, ch;
999 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
1000 	struct cpsw_vector	*rxv;
1001 
1002 	/* process every unprocessed channel */
1003 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1004 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1005 		if (!(ch_map & 0x01))
1006 			continue;
1007 
1008 		rxv = &cpsw->rxv[ch];
1009 		if (unlikely(rxv->budget > budget - num_rx))
1010 			cur_budget = budget - num_rx;
1011 		else
1012 			cur_budget = rxv->budget;
1013 
1014 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1015 		if (num_rx >= budget)
1016 			break;
1017 	}
1018 
1019 	if (num_rx < budget) {
1020 		napi_complete_done(napi_rx, num_rx);
1021 		writel(0xff, &cpsw->wr_regs->rx_en);
1022 		if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
1023 			cpsw->rx_irq_disabled = false;
1024 			enable_irq(cpsw->irqs_table[0]);
1025 		}
1026 	}
1027 
1028 	return num_rx;
1029 }
1030 
1031 static inline void soft_reset(const char *module, void __iomem *reg)
1032 {
1033 	unsigned long timeout = jiffies + HZ;
1034 
1035 	writel_relaxed(1, reg);
1036 	do {
1037 		cpu_relax();
1038 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1039 
1040 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1041 }
1042 
1043 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1044 			       struct cpsw_priv *priv)
1045 {
1046 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
1047 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1048 }
1049 
1050 static void _cpsw_adjust_link(struct cpsw_slave *slave,
1051 			      struct cpsw_priv *priv, bool *link)
1052 {
1053 	struct phy_device	*phy = slave->phy;
1054 	u32			mac_control = 0;
1055 	u32			slave_port;
1056 	struct cpsw_common *cpsw = priv->cpsw;
1057 
1058 	if (!phy)
1059 		return;
1060 
1061 	slave_port = cpsw_get_slave_port(slave->slave_num);
1062 
1063 	if (phy->link) {
1064 		mac_control = cpsw->data.mac_control;
1065 
1066 		/* enable forwarding */
1067 		cpsw_ale_control_set(cpsw->ale, slave_port,
1068 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1069 
1070 		if (phy->speed == 1000)
1071 			mac_control |= BIT(7);	/* GIGABITEN	*/
1072 		if (phy->duplex)
1073 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
1074 
1075 		/* set speed_in input in case RMII mode is used in 100Mbps */
1076 		if (phy->speed == 100)
1077 			mac_control |= BIT(15);
1078 		/* in band mode only works in 10Mbps RGMII mode */
1079 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1080 			mac_control |= BIT(18); /* In Band mode */
1081 
1082 		if (priv->rx_pause)
1083 			mac_control |= BIT(3);
1084 
1085 		if (priv->tx_pause)
1086 			mac_control |= BIT(4);
1087 
1088 		*link = true;
1089 	} else {
1090 		mac_control = 0;
1091 		/* disable forwarding */
1092 		cpsw_ale_control_set(cpsw->ale, slave_port,
1093 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1094 	}
1095 
1096 	if (mac_control != slave->mac_control) {
1097 		phy_print_status(phy);
1098 		writel_relaxed(mac_control, &slave->sliver->mac_control);
1099 	}
1100 
1101 	slave->mac_control = mac_control;
1102 }
1103 
1104 static int cpsw_get_common_speed(struct cpsw_common *cpsw)
1105 {
1106 	int i, speed;
1107 
1108 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
1109 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
1110 			speed += cpsw->slaves[i].phy->speed;
1111 
1112 	return speed;
1113 }
1114 
1115 static int cpsw_need_resplit(struct cpsw_common *cpsw)
1116 {
1117 	int i, rlim_ch_num;
1118 	int speed, ch_rate;
1119 
1120 	/* re-split resources only in case speed was changed */
1121 	speed = cpsw_get_common_speed(cpsw);
1122 	if (speed == cpsw->speed || !speed)
1123 		return 0;
1124 
1125 	cpsw->speed = speed;
1126 
1127 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
1128 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
1129 		if (!ch_rate)
1130 			break;
1131 
1132 		rlim_ch_num++;
1133 	}
1134 
1135 	/* cases not dependent on speed */
1136 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
1137 		return 0;
1138 
1139 	return 1;
1140 }
1141 
1142 static void cpsw_adjust_link(struct net_device *ndev)
1143 {
1144 	struct cpsw_priv	*priv = netdev_priv(ndev);
1145 	struct cpsw_common	*cpsw = priv->cpsw;
1146 	bool			link = false;
1147 
1148 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1149 
1150 	if (link) {
1151 		if (cpsw_need_resplit(cpsw))
1152 			cpsw_split_res(ndev);
1153 
1154 		netif_carrier_on(ndev);
1155 		if (netif_running(ndev))
1156 			netif_tx_wake_all_queues(ndev);
1157 	} else {
1158 		netif_carrier_off(ndev);
1159 		netif_tx_stop_all_queues(ndev);
1160 	}
1161 }
1162 
1163 static int cpsw_get_coalesce(struct net_device *ndev,
1164 				struct ethtool_coalesce *coal)
1165 {
1166 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1167 
1168 	coal->rx_coalesce_usecs = cpsw->coal_intvl;
1169 	return 0;
1170 }
1171 
1172 static int cpsw_set_coalesce(struct net_device *ndev,
1173 				struct ethtool_coalesce *coal)
1174 {
1175 	struct cpsw_priv *priv = netdev_priv(ndev);
1176 	u32 int_ctrl;
1177 	u32 num_interrupts = 0;
1178 	u32 prescale = 0;
1179 	u32 addnl_dvdr = 1;
1180 	u32 coal_intvl = 0;
1181 	struct cpsw_common *cpsw = priv->cpsw;
1182 
1183 	coal_intvl = coal->rx_coalesce_usecs;
1184 
1185 	int_ctrl =  readl(&cpsw->wr_regs->int_control);
1186 	prescale = cpsw->bus_freq_mhz * 4;
1187 
1188 	if (!coal->rx_coalesce_usecs) {
1189 		int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1190 		goto update_return;
1191 	}
1192 
1193 	if (coal_intvl < CPSW_CMINTMIN_INTVL)
1194 		coal_intvl = CPSW_CMINTMIN_INTVL;
1195 
1196 	if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1197 		/* Interrupt pacer works with 4us Pulse, we can
1198 		 * throttle further by dilating the 4us pulse.
1199 		 */
1200 		addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1201 
1202 		if (addnl_dvdr > 1) {
1203 			prescale *= addnl_dvdr;
1204 			if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1205 				coal_intvl = (CPSW_CMINTMAX_INTVL
1206 						* addnl_dvdr);
1207 		} else {
1208 			addnl_dvdr = 1;
1209 			coal_intvl = CPSW_CMINTMAX_INTVL;
1210 		}
1211 	}
1212 
1213 	num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1214 	writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1215 	writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1216 
1217 	int_ctrl |= CPSW_INTPACEEN;
1218 	int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1219 	int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1220 
1221 update_return:
1222 	writel(int_ctrl, &cpsw->wr_regs->int_control);
1223 
1224 	cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
1225 	cpsw->coal_intvl = coal_intvl;
1226 
1227 	return 0;
1228 }
1229 
1230 static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1231 {
1232 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1233 
1234 	switch (sset) {
1235 	case ETH_SS_STATS:
1236 		return (CPSW_STATS_COMMON_LEN +
1237 		       (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1238 		       CPSW_STATS_CH_LEN);
1239 	default:
1240 		return -EOPNOTSUPP;
1241 	}
1242 }
1243 
1244 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1245 {
1246 	int ch_stats_len;
1247 	int line;
1248 	int i;
1249 
1250 	ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1251 	for (i = 0; i < ch_stats_len; i++) {
1252 		line = i % CPSW_STATS_CH_LEN;
1253 		snprintf(*p, ETH_GSTRING_LEN,
1254 			 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1255 			 i / CPSW_STATS_CH_LEN,
1256 			 cpsw_gstrings_ch_stats[line].stat_string);
1257 		*p += ETH_GSTRING_LEN;
1258 	}
1259 }
1260 
1261 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1262 {
1263 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1264 	u8 *p = data;
1265 	int i;
1266 
1267 	switch (stringset) {
1268 	case ETH_SS_STATS:
1269 		for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1270 			memcpy(p, cpsw_gstrings_stats[i].stat_string,
1271 			       ETH_GSTRING_LEN);
1272 			p += ETH_GSTRING_LEN;
1273 		}
1274 
1275 		cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1276 		cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1277 		break;
1278 	}
1279 }
1280 
1281 static void cpsw_get_ethtool_stats(struct net_device *ndev,
1282 				    struct ethtool_stats *stats, u64 *data)
1283 {
1284 	u8 *p;
1285 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1286 	struct cpdma_chan_stats ch_stats;
1287 	int i, l, ch;
1288 
1289 	/* Collect Davinci CPDMA stats for Rx and Tx Channel */
1290 	for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1291 		data[l] = readl(cpsw->hw_stats +
1292 				cpsw_gstrings_stats[l].stat_offset);
1293 
1294 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1295 		cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1296 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1297 			p = (u8 *)&ch_stats +
1298 				cpsw_gstrings_ch_stats[i].stat_offset;
1299 			data[l] = *(u32 *)p;
1300 		}
1301 	}
1302 
1303 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1304 		cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1305 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1306 			p = (u8 *)&ch_stats +
1307 				cpsw_gstrings_ch_stats[i].stat_offset;
1308 			data[l] = *(u32 *)p;
1309 		}
1310 	}
1311 }
1312 
1313 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1314 					struct sk_buff *skb,
1315 					struct cpdma_chan *txch)
1316 {
1317 	struct cpsw_common *cpsw = priv->cpsw;
1318 
1319 	skb_tx_timestamp(skb);
1320 	return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1321 				 priv->emac_port + cpsw->data.dual_emac);
1322 }
1323 
1324 static inline void cpsw_add_dual_emac_def_ale_entries(
1325 		struct cpsw_priv *priv, struct cpsw_slave *slave,
1326 		u32 slave_port)
1327 {
1328 	struct cpsw_common *cpsw = priv->cpsw;
1329 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1330 
1331 	if (cpsw->version == CPSW_VERSION_1)
1332 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1333 	else
1334 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
1335 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1336 			  port_mask, port_mask, 0);
1337 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1338 			   port_mask, ALE_VLAN, slave->port_vlan, 0);
1339 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1340 			   HOST_PORT_NUM, ALE_VLAN |
1341 			   ALE_SECURE, slave->port_vlan);
1342 }
1343 
1344 static void soft_reset_slave(struct cpsw_slave *slave)
1345 {
1346 	char name[32];
1347 
1348 	snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1349 	soft_reset(name, &slave->sliver->soft_reset);
1350 }
1351 
1352 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1353 {
1354 	u32 slave_port;
1355 	struct phy_device *phy;
1356 	struct cpsw_common *cpsw = priv->cpsw;
1357 
1358 	soft_reset_slave(slave);
1359 
1360 	/* setup priority mapping */
1361 	writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
1362 
1363 	switch (cpsw->version) {
1364 	case CPSW_VERSION_1:
1365 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1366 		/* Increase RX FIFO size to 5 for supporting fullduplex
1367 		 * flow control mode
1368 		 */
1369 		slave_write(slave,
1370 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1371 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
1372 		break;
1373 	case CPSW_VERSION_2:
1374 	case CPSW_VERSION_3:
1375 	case CPSW_VERSION_4:
1376 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1377 		/* Increase RX FIFO size to 5 for supporting fullduplex
1378 		 * flow control mode
1379 		 */
1380 		slave_write(slave,
1381 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1382 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
1383 		break;
1384 	}
1385 
1386 	/* setup max packet size, and mac address */
1387 	writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1388 	cpsw_set_slave_mac(slave, priv);
1389 
1390 	slave->mac_control = 0;	/* no link yet */
1391 
1392 	slave_port = cpsw_get_slave_port(slave->slave_num);
1393 
1394 	if (cpsw->data.dual_emac)
1395 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1396 	else
1397 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1398 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1399 
1400 	if (slave->data->phy_node) {
1401 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
1402 				 &cpsw_adjust_link, 0, slave->data->phy_if);
1403 		if (!phy) {
1404 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1405 				slave->data->phy_node,
1406 				slave->slave_num);
1407 			return;
1408 		}
1409 	} else {
1410 		phy = phy_connect(priv->ndev, slave->data->phy_id,
1411 				 &cpsw_adjust_link, slave->data->phy_if);
1412 		if (IS_ERR(phy)) {
1413 			dev_err(priv->dev,
1414 				"phy \"%s\" not found on slave %d, err %ld\n",
1415 				slave->data->phy_id, slave->slave_num,
1416 				PTR_ERR(phy));
1417 			return;
1418 		}
1419 	}
1420 
1421 	slave->phy = phy;
1422 
1423 	phy_attached_info(slave->phy);
1424 
1425 	phy_start(slave->phy);
1426 
1427 	/* Configure GMII_SEL register */
1428 	cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1429 }
1430 
1431 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1432 {
1433 	struct cpsw_common *cpsw = priv->cpsw;
1434 	const int vlan = cpsw->data.default_vlan;
1435 	u32 reg;
1436 	int i;
1437 	int unreg_mcast_mask;
1438 
1439 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
1440 	       CPSW2_PORT_VLAN;
1441 
1442 	writel(vlan, &cpsw->host_port_regs->port_vlan);
1443 
1444 	for (i = 0; i < cpsw->data.slaves; i++)
1445 		slave_write(cpsw->slaves + i, vlan, reg);
1446 
1447 	if (priv->ndev->flags & IFF_ALLMULTI)
1448 		unreg_mcast_mask = ALE_ALL_PORTS;
1449 	else
1450 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1451 
1452 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
1453 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
1454 			  unreg_mcast_mask);
1455 }
1456 
1457 static void cpsw_init_host_port(struct cpsw_priv *priv)
1458 {
1459 	u32 fifo_mode;
1460 	u32 control_reg;
1461 	struct cpsw_common *cpsw = priv->cpsw;
1462 
1463 	/* soft reset the controller and initialize ale */
1464 	soft_reset("cpsw", &cpsw->regs->soft_reset);
1465 	cpsw_ale_start(cpsw->ale);
1466 
1467 	/* switch to vlan unaware mode */
1468 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
1469 			     CPSW_ALE_VLAN_AWARE);
1470 	control_reg = readl(&cpsw->regs->control);
1471 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
1472 	writel(control_reg, &cpsw->regs->control);
1473 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1474 		     CPSW_FIFO_NORMAL_MODE;
1475 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1476 
1477 	/* setup host port priority mapping */
1478 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
1479 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
1480 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1481 
1482 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1483 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1484 
1485 	if (!cpsw->data.dual_emac) {
1486 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1487 				   0, 0);
1488 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1489 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1490 	}
1491 }
1492 
1493 static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1494 {
1495 	struct cpsw_common *cpsw = priv->cpsw;
1496 	struct sk_buff *skb;
1497 	int ch_buf_num;
1498 	int ch, i, ret;
1499 
1500 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1501 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
1502 		for (i = 0; i < ch_buf_num; i++) {
1503 			skb = __netdev_alloc_skb_ip_align(priv->ndev,
1504 							  cpsw->rx_packet_max,
1505 							  GFP_KERNEL);
1506 			if (!skb) {
1507 				cpsw_err(priv, ifup, "cannot allocate skb\n");
1508 				return -ENOMEM;
1509 			}
1510 
1511 			skb_set_queue_mapping(skb, ch);
1512 			ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1513 						skb->data, skb_tailroom(skb),
1514 						0);
1515 			if (ret < 0) {
1516 				cpsw_err(priv, ifup,
1517 					 "cannot submit skb to channel %d rx, error %d\n",
1518 					 ch, ret);
1519 				kfree_skb(skb);
1520 				return ret;
1521 			}
1522 			kmemleak_not_leak(skb);
1523 		}
1524 
1525 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1526 			  ch, ch_buf_num);
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1533 {
1534 	u32 slave_port;
1535 
1536 	slave_port = cpsw_get_slave_port(slave->slave_num);
1537 
1538 	if (!slave->phy)
1539 		return;
1540 	phy_stop(slave->phy);
1541 	phy_disconnect(slave->phy);
1542 	slave->phy = NULL;
1543 	cpsw_ale_control_set(cpsw->ale, slave_port,
1544 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1545 	soft_reset_slave(slave);
1546 }
1547 
1548 static int cpsw_ndo_open(struct net_device *ndev)
1549 {
1550 	struct cpsw_priv *priv = netdev_priv(ndev);
1551 	struct cpsw_common *cpsw = priv->cpsw;
1552 	int ret;
1553 	u32 reg;
1554 
1555 	ret = pm_runtime_get_sync(cpsw->dev);
1556 	if (ret < 0) {
1557 		pm_runtime_put_noidle(cpsw->dev);
1558 		return ret;
1559 	}
1560 
1561 	netif_carrier_off(ndev);
1562 
1563 	/* Notify the stack of the actual queue counts. */
1564 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1565 	if (ret) {
1566 		dev_err(priv->dev, "cannot set real number of tx queues\n");
1567 		goto err_cleanup;
1568 	}
1569 
1570 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1571 	if (ret) {
1572 		dev_err(priv->dev, "cannot set real number of rx queues\n");
1573 		goto err_cleanup;
1574 	}
1575 
1576 	reg = cpsw->version;
1577 
1578 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1579 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1580 		 CPSW_RTL_VERSION(reg));
1581 
1582 	/* Initialize host and slave ports */
1583 	if (!cpsw->usage_count)
1584 		cpsw_init_host_port(priv);
1585 	for_each_slave(priv, cpsw_slave_open, priv);
1586 
1587 	/* Add default VLAN */
1588 	if (!cpsw->data.dual_emac)
1589 		cpsw_add_default_vlan(priv);
1590 	else
1591 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
1592 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
1593 
1594 	/* initialize shared resources for every ndev */
1595 	if (!cpsw->usage_count) {
1596 		/* disable priority elevation */
1597 		writel_relaxed(0, &cpsw->regs->ptype);
1598 
1599 		/* enable statistics collection only on all ports */
1600 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1601 
1602 		/* Enable internal fifo flow control */
1603 		writel(0x7, &cpsw->regs->flow_control);
1604 
1605 		napi_enable(&cpsw->napi_rx);
1606 		napi_enable(&cpsw->napi_tx);
1607 
1608 		if (cpsw->tx_irq_disabled) {
1609 			cpsw->tx_irq_disabled = false;
1610 			enable_irq(cpsw->irqs_table[1]);
1611 		}
1612 
1613 		if (cpsw->rx_irq_disabled) {
1614 			cpsw->rx_irq_disabled = false;
1615 			enable_irq(cpsw->irqs_table[0]);
1616 		}
1617 
1618 		ret = cpsw_fill_rx_channels(priv);
1619 		if (ret < 0)
1620 			goto err_cleanup;
1621 
1622 		if (cpts_register(cpsw->cpts))
1623 			dev_err(priv->dev, "error registering cpts device\n");
1624 
1625 	}
1626 
1627 	/* Enable Interrupt pacing if configured */
1628 	if (cpsw->coal_intvl != 0) {
1629 		struct ethtool_coalesce coal;
1630 
1631 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
1632 		cpsw_set_coalesce(ndev, &coal);
1633 	}
1634 
1635 	cpdma_ctlr_start(cpsw->dma);
1636 	cpsw_intr_enable(cpsw);
1637 	cpsw->usage_count++;
1638 
1639 	return 0;
1640 
1641 err_cleanup:
1642 	cpdma_ctlr_stop(cpsw->dma);
1643 	for_each_slave(priv, cpsw_slave_stop, cpsw);
1644 	pm_runtime_put_sync(cpsw->dev);
1645 	netif_carrier_off(priv->ndev);
1646 	return ret;
1647 }
1648 
1649 static int cpsw_ndo_stop(struct net_device *ndev)
1650 {
1651 	struct cpsw_priv *priv = netdev_priv(ndev);
1652 	struct cpsw_common *cpsw = priv->cpsw;
1653 
1654 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1655 	netif_tx_stop_all_queues(priv->ndev);
1656 	netif_carrier_off(priv->ndev);
1657 
1658 	if (cpsw->usage_count <= 1) {
1659 		napi_disable(&cpsw->napi_rx);
1660 		napi_disable(&cpsw->napi_tx);
1661 		cpts_unregister(cpsw->cpts);
1662 		cpsw_intr_disable(cpsw);
1663 		cpdma_ctlr_stop(cpsw->dma);
1664 		cpsw_ale_stop(cpsw->ale);
1665 	}
1666 	for_each_slave(priv, cpsw_slave_stop, cpsw);
1667 
1668 	if (cpsw_need_resplit(cpsw))
1669 		cpsw_split_res(ndev);
1670 
1671 	cpsw->usage_count--;
1672 	pm_runtime_put_sync(cpsw->dev);
1673 	return 0;
1674 }
1675 
1676 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1677 				       struct net_device *ndev)
1678 {
1679 	struct cpsw_priv *priv = netdev_priv(ndev);
1680 	struct cpsw_common *cpsw = priv->cpsw;
1681 	struct cpts *cpts = cpsw->cpts;
1682 	struct netdev_queue *txq;
1683 	struct cpdma_chan *txch;
1684 	int ret, q_idx;
1685 
1686 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1687 		cpsw_err(priv, tx_err, "packet pad failed\n");
1688 		ndev->stats.tx_dropped++;
1689 		return NET_XMIT_DROP;
1690 	}
1691 
1692 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
1693 	    cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
1694 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1695 
1696 	q_idx = skb_get_queue_mapping(skb);
1697 	if (q_idx >= cpsw->tx_ch_num)
1698 		q_idx = q_idx % cpsw->tx_ch_num;
1699 
1700 	txch = cpsw->txv[q_idx].ch;
1701 	txq = netdev_get_tx_queue(ndev, q_idx);
1702 	ret = cpsw_tx_packet_submit(priv, skb, txch);
1703 	if (unlikely(ret != 0)) {
1704 		cpsw_err(priv, tx_err, "desc submit failed\n");
1705 		goto fail;
1706 	}
1707 
1708 	/* If there is no more tx desc left free then we need to
1709 	 * tell the kernel to stop sending us tx frames.
1710 	 */
1711 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1712 		netif_tx_stop_queue(txq);
1713 
1714 		/* Barrier, so that stop_queue visible to other cpus */
1715 		smp_mb__after_atomic();
1716 
1717 		if (cpdma_check_free_tx_desc(txch))
1718 			netif_tx_wake_queue(txq);
1719 	}
1720 
1721 	return NETDEV_TX_OK;
1722 fail:
1723 	ndev->stats.tx_dropped++;
1724 	netif_tx_stop_queue(txq);
1725 
1726 	/* Barrier, so that stop_queue visible to other cpus */
1727 	smp_mb__after_atomic();
1728 
1729 	if (cpdma_check_free_tx_desc(txch))
1730 		netif_tx_wake_queue(txq);
1731 
1732 	return NETDEV_TX_BUSY;
1733 }
1734 
1735 #if IS_ENABLED(CONFIG_TI_CPTS)
1736 
1737 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
1738 {
1739 	struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
1740 	u32 ts_en, seq_id;
1741 
1742 	if (!cpts_is_tx_enabled(cpsw->cpts) &&
1743 	    !cpts_is_rx_enabled(cpsw->cpts)) {
1744 		slave_write(slave, 0, CPSW1_TS_CTL);
1745 		return;
1746 	}
1747 
1748 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1749 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1750 
1751 	if (cpts_is_tx_enabled(cpsw->cpts))
1752 		ts_en |= CPSW_V1_TS_TX_EN;
1753 
1754 	if (cpts_is_rx_enabled(cpsw->cpts))
1755 		ts_en |= CPSW_V1_TS_RX_EN;
1756 
1757 	slave_write(slave, ts_en, CPSW1_TS_CTL);
1758 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1759 }
1760 
1761 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1762 {
1763 	struct cpsw_slave *slave;
1764 	struct cpsw_common *cpsw = priv->cpsw;
1765 	u32 ctrl, mtype;
1766 
1767 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1768 
1769 	ctrl = slave_read(slave, CPSW2_CONTROL);
1770 	switch (cpsw->version) {
1771 	case CPSW_VERSION_2:
1772 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
1773 
1774 		if (cpts_is_tx_enabled(cpsw->cpts))
1775 			ctrl |= CTRL_V2_TX_TS_BITS;
1776 
1777 		if (cpts_is_rx_enabled(cpsw->cpts))
1778 			ctrl |= CTRL_V2_RX_TS_BITS;
1779 		break;
1780 	case CPSW_VERSION_3:
1781 	default:
1782 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
1783 
1784 		if (cpts_is_tx_enabled(cpsw->cpts))
1785 			ctrl |= CTRL_V3_TX_TS_BITS;
1786 
1787 		if (cpts_is_rx_enabled(cpsw->cpts))
1788 			ctrl |= CTRL_V3_RX_TS_BITS;
1789 		break;
1790 	}
1791 
1792 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1793 
1794 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1795 	slave_write(slave, ctrl, CPSW2_CONTROL);
1796 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
1797 }
1798 
1799 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1800 {
1801 	struct cpsw_priv *priv = netdev_priv(dev);
1802 	struct hwtstamp_config cfg;
1803 	struct cpsw_common *cpsw = priv->cpsw;
1804 	struct cpts *cpts = cpsw->cpts;
1805 
1806 	if (cpsw->version != CPSW_VERSION_1 &&
1807 	    cpsw->version != CPSW_VERSION_2 &&
1808 	    cpsw->version != CPSW_VERSION_3)
1809 		return -EOPNOTSUPP;
1810 
1811 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1812 		return -EFAULT;
1813 
1814 	/* reserved for future extensions */
1815 	if (cfg.flags)
1816 		return -EINVAL;
1817 
1818 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
1819 		return -ERANGE;
1820 
1821 	switch (cfg.rx_filter) {
1822 	case HWTSTAMP_FILTER_NONE:
1823 		cpts_rx_enable(cpts, 0);
1824 		break;
1825 	case HWTSTAMP_FILTER_ALL:
1826 	case HWTSTAMP_FILTER_NTP_ALL:
1827 		return -ERANGE;
1828 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1829 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1830 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1831 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
1832 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1833 		break;
1834 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1835 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1836 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1837 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1838 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1839 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1840 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1841 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1842 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1843 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
1844 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1845 		break;
1846 	default:
1847 		return -ERANGE;
1848 	}
1849 
1850 	cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
1851 
1852 	switch (cpsw->version) {
1853 	case CPSW_VERSION_1:
1854 		cpsw_hwtstamp_v1(cpsw);
1855 		break;
1856 	case CPSW_VERSION_2:
1857 	case CPSW_VERSION_3:
1858 		cpsw_hwtstamp_v2(priv);
1859 		break;
1860 	default:
1861 		WARN_ON(1);
1862 	}
1863 
1864 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1865 }
1866 
1867 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1868 {
1869 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1870 	struct cpts *cpts = cpsw->cpts;
1871 	struct hwtstamp_config cfg;
1872 
1873 	if (cpsw->version != CPSW_VERSION_1 &&
1874 	    cpsw->version != CPSW_VERSION_2 &&
1875 	    cpsw->version != CPSW_VERSION_3)
1876 		return -EOPNOTSUPP;
1877 
1878 	cfg.flags = 0;
1879 	cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1880 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1881 	cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
1882 			 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
1883 
1884 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1885 }
1886 #else
1887 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1888 {
1889 	return -EOPNOTSUPP;
1890 }
1891 
1892 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1893 {
1894 	return -EOPNOTSUPP;
1895 }
1896 #endif /*CONFIG_TI_CPTS*/
1897 
1898 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1899 {
1900 	struct cpsw_priv *priv = netdev_priv(dev);
1901 	struct cpsw_common *cpsw = priv->cpsw;
1902 	int slave_no = cpsw_slave_index(cpsw, priv);
1903 
1904 	if (!netif_running(dev))
1905 		return -EINVAL;
1906 
1907 	switch (cmd) {
1908 	case SIOCSHWTSTAMP:
1909 		return cpsw_hwtstamp_set(dev, req);
1910 	case SIOCGHWTSTAMP:
1911 		return cpsw_hwtstamp_get(dev, req);
1912 	}
1913 
1914 	if (!cpsw->slaves[slave_no].phy)
1915 		return -EOPNOTSUPP;
1916 	return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
1917 }
1918 
1919 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1920 {
1921 	struct cpsw_priv *priv = netdev_priv(ndev);
1922 	struct cpsw_common *cpsw = priv->cpsw;
1923 	int ch;
1924 
1925 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
1926 	ndev->stats.tx_errors++;
1927 	cpsw_intr_disable(cpsw);
1928 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1929 		cpdma_chan_stop(cpsw->txv[ch].ch);
1930 		cpdma_chan_start(cpsw->txv[ch].ch);
1931 	}
1932 
1933 	cpsw_intr_enable(cpsw);
1934 	netif_trans_update(ndev);
1935 	netif_tx_wake_all_queues(ndev);
1936 }
1937 
1938 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1939 {
1940 	struct cpsw_priv *priv = netdev_priv(ndev);
1941 	struct sockaddr *addr = (struct sockaddr *)p;
1942 	struct cpsw_common *cpsw = priv->cpsw;
1943 	int flags = 0;
1944 	u16 vid = 0;
1945 	int ret;
1946 
1947 	if (!is_valid_ether_addr(addr->sa_data))
1948 		return -EADDRNOTAVAIL;
1949 
1950 	ret = pm_runtime_get_sync(cpsw->dev);
1951 	if (ret < 0) {
1952 		pm_runtime_put_noidle(cpsw->dev);
1953 		return ret;
1954 	}
1955 
1956 	if (cpsw->data.dual_emac) {
1957 		vid = cpsw->slaves[priv->emac_port].port_vlan;
1958 		flags = ALE_VLAN;
1959 	}
1960 
1961 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1962 			   flags, vid);
1963 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1964 			   flags, vid);
1965 
1966 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1967 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1968 	for_each_slave(priv, cpsw_set_slave_mac, priv);
1969 
1970 	pm_runtime_put(cpsw->dev);
1971 
1972 	return 0;
1973 }
1974 
1975 #ifdef CONFIG_NET_POLL_CONTROLLER
1976 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1977 {
1978 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1979 
1980 	cpsw_intr_disable(cpsw);
1981 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1982 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1983 	cpsw_intr_enable(cpsw);
1984 }
1985 #endif
1986 
1987 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1988 				unsigned short vid)
1989 {
1990 	int ret;
1991 	int unreg_mcast_mask = 0;
1992 	u32 port_mask;
1993 	struct cpsw_common *cpsw = priv->cpsw;
1994 
1995 	if (cpsw->data.dual_emac) {
1996 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
1997 
1998 		if (priv->ndev->flags & IFF_ALLMULTI)
1999 			unreg_mcast_mask = port_mask;
2000 	} else {
2001 		port_mask = ALE_ALL_PORTS;
2002 
2003 		if (priv->ndev->flags & IFF_ALLMULTI)
2004 			unreg_mcast_mask = ALE_ALL_PORTS;
2005 		else
2006 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
2007 	}
2008 
2009 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
2010 				unreg_mcast_mask);
2011 	if (ret != 0)
2012 		return ret;
2013 
2014 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
2015 				 HOST_PORT_NUM, ALE_VLAN, vid);
2016 	if (ret != 0)
2017 		goto clean_vid;
2018 
2019 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
2020 				 port_mask, ALE_VLAN, vid, 0);
2021 	if (ret != 0)
2022 		goto clean_vlan_ucast;
2023 	return 0;
2024 
2025 clean_vlan_ucast:
2026 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2027 			   HOST_PORT_NUM, ALE_VLAN, vid);
2028 clean_vid:
2029 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2030 	return ret;
2031 }
2032 
2033 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
2034 				    __be16 proto, u16 vid)
2035 {
2036 	struct cpsw_priv *priv = netdev_priv(ndev);
2037 	struct cpsw_common *cpsw = priv->cpsw;
2038 	int ret;
2039 
2040 	if (vid == cpsw->data.default_vlan)
2041 		return 0;
2042 
2043 	ret = pm_runtime_get_sync(cpsw->dev);
2044 	if (ret < 0) {
2045 		pm_runtime_put_noidle(cpsw->dev);
2046 		return ret;
2047 	}
2048 
2049 	if (cpsw->data.dual_emac) {
2050 		/* In dual EMAC, reserved VLAN id should not be used for
2051 		 * creating VLAN interfaces as this can break the dual
2052 		 * EMAC port separation
2053 		 */
2054 		int i;
2055 
2056 		for (i = 0; i < cpsw->data.slaves; i++) {
2057 			if (vid == cpsw->slaves[i].port_vlan)
2058 				return -EINVAL;
2059 		}
2060 	}
2061 
2062 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2063 	ret = cpsw_add_vlan_ale_entry(priv, vid);
2064 
2065 	pm_runtime_put(cpsw->dev);
2066 	return ret;
2067 }
2068 
2069 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
2070 				     __be16 proto, u16 vid)
2071 {
2072 	struct cpsw_priv *priv = netdev_priv(ndev);
2073 	struct cpsw_common *cpsw = priv->cpsw;
2074 	int ret;
2075 
2076 	if (vid == cpsw->data.default_vlan)
2077 		return 0;
2078 
2079 	ret = pm_runtime_get_sync(cpsw->dev);
2080 	if (ret < 0) {
2081 		pm_runtime_put_noidle(cpsw->dev);
2082 		return ret;
2083 	}
2084 
2085 	if (cpsw->data.dual_emac) {
2086 		int i;
2087 
2088 		for (i = 0; i < cpsw->data.slaves; i++) {
2089 			if (vid == cpsw->slaves[i].port_vlan)
2090 				return -EINVAL;
2091 		}
2092 	}
2093 
2094 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
2095 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2096 	if (ret != 0)
2097 		return ret;
2098 
2099 	ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2100 				 HOST_PORT_NUM, ALE_VLAN, vid);
2101 	if (ret != 0)
2102 		return ret;
2103 
2104 	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
2105 				 0, ALE_VLAN, vid);
2106 	pm_runtime_put(cpsw->dev);
2107 	return ret;
2108 }
2109 
2110 static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
2111 {
2112 	struct cpsw_priv *priv = netdev_priv(ndev);
2113 	struct cpsw_common *cpsw = priv->cpsw;
2114 	struct cpsw_slave *slave;
2115 	u32 min_rate;
2116 	u32 ch_rate;
2117 	int i, ret;
2118 
2119 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
2120 	if (ch_rate == rate)
2121 		return 0;
2122 
2123 	ch_rate = rate * 1000;
2124 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2125 	if ((ch_rate < min_rate && ch_rate)) {
2126 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
2127 			min_rate);
2128 		return -EINVAL;
2129 	}
2130 
2131 	if (rate > cpsw->speed) {
2132 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
2133 		return -EINVAL;
2134 	}
2135 
2136 	ret = pm_runtime_get_sync(cpsw->dev);
2137 	if (ret < 0) {
2138 		pm_runtime_put_noidle(cpsw->dev);
2139 		return ret;
2140 	}
2141 
2142 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
2143 	pm_runtime_put(cpsw->dev);
2144 
2145 	if (ret)
2146 		return ret;
2147 
2148 	/* update rates for slaves tx queues */
2149 	for (i = 0; i < cpsw->data.slaves; i++) {
2150 		slave = &cpsw->slaves[i];
2151 		if (!slave->ndev)
2152 			continue;
2153 
2154 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
2155 	}
2156 
2157 	cpsw_split_res(ndev);
2158 	return ret;
2159 }
2160 
2161 static const struct net_device_ops cpsw_netdev_ops = {
2162 	.ndo_open		= cpsw_ndo_open,
2163 	.ndo_stop		= cpsw_ndo_stop,
2164 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
2165 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
2166 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
2167 	.ndo_validate_addr	= eth_validate_addr,
2168 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
2169 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
2170 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
2171 #ifdef CONFIG_NET_POLL_CONTROLLER
2172 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
2173 #endif
2174 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
2175 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
2176 };
2177 
2178 static int cpsw_get_regs_len(struct net_device *ndev)
2179 {
2180 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2181 
2182 	return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
2183 }
2184 
2185 static void cpsw_get_regs(struct net_device *ndev,
2186 			  struct ethtool_regs *regs, void *p)
2187 {
2188 	u32 *reg = p;
2189 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2190 
2191 	/* update CPSW IP version */
2192 	regs->version = cpsw->version;
2193 
2194 	cpsw_ale_dump(cpsw->ale, reg);
2195 }
2196 
2197 static void cpsw_get_drvinfo(struct net_device *ndev,
2198 			     struct ethtool_drvinfo *info)
2199 {
2200 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2201 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
2202 
2203 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
2204 	strlcpy(info->version, "1.0", sizeof(info->version));
2205 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2206 }
2207 
2208 static u32 cpsw_get_msglevel(struct net_device *ndev)
2209 {
2210 	struct cpsw_priv *priv = netdev_priv(ndev);
2211 	return priv->msg_enable;
2212 }
2213 
2214 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2215 {
2216 	struct cpsw_priv *priv = netdev_priv(ndev);
2217 	priv->msg_enable = value;
2218 }
2219 
2220 #if IS_ENABLED(CONFIG_TI_CPTS)
2221 static int cpsw_get_ts_info(struct net_device *ndev,
2222 			    struct ethtool_ts_info *info)
2223 {
2224 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2225 
2226 	info->so_timestamping =
2227 		SOF_TIMESTAMPING_TX_HARDWARE |
2228 		SOF_TIMESTAMPING_TX_SOFTWARE |
2229 		SOF_TIMESTAMPING_RX_HARDWARE |
2230 		SOF_TIMESTAMPING_RX_SOFTWARE |
2231 		SOF_TIMESTAMPING_SOFTWARE |
2232 		SOF_TIMESTAMPING_RAW_HARDWARE;
2233 	info->phc_index = cpsw->cpts->phc_index;
2234 	info->tx_types =
2235 		(1 << HWTSTAMP_TX_OFF) |
2236 		(1 << HWTSTAMP_TX_ON);
2237 	info->rx_filters =
2238 		(1 << HWTSTAMP_FILTER_NONE) |
2239 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2240 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2241 	return 0;
2242 }
2243 #else
2244 static int cpsw_get_ts_info(struct net_device *ndev,
2245 			    struct ethtool_ts_info *info)
2246 {
2247 	info->so_timestamping =
2248 		SOF_TIMESTAMPING_TX_SOFTWARE |
2249 		SOF_TIMESTAMPING_RX_SOFTWARE |
2250 		SOF_TIMESTAMPING_SOFTWARE;
2251 	info->phc_index = -1;
2252 	info->tx_types = 0;
2253 	info->rx_filters = 0;
2254 	return 0;
2255 }
2256 #endif
2257 
2258 static int cpsw_get_link_ksettings(struct net_device *ndev,
2259 				   struct ethtool_link_ksettings *ecmd)
2260 {
2261 	struct cpsw_priv *priv = netdev_priv(ndev);
2262 	struct cpsw_common *cpsw = priv->cpsw;
2263 	int slave_no = cpsw_slave_index(cpsw, priv);
2264 
2265 	if (!cpsw->slaves[slave_no].phy)
2266 		return -EOPNOTSUPP;
2267 
2268 	phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
2269 	return 0;
2270 }
2271 
2272 static int cpsw_set_link_ksettings(struct net_device *ndev,
2273 				   const struct ethtool_link_ksettings *ecmd)
2274 {
2275 	struct cpsw_priv *priv = netdev_priv(ndev);
2276 	struct cpsw_common *cpsw = priv->cpsw;
2277 	int slave_no = cpsw_slave_index(cpsw, priv);
2278 
2279 	if (cpsw->slaves[slave_no].phy)
2280 		return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2281 						 ecmd);
2282 	else
2283 		return -EOPNOTSUPP;
2284 }
2285 
2286 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2287 {
2288 	struct cpsw_priv *priv = netdev_priv(ndev);
2289 	struct cpsw_common *cpsw = priv->cpsw;
2290 	int slave_no = cpsw_slave_index(cpsw, priv);
2291 
2292 	wol->supported = 0;
2293 	wol->wolopts = 0;
2294 
2295 	if (cpsw->slaves[slave_no].phy)
2296 		phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2297 }
2298 
2299 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2300 {
2301 	struct cpsw_priv *priv = netdev_priv(ndev);
2302 	struct cpsw_common *cpsw = priv->cpsw;
2303 	int slave_no = cpsw_slave_index(cpsw, priv);
2304 
2305 	if (cpsw->slaves[slave_no].phy)
2306 		return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2307 	else
2308 		return -EOPNOTSUPP;
2309 }
2310 
2311 static void cpsw_get_pauseparam(struct net_device *ndev,
2312 				struct ethtool_pauseparam *pause)
2313 {
2314 	struct cpsw_priv *priv = netdev_priv(ndev);
2315 
2316 	pause->autoneg = AUTONEG_DISABLE;
2317 	pause->rx_pause = priv->rx_pause ? true : false;
2318 	pause->tx_pause = priv->tx_pause ? true : false;
2319 }
2320 
2321 static int cpsw_set_pauseparam(struct net_device *ndev,
2322 			       struct ethtool_pauseparam *pause)
2323 {
2324 	struct cpsw_priv *priv = netdev_priv(ndev);
2325 	bool link;
2326 
2327 	priv->rx_pause = pause->rx_pause ? true : false;
2328 	priv->tx_pause = pause->tx_pause ? true : false;
2329 
2330 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
2331 	return 0;
2332 }
2333 
2334 static int cpsw_ethtool_op_begin(struct net_device *ndev)
2335 {
2336 	struct cpsw_priv *priv = netdev_priv(ndev);
2337 	struct cpsw_common *cpsw = priv->cpsw;
2338 	int ret;
2339 
2340 	ret = pm_runtime_get_sync(cpsw->dev);
2341 	if (ret < 0) {
2342 		cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
2343 		pm_runtime_put_noidle(cpsw->dev);
2344 	}
2345 
2346 	return ret;
2347 }
2348 
2349 static void cpsw_ethtool_op_complete(struct net_device *ndev)
2350 {
2351 	struct cpsw_priv *priv = netdev_priv(ndev);
2352 	int ret;
2353 
2354 	ret = pm_runtime_put(priv->cpsw->dev);
2355 	if (ret < 0)
2356 		cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2357 }
2358 
2359 static void cpsw_get_channels(struct net_device *ndev,
2360 			      struct ethtool_channels *ch)
2361 {
2362 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2363 
2364 	ch->max_combined = 0;
2365 	ch->max_rx = CPSW_MAX_QUEUES;
2366 	ch->max_tx = CPSW_MAX_QUEUES;
2367 	ch->max_other = 0;
2368 	ch->other_count = 0;
2369 	ch->rx_count = cpsw->rx_ch_num;
2370 	ch->tx_count = cpsw->tx_ch_num;
2371 	ch->combined_count = 0;
2372 }
2373 
2374 static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2375 				  struct ethtool_channels *ch)
2376 {
2377 	if (ch->combined_count)
2378 		return -EINVAL;
2379 
2380 	/* verify we have at least one channel in each direction */
2381 	if (!ch->rx_count || !ch->tx_count)
2382 		return -EINVAL;
2383 
2384 	if (ch->rx_count > cpsw->data.channels ||
2385 	    ch->tx_count > cpsw->data.channels)
2386 		return -EINVAL;
2387 
2388 	return 0;
2389 }
2390 
2391 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2392 {
2393 	struct cpsw_common *cpsw = priv->cpsw;
2394 	void (*handler)(void *, int, int);
2395 	struct netdev_queue *queue;
2396 	struct cpsw_vector *vec;
2397 	int ret, *ch;
2398 
2399 	if (rx) {
2400 		ch = &cpsw->rx_ch_num;
2401 		vec = cpsw->rxv;
2402 		handler = cpsw_rx_handler;
2403 	} else {
2404 		ch = &cpsw->tx_ch_num;
2405 		vec = cpsw->txv;
2406 		handler = cpsw_tx_handler;
2407 	}
2408 
2409 	while (*ch < ch_num) {
2410 		vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
2411 		queue = netdev_get_tx_queue(priv->ndev, *ch);
2412 		queue->tx_maxrate = 0;
2413 
2414 		if (IS_ERR(vec[*ch].ch))
2415 			return PTR_ERR(vec[*ch].ch);
2416 
2417 		if (!vec[*ch].ch)
2418 			return -EINVAL;
2419 
2420 		cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2421 			  (rx ? "rx" : "tx"));
2422 		(*ch)++;
2423 	}
2424 
2425 	while (*ch > ch_num) {
2426 		(*ch)--;
2427 
2428 		ret = cpdma_chan_destroy(vec[*ch].ch);
2429 		if (ret)
2430 			return ret;
2431 
2432 		cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2433 			  (rx ? "rx" : "tx"));
2434 	}
2435 
2436 	return 0;
2437 }
2438 
2439 static int cpsw_update_channels(struct cpsw_priv *priv,
2440 				struct ethtool_channels *ch)
2441 {
2442 	int ret;
2443 
2444 	ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2445 	if (ret)
2446 		return ret;
2447 
2448 	ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2449 	if (ret)
2450 		return ret;
2451 
2452 	return 0;
2453 }
2454 
2455 static void cpsw_suspend_data_pass(struct net_device *ndev)
2456 {
2457 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2458 	struct cpsw_slave *slave;
2459 	int i;
2460 
2461 	/* Disable NAPI scheduling */
2462 	cpsw_intr_disable(cpsw);
2463 
2464 	/* Stop all transmit queues for every network device.
2465 	 * Disable re-using rx descriptors with dormant_on.
2466 	 */
2467 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2468 		if (!(slave->ndev && netif_running(slave->ndev)))
2469 			continue;
2470 
2471 		netif_tx_stop_all_queues(slave->ndev);
2472 		netif_dormant_on(slave->ndev);
2473 	}
2474 
2475 	/* Handle rest of tx packets and stop cpdma channels */
2476 	cpdma_ctlr_stop(cpsw->dma);
2477 }
2478 
2479 static int cpsw_resume_data_pass(struct net_device *ndev)
2480 {
2481 	struct cpsw_priv *priv = netdev_priv(ndev);
2482 	struct cpsw_common *cpsw = priv->cpsw;
2483 	struct cpsw_slave *slave;
2484 	int i, ret;
2485 
2486 	/* Allow rx packets handling */
2487 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2488 		if (slave->ndev && netif_running(slave->ndev))
2489 			netif_dormant_off(slave->ndev);
2490 
2491 	/* After this receive is started */
2492 	if (cpsw->usage_count) {
2493 		ret = cpsw_fill_rx_channels(priv);
2494 		if (ret)
2495 			return ret;
2496 
2497 		cpdma_ctlr_start(cpsw->dma);
2498 		cpsw_intr_enable(cpsw);
2499 	}
2500 
2501 	/* Resume transmit for every affected interface */
2502 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2503 		if (slave->ndev && netif_running(slave->ndev))
2504 			netif_tx_start_all_queues(slave->ndev);
2505 
2506 	return 0;
2507 }
2508 
2509 static int cpsw_set_channels(struct net_device *ndev,
2510 			     struct ethtool_channels *chs)
2511 {
2512 	struct cpsw_priv *priv = netdev_priv(ndev);
2513 	struct cpsw_common *cpsw = priv->cpsw;
2514 	struct cpsw_slave *slave;
2515 	int i, ret;
2516 
2517 	ret = cpsw_check_ch_settings(cpsw, chs);
2518 	if (ret < 0)
2519 		return ret;
2520 
2521 	cpsw_suspend_data_pass(ndev);
2522 	ret = cpsw_update_channels(priv, chs);
2523 	if (ret)
2524 		goto err;
2525 
2526 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2527 		if (!(slave->ndev && netif_running(slave->ndev)))
2528 			continue;
2529 
2530 		/* Inform stack about new count of queues */
2531 		ret = netif_set_real_num_tx_queues(slave->ndev,
2532 						   cpsw->tx_ch_num);
2533 		if (ret) {
2534 			dev_err(priv->dev, "cannot set real number of tx queues\n");
2535 			goto err;
2536 		}
2537 
2538 		ret = netif_set_real_num_rx_queues(slave->ndev,
2539 						   cpsw->rx_ch_num);
2540 		if (ret) {
2541 			dev_err(priv->dev, "cannot set real number of rx queues\n");
2542 			goto err;
2543 		}
2544 	}
2545 
2546 	if (cpsw->usage_count)
2547 		cpsw_split_res(ndev);
2548 
2549 	ret = cpsw_resume_data_pass(ndev);
2550 	if (!ret)
2551 		return 0;
2552 err:
2553 	dev_err(priv->dev, "cannot update channels number, closing device\n");
2554 	dev_close(ndev);
2555 	return ret;
2556 }
2557 
2558 static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2559 {
2560 	struct cpsw_priv *priv = netdev_priv(ndev);
2561 	struct cpsw_common *cpsw = priv->cpsw;
2562 	int slave_no = cpsw_slave_index(cpsw, priv);
2563 
2564 	if (cpsw->slaves[slave_no].phy)
2565 		return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2566 	else
2567 		return -EOPNOTSUPP;
2568 }
2569 
2570 static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2571 {
2572 	struct cpsw_priv *priv = netdev_priv(ndev);
2573 	struct cpsw_common *cpsw = priv->cpsw;
2574 	int slave_no = cpsw_slave_index(cpsw, priv);
2575 
2576 	if (cpsw->slaves[slave_no].phy)
2577 		return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2578 	else
2579 		return -EOPNOTSUPP;
2580 }
2581 
2582 static int cpsw_nway_reset(struct net_device *ndev)
2583 {
2584 	struct cpsw_priv *priv = netdev_priv(ndev);
2585 	struct cpsw_common *cpsw = priv->cpsw;
2586 	int slave_no = cpsw_slave_index(cpsw, priv);
2587 
2588 	if (cpsw->slaves[slave_no].phy)
2589 		return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2590 	else
2591 		return -EOPNOTSUPP;
2592 }
2593 
2594 static void cpsw_get_ringparam(struct net_device *ndev,
2595 			       struct ethtool_ringparam *ering)
2596 {
2597 	struct cpsw_priv *priv = netdev_priv(ndev);
2598 	struct cpsw_common *cpsw = priv->cpsw;
2599 
2600 	/* not supported */
2601 	ering->tx_max_pending = 0;
2602 	ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
2603 	ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
2604 	ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2605 }
2606 
2607 static int cpsw_set_ringparam(struct net_device *ndev,
2608 			      struct ethtool_ringparam *ering)
2609 {
2610 	struct cpsw_priv *priv = netdev_priv(ndev);
2611 	struct cpsw_common *cpsw = priv->cpsw;
2612 	int ret;
2613 
2614 	/* ignore ering->tx_pending - only rx_pending adjustment is supported */
2615 
2616 	if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
2617 	    ering->rx_pending < CPSW_MAX_QUEUES ||
2618 	    ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
2619 		return -EINVAL;
2620 
2621 	if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
2622 		return 0;
2623 
2624 	cpsw_suspend_data_pass(ndev);
2625 
2626 	cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
2627 
2628 	if (cpsw->usage_count)
2629 		cpdma_chan_split_pool(cpsw->dma);
2630 
2631 	ret = cpsw_resume_data_pass(ndev);
2632 	if (!ret)
2633 		return 0;
2634 
2635 	dev_err(&ndev->dev, "cannot set ring params, closing device\n");
2636 	dev_close(ndev);
2637 	return ret;
2638 }
2639 
2640 static const struct ethtool_ops cpsw_ethtool_ops = {
2641 	.get_drvinfo	= cpsw_get_drvinfo,
2642 	.get_msglevel	= cpsw_get_msglevel,
2643 	.set_msglevel	= cpsw_set_msglevel,
2644 	.get_link	= ethtool_op_get_link,
2645 	.get_ts_info	= cpsw_get_ts_info,
2646 	.get_coalesce	= cpsw_get_coalesce,
2647 	.set_coalesce	= cpsw_set_coalesce,
2648 	.get_sset_count		= cpsw_get_sset_count,
2649 	.get_strings		= cpsw_get_strings,
2650 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
2651 	.get_pauseparam		= cpsw_get_pauseparam,
2652 	.set_pauseparam		= cpsw_set_pauseparam,
2653 	.get_wol	= cpsw_get_wol,
2654 	.set_wol	= cpsw_set_wol,
2655 	.get_regs_len	= cpsw_get_regs_len,
2656 	.get_regs	= cpsw_get_regs,
2657 	.begin		= cpsw_ethtool_op_begin,
2658 	.complete	= cpsw_ethtool_op_complete,
2659 	.get_channels	= cpsw_get_channels,
2660 	.set_channels	= cpsw_set_channels,
2661 	.get_link_ksettings	= cpsw_get_link_ksettings,
2662 	.set_link_ksettings	= cpsw_set_link_ksettings,
2663 	.get_eee	= cpsw_get_eee,
2664 	.set_eee	= cpsw_set_eee,
2665 	.nway_reset	= cpsw_nway_reset,
2666 	.get_ringparam = cpsw_get_ringparam,
2667 	.set_ringparam = cpsw_set_ringparam,
2668 };
2669 
2670 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
2671 			    u32 slave_reg_ofs, u32 sliver_reg_ofs)
2672 {
2673 	void __iomem		*regs = cpsw->regs;
2674 	int			slave_num = slave->slave_num;
2675 	struct cpsw_slave_data	*data = cpsw->data.slave_data + slave_num;
2676 
2677 	slave->data	= data;
2678 	slave->regs	= regs + slave_reg_ofs;
2679 	slave->sliver	= regs + sliver_reg_ofs;
2680 	slave->port_vlan = data->dual_emac_res_vlan;
2681 }
2682 
2683 static int cpsw_probe_dt(struct cpsw_platform_data *data,
2684 			 struct platform_device *pdev)
2685 {
2686 	struct device_node *node = pdev->dev.of_node;
2687 	struct device_node *slave_node;
2688 	int i = 0, ret;
2689 	u32 prop;
2690 
2691 	if (!node)
2692 		return -EINVAL;
2693 
2694 	if (of_property_read_u32(node, "slaves", &prop)) {
2695 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
2696 		return -EINVAL;
2697 	}
2698 	data->slaves = prop;
2699 
2700 	if (of_property_read_u32(node, "active_slave", &prop)) {
2701 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
2702 		return -EINVAL;
2703 	}
2704 	data->active_slave = prop;
2705 
2706 	data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2707 					* sizeof(struct cpsw_slave_data),
2708 					GFP_KERNEL);
2709 	if (!data->slave_data)
2710 		return -ENOMEM;
2711 
2712 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
2713 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
2714 		return -EINVAL;
2715 	}
2716 	data->channels = prop;
2717 
2718 	if (of_property_read_u32(node, "ale_entries", &prop)) {
2719 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
2720 		return -EINVAL;
2721 	}
2722 	data->ale_entries = prop;
2723 
2724 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
2725 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
2726 		return -EINVAL;
2727 	}
2728 	data->bd_ram_size = prop;
2729 
2730 	if (of_property_read_u32(node, "mac_control", &prop)) {
2731 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
2732 		return -EINVAL;
2733 	}
2734 	data->mac_control = prop;
2735 
2736 	if (of_property_read_bool(node, "dual_emac"))
2737 		data->dual_emac = 1;
2738 
2739 	/*
2740 	 * Populate all the child nodes here...
2741 	 */
2742 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2743 	/* We do not want to force this, as in some cases may not have child */
2744 	if (ret)
2745 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
2746 
2747 	for_each_available_child_of_node(node, slave_node) {
2748 		struct cpsw_slave_data *slave_data = data->slave_data + i;
2749 		const void *mac_addr = NULL;
2750 		int lenp;
2751 		const __be32 *parp;
2752 
2753 		/* This is no slave child node, continue */
2754 		if (strcmp(slave_node->name, "slave"))
2755 			continue;
2756 
2757 		slave_data->phy_node = of_parse_phandle(slave_node,
2758 							"phy-handle", 0);
2759 		parp = of_get_property(slave_node, "phy_id", &lenp);
2760 		if (slave_data->phy_node) {
2761 			dev_dbg(&pdev->dev,
2762 				"slave[%d] using phy-handle=\"%pOF\"\n",
2763 				i, slave_data->phy_node);
2764 		} else if (of_phy_is_fixed_link(slave_node)) {
2765 			/* In the case of a fixed PHY, the DT node associated
2766 			 * to the PHY is the Ethernet MAC DT node.
2767 			 */
2768 			ret = of_phy_register_fixed_link(slave_node);
2769 			if (ret) {
2770 				if (ret != -EPROBE_DEFER)
2771 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
2772 				return ret;
2773 			}
2774 			slave_data->phy_node = of_node_get(slave_node);
2775 		} else if (parp) {
2776 			u32 phyid;
2777 			struct device_node *mdio_node;
2778 			struct platform_device *mdio;
2779 
2780 			if (lenp != (sizeof(__be32) * 2)) {
2781 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2782 				goto no_phy_slave;
2783 			}
2784 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2785 			phyid = be32_to_cpup(parp+1);
2786 			mdio = of_find_device_by_node(mdio_node);
2787 			of_node_put(mdio_node);
2788 			if (!mdio) {
2789 				dev_err(&pdev->dev, "Missing mdio platform device\n");
2790 				return -EINVAL;
2791 			}
2792 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2793 				 PHY_ID_FMT, mdio->name, phyid);
2794 			put_device(&mdio->dev);
2795 		} else {
2796 			dev_err(&pdev->dev,
2797 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2798 				i);
2799 			goto no_phy_slave;
2800 		}
2801 		slave_data->phy_if = of_get_phy_mode(slave_node);
2802 		if (slave_data->phy_if < 0) {
2803 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2804 				i);
2805 			return slave_data->phy_if;
2806 		}
2807 
2808 no_phy_slave:
2809 		mac_addr = of_get_mac_address(slave_node);
2810 		if (mac_addr) {
2811 			memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
2812 		} else {
2813 			ret = ti_cm_get_macid(&pdev->dev, i,
2814 					      slave_data->mac_addr);
2815 			if (ret)
2816 				return ret;
2817 		}
2818 		if (data->dual_emac) {
2819 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
2820 						 &prop)) {
2821 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
2822 				slave_data->dual_emac_res_vlan = i+1;
2823 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2824 					slave_data->dual_emac_res_vlan, i);
2825 			} else {
2826 				slave_data->dual_emac_res_vlan = prop;
2827 			}
2828 		}
2829 
2830 		i++;
2831 		if (i == data->slaves)
2832 			break;
2833 	}
2834 
2835 	return 0;
2836 }
2837 
2838 static void cpsw_remove_dt(struct platform_device *pdev)
2839 {
2840 	struct net_device *ndev = platform_get_drvdata(pdev);
2841 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2842 	struct cpsw_platform_data *data = &cpsw->data;
2843 	struct device_node *node = pdev->dev.of_node;
2844 	struct device_node *slave_node;
2845 	int i = 0;
2846 
2847 	for_each_available_child_of_node(node, slave_node) {
2848 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
2849 
2850 		if (strcmp(slave_node->name, "slave"))
2851 			continue;
2852 
2853 		if (of_phy_is_fixed_link(slave_node))
2854 			of_phy_deregister_fixed_link(slave_node);
2855 
2856 		of_node_put(slave_data->phy_node);
2857 
2858 		i++;
2859 		if (i == data->slaves)
2860 			break;
2861 	}
2862 
2863 	of_platform_depopulate(&pdev->dev);
2864 }
2865 
2866 static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
2867 {
2868 	struct cpsw_common		*cpsw = priv->cpsw;
2869 	struct cpsw_platform_data	*data = &cpsw->data;
2870 	struct net_device		*ndev;
2871 	struct cpsw_priv		*priv_sl2;
2872 	int ret = 0;
2873 
2874 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2875 	if (!ndev) {
2876 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
2877 		return -ENOMEM;
2878 	}
2879 
2880 	priv_sl2 = netdev_priv(ndev);
2881 	priv_sl2->cpsw = cpsw;
2882 	priv_sl2->ndev = ndev;
2883 	priv_sl2->dev  = &ndev->dev;
2884 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2885 
2886 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2887 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2888 			ETH_ALEN);
2889 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2890 			 priv_sl2->mac_addr);
2891 	} else {
2892 		random_ether_addr(priv_sl2->mac_addr);
2893 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2894 			 priv_sl2->mac_addr);
2895 	}
2896 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2897 
2898 	priv_sl2->emac_port = 1;
2899 	cpsw->slaves[1].ndev = ndev;
2900 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2901 
2902 	ndev->netdev_ops = &cpsw_netdev_ops;
2903 	ndev->ethtool_ops = &cpsw_ethtool_ops;
2904 
2905 	/* register the network device */
2906 	SET_NETDEV_DEV(ndev, cpsw->dev);
2907 	ret = register_netdev(ndev);
2908 	if (ret) {
2909 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
2910 		free_netdev(ndev);
2911 		ret = -ENODEV;
2912 	}
2913 
2914 	return ret;
2915 }
2916 
2917 #define CPSW_QUIRK_IRQ		BIT(0)
2918 
2919 static const struct platform_device_id cpsw_devtype[] = {
2920 	{
2921 		/* keep it for existing comaptibles */
2922 		.name = "cpsw",
2923 		.driver_data = CPSW_QUIRK_IRQ,
2924 	}, {
2925 		.name = "am335x-cpsw",
2926 		.driver_data = CPSW_QUIRK_IRQ,
2927 	}, {
2928 		.name = "am4372-cpsw",
2929 		.driver_data = 0,
2930 	}, {
2931 		.name = "dra7-cpsw",
2932 		.driver_data = 0,
2933 	}, {
2934 		/* sentinel */
2935 	}
2936 };
2937 MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2938 
2939 enum ti_cpsw_type {
2940 	CPSW = 0,
2941 	AM335X_CPSW,
2942 	AM4372_CPSW,
2943 	DRA7_CPSW,
2944 };
2945 
2946 static const struct of_device_id cpsw_of_mtable[] = {
2947 	{ .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2948 	{ .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2949 	{ .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2950 	{ .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2951 	{ /* sentinel */ },
2952 };
2953 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2954 
2955 static int cpsw_probe(struct platform_device *pdev)
2956 {
2957 	struct clk			*clk;
2958 	struct cpsw_platform_data	*data;
2959 	struct net_device		*ndev;
2960 	struct cpsw_priv		*priv;
2961 	struct cpdma_params		dma_params;
2962 	struct cpsw_ale_params		ale_params;
2963 	void __iomem			*ss_regs;
2964 	void __iomem			*cpts_regs;
2965 	struct resource			*res, *ss_res;
2966 	const struct of_device_id	*of_id;
2967 	struct gpio_descs		*mode;
2968 	u32 slave_offset, sliver_offset, slave_size;
2969 	struct cpsw_common		*cpsw;
2970 	int ret = 0, i;
2971 	int irq;
2972 
2973 	cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
2974 	if (!cpsw)
2975 		return -ENOMEM;
2976 
2977 	cpsw->dev = &pdev->dev;
2978 
2979 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2980 	if (!ndev) {
2981 		dev_err(&pdev->dev, "error allocating net_device\n");
2982 		return -ENOMEM;
2983 	}
2984 
2985 	platform_set_drvdata(pdev, ndev);
2986 	priv = netdev_priv(ndev);
2987 	priv->cpsw = cpsw;
2988 	priv->ndev = ndev;
2989 	priv->dev  = &ndev->dev;
2990 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2991 	cpsw->rx_packet_max = max(rx_packet_max, 128);
2992 
2993 	mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2994 	if (IS_ERR(mode)) {
2995 		ret = PTR_ERR(mode);
2996 		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2997 		goto clean_ndev_ret;
2998 	}
2999 
3000 	/*
3001 	 * This may be required here for child devices.
3002 	 */
3003 	pm_runtime_enable(&pdev->dev);
3004 
3005 	/* Select default pin state */
3006 	pinctrl_pm_select_default_state(&pdev->dev);
3007 
3008 	/* Need to enable clocks with runtime PM api to access module
3009 	 * registers
3010 	 */
3011 	ret = pm_runtime_get_sync(&pdev->dev);
3012 	if (ret < 0) {
3013 		pm_runtime_put_noidle(&pdev->dev);
3014 		goto clean_runtime_disable_ret;
3015 	}
3016 
3017 	ret = cpsw_probe_dt(&cpsw->data, pdev);
3018 	if (ret)
3019 		goto clean_dt_ret;
3020 
3021 	data = &cpsw->data;
3022 	cpsw->rx_ch_num = 1;
3023 	cpsw->tx_ch_num = 1;
3024 
3025 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3026 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
3027 		dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3028 	} else {
3029 		eth_random_addr(priv->mac_addr);
3030 		dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3031 	}
3032 
3033 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3034 
3035 	cpsw->slaves = devm_kzalloc(&pdev->dev,
3036 				    sizeof(struct cpsw_slave) * data->slaves,
3037 				    GFP_KERNEL);
3038 	if (!cpsw->slaves) {
3039 		ret = -ENOMEM;
3040 		goto clean_dt_ret;
3041 	}
3042 	for (i = 0; i < data->slaves; i++)
3043 		cpsw->slaves[i].slave_num = i;
3044 
3045 	cpsw->slaves[0].ndev = ndev;
3046 	priv->emac_port = 0;
3047 
3048 	clk = devm_clk_get(&pdev->dev, "fck");
3049 	if (IS_ERR(clk)) {
3050 		dev_err(priv->dev, "fck is not found\n");
3051 		ret = -ENODEV;
3052 		goto clean_dt_ret;
3053 	}
3054 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3055 
3056 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3057 	ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3058 	if (IS_ERR(ss_regs)) {
3059 		ret = PTR_ERR(ss_regs);
3060 		goto clean_dt_ret;
3061 	}
3062 	cpsw->regs = ss_regs;
3063 
3064 	cpsw->version = readl(&cpsw->regs->id_ver);
3065 
3066 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3067 	cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
3068 	if (IS_ERR(cpsw->wr_regs)) {
3069 		ret = PTR_ERR(cpsw->wr_regs);
3070 		goto clean_dt_ret;
3071 	}
3072 
3073 	memset(&dma_params, 0, sizeof(dma_params));
3074 	memset(&ale_params, 0, sizeof(ale_params));
3075 
3076 	switch (cpsw->version) {
3077 	case CPSW_VERSION_1:
3078 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
3079 		cpts_regs		= ss_regs + CPSW1_CPTS_OFFSET;
3080 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
3081 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
3082 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
3083 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
3084 		slave_offset         = CPSW1_SLAVE_OFFSET;
3085 		slave_size           = CPSW1_SLAVE_SIZE;
3086 		sliver_offset        = CPSW1_SLIVER_OFFSET;
3087 		dma_params.desc_mem_phys = 0;
3088 		break;
3089 	case CPSW_VERSION_2:
3090 	case CPSW_VERSION_3:
3091 	case CPSW_VERSION_4:
3092 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
3093 		cpts_regs		= ss_regs + CPSW2_CPTS_OFFSET;
3094 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
3095 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
3096 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
3097 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
3098 		slave_offset         = CPSW2_SLAVE_OFFSET;
3099 		slave_size           = CPSW2_SLAVE_SIZE;
3100 		sliver_offset        = CPSW2_SLIVER_OFFSET;
3101 		dma_params.desc_mem_phys =
3102 			(u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3103 		break;
3104 	default:
3105 		dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3106 		ret = -ENODEV;
3107 		goto clean_dt_ret;
3108 	}
3109 	for (i = 0; i < cpsw->data.slaves; i++) {
3110 		struct cpsw_slave *slave = &cpsw->slaves[i];
3111 
3112 		cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3113 		slave_offset  += slave_size;
3114 		sliver_offset += SLIVER_SIZE;
3115 	}
3116 
3117 	dma_params.dev		= &pdev->dev;
3118 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
3119 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
3120 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
3121 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
3122 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
3123 
3124 	dma_params.num_chan		= data->channels;
3125 	dma_params.has_soft_reset	= true;
3126 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
3127 	dma_params.desc_mem_size	= data->bd_ram_size;
3128 	dma_params.desc_align		= 16;
3129 	dma_params.has_ext_regs		= true;
3130 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
3131 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
3132 	dma_params.descs_pool_size	= descs_pool_size;
3133 
3134 	cpsw->dma = cpdma_ctlr_create(&dma_params);
3135 	if (!cpsw->dma) {
3136 		dev_err(priv->dev, "error initializing dma\n");
3137 		ret = -ENOMEM;
3138 		goto clean_dt_ret;
3139 	}
3140 
3141 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
3142 	if (IS_ERR(cpsw->txv[0].ch)) {
3143 		dev_err(priv->dev, "error initializing tx dma channel\n");
3144 		ret = PTR_ERR(cpsw->txv[0].ch);
3145 		goto clean_dma_ret;
3146 	}
3147 
3148 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
3149 	if (IS_ERR(cpsw->rxv[0].ch)) {
3150 		dev_err(priv->dev, "error initializing rx dma channel\n");
3151 		ret = PTR_ERR(cpsw->rxv[0].ch);
3152 		goto clean_dma_ret;
3153 	}
3154 
3155 	ale_params.dev			= &pdev->dev;
3156 	ale_params.ale_ageout		= ale_ageout;
3157 	ale_params.ale_entries		= data->ale_entries;
3158 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
3159 
3160 	cpsw->ale = cpsw_ale_create(&ale_params);
3161 	if (!cpsw->ale) {
3162 		dev_err(priv->dev, "error initializing ale engine\n");
3163 		ret = -ENODEV;
3164 		goto clean_dma_ret;
3165 	}
3166 
3167 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
3168 	if (IS_ERR(cpsw->cpts)) {
3169 		ret = PTR_ERR(cpsw->cpts);
3170 		goto clean_dma_ret;
3171 	}
3172 
3173 	ndev->irq = platform_get_irq(pdev, 1);
3174 	if (ndev->irq < 0) {
3175 		dev_err(priv->dev, "error getting irq resource\n");
3176 		ret = ndev->irq;
3177 		goto clean_dma_ret;
3178 	}
3179 
3180 	of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
3181 	if (of_id) {
3182 		pdev->id_entry = of_id->data;
3183 		if (pdev->id_entry->driver_data)
3184 			cpsw->quirk_irq = true;
3185 	}
3186 
3187 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3188 
3189 	ndev->netdev_ops = &cpsw_netdev_ops;
3190 	ndev->ethtool_ops = &cpsw_ethtool_ops;
3191 	netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
3192 	netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
3193 	cpsw_split_res(ndev);
3194 
3195 	/* register the network device */
3196 	SET_NETDEV_DEV(ndev, &pdev->dev);
3197 	ret = register_netdev(ndev);
3198 	if (ret) {
3199 		dev_err(priv->dev, "error registering net device\n");
3200 		ret = -ENODEV;
3201 		goto clean_dma_ret;
3202 	}
3203 
3204 	if (cpsw->data.dual_emac) {
3205 		ret = cpsw_probe_dual_emac(priv);
3206 		if (ret) {
3207 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3208 			goto clean_unregister_netdev_ret;
3209 		}
3210 	}
3211 
3212 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3213 	 * MISC IRQs which are always kept disabled with this driver so
3214 	 * we will not request them.
3215 	 *
3216 	 * If anyone wants to implement support for those, make sure to
3217 	 * first request and append them to irqs_table array.
3218 	 */
3219 
3220 	/* RX IRQ */
3221 	irq = platform_get_irq(pdev, 1);
3222 	if (irq < 0) {
3223 		ret = irq;
3224 		goto clean_dma_ret;
3225 	}
3226 
3227 	cpsw->irqs_table[0] = irq;
3228 	ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3229 			       0, dev_name(&pdev->dev), cpsw);
3230 	if (ret < 0) {
3231 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3232 		goto clean_dma_ret;
3233 	}
3234 
3235 	/* TX IRQ */
3236 	irq = platform_get_irq(pdev, 2);
3237 	if (irq < 0) {
3238 		ret = irq;
3239 		goto clean_dma_ret;
3240 	}
3241 
3242 	cpsw->irqs_table[1] = irq;
3243 	ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3244 			       0, dev_name(&pdev->dev), cpsw);
3245 	if (ret < 0) {
3246 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3247 		goto clean_dma_ret;
3248 	}
3249 
3250 	cpsw_notice(priv, probe,
3251 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
3252 		    &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3253 
3254 	pm_runtime_put(&pdev->dev);
3255 
3256 	return 0;
3257 
3258 clean_unregister_netdev_ret:
3259 	unregister_netdev(ndev);
3260 clean_dma_ret:
3261 	cpdma_ctlr_destroy(cpsw->dma);
3262 clean_dt_ret:
3263 	cpsw_remove_dt(pdev);
3264 	pm_runtime_put_sync(&pdev->dev);
3265 clean_runtime_disable_ret:
3266 	pm_runtime_disable(&pdev->dev);
3267 clean_ndev_ret:
3268 	free_netdev(priv->ndev);
3269 	return ret;
3270 }
3271 
3272 static int cpsw_remove(struct platform_device *pdev)
3273 {
3274 	struct net_device *ndev = platform_get_drvdata(pdev);
3275 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3276 	int ret;
3277 
3278 	ret = pm_runtime_get_sync(&pdev->dev);
3279 	if (ret < 0) {
3280 		pm_runtime_put_noidle(&pdev->dev);
3281 		return ret;
3282 	}
3283 
3284 	if (cpsw->data.dual_emac)
3285 		unregister_netdev(cpsw->slaves[1].ndev);
3286 	unregister_netdev(ndev);
3287 
3288 	cpts_release(cpsw->cpts);
3289 	cpdma_ctlr_destroy(cpsw->dma);
3290 	cpsw_remove_dt(pdev);
3291 	pm_runtime_put_sync(&pdev->dev);
3292 	pm_runtime_disable(&pdev->dev);
3293 	if (cpsw->data.dual_emac)
3294 		free_netdev(cpsw->slaves[1].ndev);
3295 	free_netdev(ndev);
3296 	return 0;
3297 }
3298 
3299 #ifdef CONFIG_PM_SLEEP
3300 static int cpsw_suspend(struct device *dev)
3301 {
3302 	struct platform_device	*pdev = to_platform_device(dev);
3303 	struct net_device	*ndev = platform_get_drvdata(pdev);
3304 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3305 
3306 	if (cpsw->data.dual_emac) {
3307 		int i;
3308 
3309 		for (i = 0; i < cpsw->data.slaves; i++) {
3310 			if (netif_running(cpsw->slaves[i].ndev))
3311 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
3312 		}
3313 	} else {
3314 		if (netif_running(ndev))
3315 			cpsw_ndo_stop(ndev);
3316 	}
3317 
3318 	/* Select sleep pin state */
3319 	pinctrl_pm_select_sleep_state(dev);
3320 
3321 	return 0;
3322 }
3323 
3324 static int cpsw_resume(struct device *dev)
3325 {
3326 	struct platform_device	*pdev = to_platform_device(dev);
3327 	struct net_device	*ndev = platform_get_drvdata(pdev);
3328 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3329 
3330 	/* Select default pin state */
3331 	pinctrl_pm_select_default_state(dev);
3332 
3333 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3334 	rtnl_lock();
3335 	if (cpsw->data.dual_emac) {
3336 		int i;
3337 
3338 		for (i = 0; i < cpsw->data.slaves; i++) {
3339 			if (netif_running(cpsw->slaves[i].ndev))
3340 				cpsw_ndo_open(cpsw->slaves[i].ndev);
3341 		}
3342 	} else {
3343 		if (netif_running(ndev))
3344 			cpsw_ndo_open(ndev);
3345 	}
3346 	rtnl_unlock();
3347 
3348 	return 0;
3349 }
3350 #endif
3351 
3352 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3353 
3354 static struct platform_driver cpsw_driver = {
3355 	.driver = {
3356 		.name	 = "cpsw",
3357 		.pm	 = &cpsw_pm_ops,
3358 		.of_match_table = cpsw_of_mtable,
3359 	},
3360 	.probe = cpsw_probe,
3361 	.remove = cpsw_remove,
3362 };
3363 
3364 module_platform_driver(cpsw_driver);
3365 
3366 MODULE_LICENSE("GPL");
3367 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3368 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3369 MODULE_DESCRIPTION("TI CPSW Ethernet driver");
3370