2 * Texas Instruments Ethernet Switch Driver
4 * Copyright (C) 2012 Texas Instruments
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.
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.
16 #include <linux/kernel.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>
34 #include <linux/of_mdio.h>
35 #include <linux/of_net.h>
36 #include <linux/of_device.h>
37 #include <linux/if_vlan.h>
39 #include <linux/pinctrl/consumer.h>
44 #include "davinci_cpdma.h"
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 | \
55 #define cpsw_info(priv, type, format, ...) \
57 if (netif_msg_##type(priv) && net_ratelimit()) \
58 dev_info(priv->dev, format, ## __VA_ARGS__); \
61 #define cpsw_err(priv, type, format, ...) \
63 if (netif_msg_##type(priv) && net_ratelimit()) \
64 dev_err(priv->dev, format, ## __VA_ARGS__); \
67 #define cpsw_dbg(priv, type, format, ...) \
69 if (netif_msg_##type(priv) && net_ratelimit()) \
70 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
73 #define cpsw_notice(priv, type, format, ...) \
75 if (netif_msg_##type(priv) && net_ratelimit()) \
76 dev_notice(priv->dev, format, ## __VA_ARGS__); \
79 #define ALE_ALL_PORTS 0x7
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)
85 #define CPSW_VERSION_1 0x19010a
86 #define CPSW_VERSION_2 0x19010c
87 #define CPSW_VERSION_3 0x19010f
88 #define CPSW_VERSION_4 0x190112
90 #define HOST_PORT_NUM 0
91 #define SLIVER_SIZE 0x40
93 #define CPSW1_HOST_PORT_OFFSET 0x028
94 #define CPSW1_SLAVE_OFFSET 0x050
95 #define CPSW1_SLAVE_SIZE 0x040
96 #define CPSW1_CPDMA_OFFSET 0x100
97 #define CPSW1_STATERAM_OFFSET 0x200
98 #define CPSW1_HW_STATS 0x400
99 #define CPSW1_CPTS_OFFSET 0x500
100 #define CPSW1_ALE_OFFSET 0x600
101 #define CPSW1_SLIVER_OFFSET 0x700
103 #define CPSW2_HOST_PORT_OFFSET 0x108
104 #define CPSW2_SLAVE_OFFSET 0x200
105 #define CPSW2_SLAVE_SIZE 0x100
106 #define CPSW2_CPDMA_OFFSET 0x800
107 #define CPSW2_HW_STATS 0x900
108 #define CPSW2_STATERAM_OFFSET 0xa00
109 #define CPSW2_CPTS_OFFSET 0xc00
110 #define CPSW2_ALE_OFFSET 0xd00
111 #define CPSW2_SLIVER_OFFSET 0xd80
112 #define CPSW2_BD_OFFSET 0x2000
114 #define CPDMA_RXTHRESH 0x0c0
115 #define CPDMA_RXFREE 0x0e0
116 #define CPDMA_TXHDP 0x00
117 #define CPDMA_RXHDP 0x20
118 #define CPDMA_TXCP 0x40
119 #define CPDMA_RXCP 0x60
121 #define CPSW_POLL_WEIGHT 64
122 #define CPSW_MIN_PACKET_SIZE 60
123 #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
125 #define RX_PRIORITY_MAPPING 0x76543210
126 #define TX_PRIORITY_MAPPING 0x33221100
127 #define CPDMA_TX_PRIORITY_MAP 0x76543210
129 #define CPSW_VLAN_AWARE BIT(1)
130 #define CPSW_ALE_VLAN_AWARE 1
132 #define CPSW_FIFO_NORMAL_MODE (0 << 16)
133 #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
134 #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
136 #define CPSW_INTPACEEN (0x3f << 16)
137 #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
138 #define CPSW_CMINTMAX_CNT 63
139 #define CPSW_CMINTMIN_CNT 2
140 #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
141 #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
143 #define cpsw_slave_index(cpsw, priv) \
144 ((cpsw->data.dual_emac) ? priv->emac_port : \
145 cpsw->data.active_slave)
147 #define CPSW_MAX_QUEUES 8
149 static int debug_level;
150 module_param(debug_level, int, 0);
151 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
153 static int ale_ageout = 10;
154 module_param(ale_ageout, int, 0);
155 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
157 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
158 module_param(rx_packet_max, int, 0);
159 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
161 struct cpsw_wr_regs {
181 struct cpsw_ss_regs {
198 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
199 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
200 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
201 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
202 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
203 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
204 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
205 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
208 #define CPSW2_CONTROL 0x00 /* Control Register */
209 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
210 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
211 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
212 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
213 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
214 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
216 /* CPSW_PORT_V1 and V2 */
217 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
218 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
219 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
221 /* CPSW_PORT_V2 only */
222 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
223 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
224 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
225 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
226 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
227 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
228 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
229 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
231 /* Bit definitions for the CPSW2_CONTROL register */
232 #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
233 #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
234 #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
235 #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
236 #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
237 #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
238 #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
239 #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
240 #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
241 #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
242 #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
243 #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
244 #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
245 #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
246 #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
247 #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
248 #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
250 #define CTRL_V2_TS_BITS \
251 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
252 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
254 #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
255 #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
256 #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
259 #define CTRL_V3_TS_BITS \
260 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
261 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
264 #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
265 #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
266 #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
268 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
269 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
270 #define TS_SEQ_ID_OFFSET_MASK (0x3f)
271 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
272 #define TS_MSG_TYPE_EN_MASK (0xffff)
274 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
275 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
277 /* Bit definitions for the CPSW1_TS_CTL register */
278 #define CPSW_V1_TS_RX_EN BIT(0)
279 #define CPSW_V1_TS_TX_EN BIT(4)
280 #define CPSW_V1_MSG_TYPE_OFS 16
282 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
283 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
285 #define CPSW_MAX_BLKS_TX 15
286 #define CPSW_MAX_BLKS_TX_SHIFT 4
287 #define CPSW_MAX_BLKS_RX 5
289 struct cpsw_host_regs {
295 u32 cpdma_tx_pri_map;
296 u32 cpdma_rx_chan_map;
299 struct cpsw_sliver_regs {
312 struct cpsw_hw_stats {
314 u32 rxbroadcastframes;
315 u32 rxmulticastframes;
318 u32 rxaligncodeerrors;
319 u32 rxoversizedframes;
321 u32 rxundersizedframes;
326 u32 txbroadcastframes;
327 u32 txmulticastframes;
329 u32 txdeferredframes;
330 u32 txcollisionframes;
331 u32 txsinglecollframes;
332 u32 txmultcollframes;
333 u32 txexcessivecollisions;
334 u32 txlatecollisions;
336 u32 txcarriersenseerrors;
339 u32 octetframes65t127;
340 u32 octetframes128t255;
341 u32 octetframes256t511;
342 u32 octetframes512t1023;
343 u32 octetframes1024tup;
352 struct cpsw_sliver_regs __iomem *sliver;
355 struct cpsw_slave_data *data;
356 struct phy_device *phy;
357 struct net_device *ndev;
362 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
364 return __raw_readl(slave->regs + offset);
367 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
369 __raw_writel(val, slave->regs + offset);
374 struct cpsw_platform_data data;
375 struct napi_struct napi_rx;
376 struct napi_struct napi_tx;
377 struct cpsw_ss_regs __iomem *regs;
378 struct cpsw_wr_regs __iomem *wr_regs;
379 u8 __iomem *hw_stats;
380 struct cpsw_host_regs __iomem *host_port_regs;
385 struct cpsw_slave *slaves;
386 struct cpdma_ctlr *dma;
387 struct cpdma_chan *txch[CPSW_MAX_QUEUES];
388 struct cpdma_chan *rxch[CPSW_MAX_QUEUES];
389 struct cpsw_ale *ale;
391 bool rx_irq_disabled;
392 bool tx_irq_disabled;
393 u32 irqs_table[IRQ_NUM];
395 int rx_ch_num, tx_ch_num;
399 struct net_device *ndev;
402 u8 mac_addr[ETH_ALEN];
406 struct cpsw_common *cpsw;
410 char stat_string[ETH_GSTRING_LEN];
422 #define CPSW_STAT(m) CPSW_STATS, \
423 sizeof(((struct cpsw_hw_stats *)0)->m), \
424 offsetof(struct cpsw_hw_stats, m)
425 #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
426 sizeof(((struct cpdma_chan_stats *)0)->m), \
427 offsetof(struct cpdma_chan_stats, m)
428 #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
429 sizeof(((struct cpdma_chan_stats *)0)->m), \
430 offsetof(struct cpdma_chan_stats, m)
432 static const struct cpsw_stats cpsw_gstrings_stats[] = {
433 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
434 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
435 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
436 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
437 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
438 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
439 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
440 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
441 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
442 { "Rx Fragments", CPSW_STAT(rxfragments) },
443 { "Rx Octets", CPSW_STAT(rxoctets) },
444 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
445 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
446 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
447 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
448 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
449 { "Collisions", CPSW_STAT(txcollisionframes) },
450 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
451 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
452 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
453 { "Late Collisions", CPSW_STAT(txlatecollisions) },
454 { "Tx Underrun", CPSW_STAT(txunderrun) },
455 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
456 { "Tx Octets", CPSW_STAT(txoctets) },
457 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
458 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
459 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
460 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
461 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
462 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
463 { "Net Octets", CPSW_STAT(netoctets) },
464 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
465 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
466 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
469 static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
470 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
471 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
472 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
473 { "misqueued", CPDMA_RX_STAT(misqueued) },
474 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
475 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
476 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
477 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
478 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
479 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
480 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
481 { "requeue", CPDMA_RX_STAT(requeue) },
482 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
485 #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
486 #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
488 #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
489 #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
490 #define for_each_slave(priv, func, arg...) \
492 struct cpsw_slave *slave; \
493 struct cpsw_common *cpsw = (priv)->cpsw; \
495 if (cpsw->data.dual_emac) \
496 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
498 for (n = cpsw->data.slaves, \
499 slave = cpsw->slaves; \
501 (func)(slave++, ##arg); \
504 #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
506 if (!cpsw->data.dual_emac) \
508 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
509 ndev = cpsw->slaves[0].ndev; \
511 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
512 ndev = cpsw->slaves[1].ndev; \
516 #define cpsw_add_mcast(cpsw, priv, addr) \
518 if (cpsw->data.dual_emac) { \
519 struct cpsw_slave *slave = cpsw->slaves + \
521 int slave_port = cpsw_get_slave_port( \
523 cpsw_ale_add_mcast(cpsw->ale, addr, \
524 1 << slave_port | ALE_PORT_HOST, \
525 ALE_VLAN, slave->port_vlan, 0); \
527 cpsw_ale_add_mcast(cpsw->ale, addr, \
533 static inline int cpsw_get_slave_port(u32 slave_num)
535 return slave_num + 1;
538 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
540 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
541 struct cpsw_ale *ale = cpsw->ale;
544 if (cpsw->data.dual_emac) {
547 /* Enabling promiscuous mode for one interface will be
548 * common for both the interface as the interface shares
549 * the same hardware resource.
551 for (i = 0; i < cpsw->data.slaves; i++)
552 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
555 if (!enable && flag) {
557 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
562 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
564 dev_dbg(&ndev->dev, "promiscuity enabled\n");
567 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
568 dev_dbg(&ndev->dev, "promiscuity disabled\n");
572 unsigned long timeout = jiffies + HZ;
574 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
575 for (i = 0; i <= cpsw->data.slaves; i++) {
576 cpsw_ale_control_set(ale, i,
577 ALE_PORT_NOLEARN, 1);
578 cpsw_ale_control_set(ale, i,
579 ALE_PORT_NO_SA_UPDATE, 1);
582 /* Clear All Untouched entries */
583 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
586 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
588 } while (time_after(timeout, jiffies));
589 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
591 /* Clear all mcast from ALE */
592 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
594 /* Flood All Unicast Packets to Host port */
595 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
596 dev_dbg(&ndev->dev, "promiscuity enabled\n");
598 /* Don't Flood All Unicast Packets to Host port */
599 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
601 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
602 for (i = 0; i <= cpsw->data.slaves; i++) {
603 cpsw_ale_control_set(ale, i,
604 ALE_PORT_NOLEARN, 0);
605 cpsw_ale_control_set(ale, i,
606 ALE_PORT_NO_SA_UPDATE, 0);
608 dev_dbg(&ndev->dev, "promiscuity disabled\n");
613 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
615 struct cpsw_priv *priv = netdev_priv(ndev);
616 struct cpsw_common *cpsw = priv->cpsw;
619 if (cpsw->data.dual_emac)
620 vid = cpsw->slaves[priv->emac_port].port_vlan;
622 vid = cpsw->data.default_vlan;
624 if (ndev->flags & IFF_PROMISC) {
625 /* Enable promiscuous mode */
626 cpsw_set_promiscious(ndev, true);
627 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
630 /* Disable promiscuous mode */
631 cpsw_set_promiscious(ndev, false);
634 /* Restore allmulti on vlans if necessary */
635 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
637 /* Clear all mcast from ALE */
638 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
640 if (!netdev_mc_empty(ndev)) {
641 struct netdev_hw_addr *ha;
643 /* program multicast address list into ALE register */
644 netdev_for_each_mc_addr(ha, ndev) {
645 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
650 static void cpsw_intr_enable(struct cpsw_common *cpsw)
652 __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
653 __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
655 cpdma_ctlr_int_ctrl(cpsw->dma, true);
659 static void cpsw_intr_disable(struct cpsw_common *cpsw)
661 __raw_writel(0, &cpsw->wr_regs->tx_en);
662 __raw_writel(0, &cpsw->wr_regs->rx_en);
664 cpdma_ctlr_int_ctrl(cpsw->dma, false);
668 static void cpsw_tx_handler(void *token, int len, int status)
670 struct netdev_queue *txq;
671 struct sk_buff *skb = token;
672 struct net_device *ndev = skb->dev;
673 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
675 /* Check whether the queue is stopped due to stalled tx dma, if the
676 * queue is stopped then start the queue as we have free desc for tx
678 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
679 if (unlikely(netif_tx_queue_stopped(txq)))
680 netif_tx_wake_queue(txq);
682 cpts_tx_timestamp(cpsw->cpts, skb);
683 ndev->stats.tx_packets++;
684 ndev->stats.tx_bytes += len;
685 dev_kfree_skb_any(skb);
688 static void cpsw_rx_handler(void *token, int len, int status)
690 struct cpdma_chan *ch;
691 struct sk_buff *skb = token;
692 struct sk_buff *new_skb;
693 struct net_device *ndev = skb->dev;
695 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
697 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
699 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
700 bool ndev_status = false;
701 struct cpsw_slave *slave = cpsw->slaves;
704 if (cpsw->data.dual_emac) {
705 /* In dual emac mode check for all interfaces */
706 for (n = cpsw->data.slaves; n; n--, slave++)
707 if (netif_running(slave->ndev))
711 if (ndev_status && (status >= 0)) {
712 /* The packet received is for the interface which
713 * is already down and the other interface is up
714 * and running, instead of freeing which results
715 * in reducing of the number of rx descriptor in
716 * DMA engine, requeue skb back to cpdma.
722 /* the interface is going down, skbs are purged */
723 dev_kfree_skb_any(skb);
727 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
729 skb_copy_queue_mapping(new_skb, skb);
731 cpts_rx_timestamp(cpsw->cpts, skb);
732 skb->protocol = eth_type_trans(skb, ndev);
733 netif_receive_skb(skb);
734 ndev->stats.rx_bytes += len;
735 ndev->stats.rx_packets++;
736 kmemleak_not_leak(new_skb);
738 ndev->stats.rx_dropped++;
743 if (netif_dormant(ndev)) {
744 dev_kfree_skb_any(new_skb);
748 ch = cpsw->rxch[skb_get_queue_mapping(new_skb)];
749 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
750 skb_tailroom(new_skb), 0);
751 if (WARN_ON(ret < 0))
752 dev_kfree_skb_any(new_skb);
755 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
757 struct cpsw_common *cpsw = dev_id;
759 writel(0, &cpsw->wr_regs->tx_en);
760 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
762 if (cpsw->quirk_irq) {
763 disable_irq_nosync(cpsw->irqs_table[1]);
764 cpsw->tx_irq_disabled = true;
767 napi_schedule(&cpsw->napi_tx);
771 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
773 struct cpsw_common *cpsw = dev_id;
775 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
776 writel(0, &cpsw->wr_regs->rx_en);
778 if (cpsw->quirk_irq) {
779 disable_irq_nosync(cpsw->irqs_table[0]);
780 cpsw->rx_irq_disabled = true;
783 napi_schedule(&cpsw->napi_rx);
787 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
791 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
793 /* process every unprocessed channel */
794 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
795 for (ch = 0, num_tx = 0; num_tx < budget; ch_map >>= 1, ch++) {
797 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
804 if (!(ch_map & 0x01))
807 num_tx += cpdma_chan_process(cpsw->txch[ch], budget - num_tx);
810 if (num_tx < budget) {
811 napi_complete(napi_tx);
812 writel(0xff, &cpsw->wr_regs->tx_en);
813 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
814 cpsw->tx_irq_disabled = false;
815 enable_irq(cpsw->irqs_table[1]);
822 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
826 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
828 /* process every unprocessed channel */
829 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
830 for (ch = 0, num_rx = 0; num_rx < budget; ch_map >>= 1, ch++) {
832 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
839 if (!(ch_map & 0x01))
842 num_rx += cpdma_chan_process(cpsw->rxch[ch], budget - num_rx);
845 if (num_rx < budget) {
846 napi_complete(napi_rx);
847 writel(0xff, &cpsw->wr_regs->rx_en);
848 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
849 cpsw->rx_irq_disabled = false;
850 enable_irq(cpsw->irqs_table[0]);
857 static inline void soft_reset(const char *module, void __iomem *reg)
859 unsigned long timeout = jiffies + HZ;
861 __raw_writel(1, reg);
864 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
866 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
869 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
870 ((mac)[2] << 16) | ((mac)[3] << 24))
871 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
873 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
874 struct cpsw_priv *priv)
876 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
877 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
880 static void _cpsw_adjust_link(struct cpsw_slave *slave,
881 struct cpsw_priv *priv, bool *link)
883 struct phy_device *phy = slave->phy;
886 struct cpsw_common *cpsw = priv->cpsw;
891 slave_port = cpsw_get_slave_port(slave->slave_num);
894 mac_control = cpsw->data.mac_control;
896 /* enable forwarding */
897 cpsw_ale_control_set(cpsw->ale, slave_port,
898 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
900 if (phy->speed == 1000)
901 mac_control |= BIT(7); /* GIGABITEN */
903 mac_control |= BIT(0); /* FULLDUPLEXEN */
905 /* set speed_in input in case RMII mode is used in 100Mbps */
906 if (phy->speed == 100)
907 mac_control |= BIT(15);
908 /* in band mode only works in 10Mbps RGMII mode */
909 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
910 mac_control |= BIT(18); /* In Band mode */
913 mac_control |= BIT(3);
916 mac_control |= BIT(4);
921 /* disable forwarding */
922 cpsw_ale_control_set(cpsw->ale, slave_port,
923 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
926 if (mac_control != slave->mac_control) {
927 phy_print_status(phy);
928 __raw_writel(mac_control, &slave->sliver->mac_control);
931 slave->mac_control = mac_control;
934 static void cpsw_adjust_link(struct net_device *ndev)
936 struct cpsw_priv *priv = netdev_priv(ndev);
939 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
942 netif_carrier_on(ndev);
943 if (netif_running(ndev))
944 netif_tx_wake_all_queues(ndev);
946 netif_carrier_off(ndev);
947 netif_tx_stop_all_queues(ndev);
951 static int cpsw_get_coalesce(struct net_device *ndev,
952 struct ethtool_coalesce *coal)
954 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
956 coal->rx_coalesce_usecs = cpsw->coal_intvl;
960 static int cpsw_set_coalesce(struct net_device *ndev,
961 struct ethtool_coalesce *coal)
963 struct cpsw_priv *priv = netdev_priv(ndev);
965 u32 num_interrupts = 0;
969 struct cpsw_common *cpsw = priv->cpsw;
971 coal_intvl = coal->rx_coalesce_usecs;
973 int_ctrl = readl(&cpsw->wr_regs->int_control);
974 prescale = cpsw->bus_freq_mhz * 4;
976 if (!coal->rx_coalesce_usecs) {
977 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
981 if (coal_intvl < CPSW_CMINTMIN_INTVL)
982 coal_intvl = CPSW_CMINTMIN_INTVL;
984 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
985 /* Interrupt pacer works with 4us Pulse, we can
986 * throttle further by dilating the 4us pulse.
988 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
990 if (addnl_dvdr > 1) {
991 prescale *= addnl_dvdr;
992 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
993 coal_intvl = (CPSW_CMINTMAX_INTVL
997 coal_intvl = CPSW_CMINTMAX_INTVL;
1001 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1002 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1003 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1005 int_ctrl |= CPSW_INTPACEEN;
1006 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1007 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1010 writel(int_ctrl, &cpsw->wr_regs->int_control);
1012 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
1013 cpsw->coal_intvl = coal_intvl;
1018 static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1020 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1024 return (CPSW_STATS_COMMON_LEN +
1025 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1032 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1038 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1039 for (i = 0; i < ch_stats_len; i++) {
1040 line = i % CPSW_STATS_CH_LEN;
1041 snprintf(*p, ETH_GSTRING_LEN,
1042 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1043 i / CPSW_STATS_CH_LEN,
1044 cpsw_gstrings_ch_stats[line].stat_string);
1045 *p += ETH_GSTRING_LEN;
1049 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1051 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1055 switch (stringset) {
1057 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1058 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1060 p += ETH_GSTRING_LEN;
1063 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1064 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1069 static void cpsw_get_ethtool_stats(struct net_device *ndev,
1070 struct ethtool_stats *stats, u64 *data)
1073 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1074 struct cpdma_chan_stats ch_stats;
1077 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
1078 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1079 data[l] = readl(cpsw->hw_stats +
1080 cpsw_gstrings_stats[l].stat_offset);
1082 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1083 cpdma_chan_get_stats(cpsw->rxch[ch], &ch_stats);
1084 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1085 p = (u8 *)&ch_stats +
1086 cpsw_gstrings_ch_stats[i].stat_offset;
1087 data[l] = *(u32 *)p;
1091 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1092 cpdma_chan_get_stats(cpsw->txch[ch], &ch_stats);
1093 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1094 p = (u8 *)&ch_stats +
1095 cpsw_gstrings_ch_stats[i].stat_offset;
1096 data[l] = *(u32 *)p;
1101 static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
1104 u32 usage_count = 0;
1106 if (!cpsw->data.dual_emac)
1109 for (i = 0; i < cpsw->data.slaves; i++)
1110 if (cpsw->slaves[i].open_stat)
1116 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1117 struct sk_buff *skb,
1118 struct cpdma_chan *txch)
1120 struct cpsw_common *cpsw = priv->cpsw;
1122 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1123 priv->emac_port + cpsw->data.dual_emac);
1126 static inline void cpsw_add_dual_emac_def_ale_entries(
1127 struct cpsw_priv *priv, struct cpsw_slave *slave,
1130 struct cpsw_common *cpsw = priv->cpsw;
1131 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1133 if (cpsw->version == CPSW_VERSION_1)
1134 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1136 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
1137 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1138 port_mask, port_mask, 0);
1139 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1140 port_mask, ALE_VLAN, slave->port_vlan, 0);
1141 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1142 HOST_PORT_NUM, ALE_VLAN |
1143 ALE_SECURE, slave->port_vlan);
1144 cpsw_ale_control_set(cpsw->ale, slave_port,
1145 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1148 static void soft_reset_slave(struct cpsw_slave *slave)
1152 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1153 soft_reset(name, &slave->sliver->soft_reset);
1156 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1159 struct cpsw_common *cpsw = priv->cpsw;
1161 soft_reset_slave(slave);
1163 /* setup priority mapping */
1164 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
1166 switch (cpsw->version) {
1167 case CPSW_VERSION_1:
1168 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1169 /* Increase RX FIFO size to 5 for supporting fullduplex
1173 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1174 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
1176 case CPSW_VERSION_2:
1177 case CPSW_VERSION_3:
1178 case CPSW_VERSION_4:
1179 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1180 /* Increase RX FIFO size to 5 for supporting fullduplex
1184 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1185 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
1189 /* setup max packet size, and mac address */
1190 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1191 cpsw_set_slave_mac(slave, priv);
1193 slave->mac_control = 0; /* no link yet */
1195 slave_port = cpsw_get_slave_port(slave->slave_num);
1197 if (cpsw->data.dual_emac)
1198 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1200 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1201 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1203 if (slave->data->phy_node) {
1204 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
1205 &cpsw_adjust_link, 0, slave->data->phy_if);
1207 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1208 slave->data->phy_node->full_name,
1213 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
1214 &cpsw_adjust_link, slave->data->phy_if);
1215 if (IS_ERR(slave->phy)) {
1217 "phy \"%s\" not found on slave %d, err %ld\n",
1218 slave->data->phy_id, slave->slave_num,
1219 PTR_ERR(slave->phy));
1225 phy_attached_info(slave->phy);
1227 phy_start(slave->phy);
1229 /* Configure GMII_SEL register */
1230 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1233 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1235 struct cpsw_common *cpsw = priv->cpsw;
1236 const int vlan = cpsw->data.default_vlan;
1239 int unreg_mcast_mask;
1241 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
1244 writel(vlan, &cpsw->host_port_regs->port_vlan);
1246 for (i = 0; i < cpsw->data.slaves; i++)
1247 slave_write(cpsw->slaves + i, vlan, reg);
1249 if (priv->ndev->flags & IFF_ALLMULTI)
1250 unreg_mcast_mask = ALE_ALL_PORTS;
1252 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1254 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
1255 ALE_ALL_PORTS, ALE_ALL_PORTS,
1259 static void cpsw_init_host_port(struct cpsw_priv *priv)
1263 struct cpsw_common *cpsw = priv->cpsw;
1265 /* soft reset the controller and initialize ale */
1266 soft_reset("cpsw", &cpsw->regs->soft_reset);
1267 cpsw_ale_start(cpsw->ale);
1269 /* switch to vlan unaware mode */
1270 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
1271 CPSW_ALE_VLAN_AWARE);
1272 control_reg = readl(&cpsw->regs->control);
1273 control_reg |= CPSW_VLAN_AWARE;
1274 writel(control_reg, &cpsw->regs->control);
1275 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1276 CPSW_FIFO_NORMAL_MODE;
1277 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1279 /* setup host port priority mapping */
1280 __raw_writel(CPDMA_TX_PRIORITY_MAP,
1281 &cpsw->host_port_regs->cpdma_tx_pri_map);
1282 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1284 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1285 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1287 if (!cpsw->data.dual_emac) {
1288 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1290 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1291 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1295 static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1297 struct cpsw_common *cpsw = priv->cpsw;
1298 struct sk_buff *skb;
1302 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1303 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxch[ch]);
1304 for (i = 0; i < ch_buf_num; i++) {
1305 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1306 cpsw->rx_packet_max,
1309 cpsw_err(priv, ifup, "cannot allocate skb\n");
1313 skb_set_queue_mapping(skb, ch);
1314 ret = cpdma_chan_submit(cpsw->rxch[ch], skb, skb->data,
1315 skb_tailroom(skb), 0);
1317 cpsw_err(priv, ifup,
1318 "cannot submit skb to channel %d rx, error %d\n",
1323 kmemleak_not_leak(skb);
1326 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1333 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1337 slave_port = cpsw_get_slave_port(slave->slave_num);
1341 phy_stop(slave->phy);
1342 phy_disconnect(slave->phy);
1344 cpsw_ale_control_set(cpsw->ale, slave_port,
1345 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1346 soft_reset_slave(slave);
1349 static int cpsw_ndo_open(struct net_device *ndev)
1351 struct cpsw_priv *priv = netdev_priv(ndev);
1352 struct cpsw_common *cpsw = priv->cpsw;
1356 ret = pm_runtime_get_sync(cpsw->dev);
1358 pm_runtime_put_noidle(cpsw->dev);
1362 if (!cpsw_common_res_usage_state(cpsw))
1363 cpsw_intr_disable(cpsw);
1364 netif_carrier_off(ndev);
1366 /* Notify the stack of the actual queue counts. */
1367 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1369 dev_err(priv->dev, "cannot set real number of tx queues\n");
1373 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1375 dev_err(priv->dev, "cannot set real number of rx queues\n");
1379 reg = cpsw->version;
1381 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1382 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1383 CPSW_RTL_VERSION(reg));
1385 /* initialize host and slave ports */
1386 if (!cpsw_common_res_usage_state(cpsw))
1387 cpsw_init_host_port(priv);
1388 for_each_slave(priv, cpsw_slave_open, priv);
1390 /* Add default VLAN */
1391 if (!cpsw->data.dual_emac)
1392 cpsw_add_default_vlan(priv);
1394 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
1395 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
1397 if (!cpsw_common_res_usage_state(cpsw)) {
1398 /* setup tx dma to fixed prio and zero offset */
1399 cpdma_control_set(cpsw->dma, CPDMA_TX_PRIO_FIXED, 1);
1400 cpdma_control_set(cpsw->dma, CPDMA_RX_BUFFER_OFFSET, 0);
1402 /* disable priority elevation */
1403 __raw_writel(0, &cpsw->regs->ptype);
1405 /* enable statistics collection only on all ports */
1406 __raw_writel(0x7, &cpsw->regs->stat_port_en);
1408 /* Enable internal fifo flow control */
1409 writel(0x7, &cpsw->regs->flow_control);
1411 napi_enable(&cpsw->napi_rx);
1412 napi_enable(&cpsw->napi_tx);
1414 if (cpsw->tx_irq_disabled) {
1415 cpsw->tx_irq_disabled = false;
1416 enable_irq(cpsw->irqs_table[1]);
1419 if (cpsw->rx_irq_disabled) {
1420 cpsw->rx_irq_disabled = false;
1421 enable_irq(cpsw->irqs_table[0]);
1424 ret = cpsw_fill_rx_channels(priv);
1428 if (cpts_register(cpsw->dev, cpsw->cpts,
1429 cpsw->data.cpts_clock_mult,
1430 cpsw->data.cpts_clock_shift))
1431 dev_err(priv->dev, "error registering cpts device\n");
1435 /* Enable Interrupt pacing if configured */
1436 if (cpsw->coal_intvl != 0) {
1437 struct ethtool_coalesce coal;
1439 coal.rx_coalesce_usecs = cpsw->coal_intvl;
1440 cpsw_set_coalesce(ndev, &coal);
1443 cpdma_ctlr_start(cpsw->dma);
1444 cpsw_intr_enable(cpsw);
1446 if (cpsw->data.dual_emac)
1447 cpsw->slaves[priv->emac_port].open_stat = true;
1449 netif_tx_start_all_queues(ndev);
1454 cpdma_ctlr_stop(cpsw->dma);
1455 for_each_slave(priv, cpsw_slave_stop, cpsw);
1456 pm_runtime_put_sync(cpsw->dev);
1457 netif_carrier_off(priv->ndev);
1461 static int cpsw_ndo_stop(struct net_device *ndev)
1463 struct cpsw_priv *priv = netdev_priv(ndev);
1464 struct cpsw_common *cpsw = priv->cpsw;
1466 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1467 netif_tx_stop_all_queues(priv->ndev);
1468 netif_carrier_off(priv->ndev);
1470 if (cpsw_common_res_usage_state(cpsw) <= 1) {
1471 napi_disable(&cpsw->napi_rx);
1472 napi_disable(&cpsw->napi_tx);
1473 cpts_unregister(cpsw->cpts);
1474 cpsw_intr_disable(cpsw);
1475 cpdma_ctlr_stop(cpsw->dma);
1476 cpsw_ale_stop(cpsw->ale);
1478 for_each_slave(priv, cpsw_slave_stop, cpsw);
1479 pm_runtime_put_sync(cpsw->dev);
1480 if (cpsw->data.dual_emac)
1481 cpsw->slaves[priv->emac_port].open_stat = false;
1485 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1486 struct net_device *ndev)
1488 struct cpsw_priv *priv = netdev_priv(ndev);
1489 struct cpsw_common *cpsw = priv->cpsw;
1490 struct netdev_queue *txq;
1491 struct cpdma_chan *txch;
1494 netif_trans_update(ndev);
1496 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1497 cpsw_err(priv, tx_err, "packet pad failed\n");
1498 ndev->stats.tx_dropped++;
1499 return NETDEV_TX_OK;
1502 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
1503 cpsw->cpts->tx_enable)
1504 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1506 skb_tx_timestamp(skb);
1508 q_idx = skb_get_queue_mapping(skb);
1509 if (q_idx >= cpsw->tx_ch_num)
1510 q_idx = q_idx % cpsw->tx_ch_num;
1512 txch = cpsw->txch[q_idx];
1513 ret = cpsw_tx_packet_submit(priv, skb, txch);
1514 if (unlikely(ret != 0)) {
1515 cpsw_err(priv, tx_err, "desc submit failed\n");
1519 /* If there is no more tx desc left free then we need to
1520 * tell the kernel to stop sending us tx frames.
1522 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1523 txq = netdev_get_tx_queue(ndev, q_idx);
1524 netif_tx_stop_queue(txq);
1527 return NETDEV_TX_OK;
1529 ndev->stats.tx_dropped++;
1530 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
1531 netif_tx_stop_queue(txq);
1532 return NETDEV_TX_BUSY;
1535 #ifdef CONFIG_TI_CPTS
1537 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
1539 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
1542 if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) {
1543 slave_write(slave, 0, CPSW1_TS_CTL);
1547 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1548 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1550 if (cpsw->cpts->tx_enable)
1551 ts_en |= CPSW_V1_TS_TX_EN;
1553 if (cpsw->cpts->rx_enable)
1554 ts_en |= CPSW_V1_TS_RX_EN;
1556 slave_write(slave, ts_en, CPSW1_TS_CTL);
1557 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1560 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1562 struct cpsw_slave *slave;
1563 struct cpsw_common *cpsw = priv->cpsw;
1566 if (cpsw->data.dual_emac)
1567 slave = &cpsw->slaves[priv->emac_port];
1569 slave = &cpsw->slaves[cpsw->data.active_slave];
1571 ctrl = slave_read(slave, CPSW2_CONTROL);
1572 switch (cpsw->version) {
1573 case CPSW_VERSION_2:
1574 ctrl &= ~CTRL_V2_ALL_TS_MASK;
1576 if (cpsw->cpts->tx_enable)
1577 ctrl |= CTRL_V2_TX_TS_BITS;
1579 if (cpsw->cpts->rx_enable)
1580 ctrl |= CTRL_V2_RX_TS_BITS;
1582 case CPSW_VERSION_3:
1584 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1586 if (cpsw->cpts->tx_enable)
1587 ctrl |= CTRL_V3_TX_TS_BITS;
1589 if (cpsw->cpts->rx_enable)
1590 ctrl |= CTRL_V3_RX_TS_BITS;
1594 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1596 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1597 slave_write(slave, ctrl, CPSW2_CONTROL);
1598 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
1601 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1603 struct cpsw_priv *priv = netdev_priv(dev);
1604 struct hwtstamp_config cfg;
1605 struct cpsw_common *cpsw = priv->cpsw;
1606 struct cpts *cpts = cpsw->cpts;
1608 if (cpsw->version != CPSW_VERSION_1 &&
1609 cpsw->version != CPSW_VERSION_2 &&
1610 cpsw->version != CPSW_VERSION_3)
1613 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1616 /* reserved for future extensions */
1620 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
1623 switch (cfg.rx_filter) {
1624 case HWTSTAMP_FILTER_NONE:
1625 cpts->rx_enable = 0;
1627 case HWTSTAMP_FILTER_ALL:
1628 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1629 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1630 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1632 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1633 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1634 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1635 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1636 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1637 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1638 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1639 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1640 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1641 cpts->rx_enable = 1;
1642 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1648 cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
1650 switch (cpsw->version) {
1651 case CPSW_VERSION_1:
1652 cpsw_hwtstamp_v1(cpsw);
1654 case CPSW_VERSION_2:
1655 case CPSW_VERSION_3:
1656 cpsw_hwtstamp_v2(priv);
1662 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1665 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1667 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1668 struct cpts *cpts = cpsw->cpts;
1669 struct hwtstamp_config cfg;
1671 if (cpsw->version != CPSW_VERSION_1 &&
1672 cpsw->version != CPSW_VERSION_2 &&
1673 cpsw->version != CPSW_VERSION_3)
1677 cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1678 cfg.rx_filter = (cpts->rx_enable ?
1679 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1681 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1684 #endif /*CONFIG_TI_CPTS*/
1686 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1688 struct cpsw_priv *priv = netdev_priv(dev);
1689 struct cpsw_common *cpsw = priv->cpsw;
1690 int slave_no = cpsw_slave_index(cpsw, priv);
1692 if (!netif_running(dev))
1696 #ifdef CONFIG_TI_CPTS
1698 return cpsw_hwtstamp_set(dev, req);
1700 return cpsw_hwtstamp_get(dev, req);
1704 if (!cpsw->slaves[slave_no].phy)
1706 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
1709 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1711 struct cpsw_priv *priv = netdev_priv(ndev);
1712 struct cpsw_common *cpsw = priv->cpsw;
1715 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
1716 ndev->stats.tx_errors++;
1717 cpsw_intr_disable(cpsw);
1718 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1719 cpdma_chan_stop(cpsw->txch[ch]);
1720 cpdma_chan_start(cpsw->txch[ch]);
1723 cpsw_intr_enable(cpsw);
1726 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1728 struct cpsw_priv *priv = netdev_priv(ndev);
1729 struct sockaddr *addr = (struct sockaddr *)p;
1730 struct cpsw_common *cpsw = priv->cpsw;
1735 if (!is_valid_ether_addr(addr->sa_data))
1736 return -EADDRNOTAVAIL;
1738 ret = pm_runtime_get_sync(cpsw->dev);
1740 pm_runtime_put_noidle(cpsw->dev);
1744 if (cpsw->data.dual_emac) {
1745 vid = cpsw->slaves[priv->emac_port].port_vlan;
1749 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1751 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1754 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1755 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1756 for_each_slave(priv, cpsw_set_slave_mac, priv);
1758 pm_runtime_put(cpsw->dev);
1763 #ifdef CONFIG_NET_POLL_CONTROLLER
1764 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1766 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1768 cpsw_intr_disable(cpsw);
1769 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1770 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1771 cpsw_intr_enable(cpsw);
1775 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1779 int unreg_mcast_mask = 0;
1781 struct cpsw_common *cpsw = priv->cpsw;
1783 if (cpsw->data.dual_emac) {
1784 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
1786 if (priv->ndev->flags & IFF_ALLMULTI)
1787 unreg_mcast_mask = port_mask;
1789 port_mask = ALE_ALL_PORTS;
1791 if (priv->ndev->flags & IFF_ALLMULTI)
1792 unreg_mcast_mask = ALE_ALL_PORTS;
1794 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1797 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
1802 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1803 HOST_PORT_NUM, ALE_VLAN, vid);
1807 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1808 port_mask, ALE_VLAN, vid, 0);
1810 goto clean_vlan_ucast;
1814 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1815 HOST_PORT_NUM, ALE_VLAN, vid);
1817 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1821 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
1822 __be16 proto, u16 vid)
1824 struct cpsw_priv *priv = netdev_priv(ndev);
1825 struct cpsw_common *cpsw = priv->cpsw;
1828 if (vid == cpsw->data.default_vlan)
1831 ret = pm_runtime_get_sync(cpsw->dev);
1833 pm_runtime_put_noidle(cpsw->dev);
1837 if (cpsw->data.dual_emac) {
1838 /* In dual EMAC, reserved VLAN id should not be used for
1839 * creating VLAN interfaces as this can break the dual
1840 * EMAC port separation
1844 for (i = 0; i < cpsw->data.slaves; i++) {
1845 if (vid == cpsw->slaves[i].port_vlan)
1850 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1851 ret = cpsw_add_vlan_ale_entry(priv, vid);
1853 pm_runtime_put(cpsw->dev);
1857 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1858 __be16 proto, u16 vid)
1860 struct cpsw_priv *priv = netdev_priv(ndev);
1861 struct cpsw_common *cpsw = priv->cpsw;
1864 if (vid == cpsw->data.default_vlan)
1867 ret = pm_runtime_get_sync(cpsw->dev);
1869 pm_runtime_put_noidle(cpsw->dev);
1873 if (cpsw->data.dual_emac) {
1876 for (i = 0; i < cpsw->data.slaves; i++) {
1877 if (vid == cpsw->slaves[i].port_vlan)
1882 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1883 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1887 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1888 HOST_PORT_NUM, ALE_VLAN, vid);
1892 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1894 pm_runtime_put(cpsw->dev);
1898 static const struct net_device_ops cpsw_netdev_ops = {
1899 .ndo_open = cpsw_ndo_open,
1900 .ndo_stop = cpsw_ndo_stop,
1901 .ndo_start_xmit = cpsw_ndo_start_xmit,
1902 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
1903 .ndo_do_ioctl = cpsw_ndo_ioctl,
1904 .ndo_validate_addr = eth_validate_addr,
1905 .ndo_change_mtu = eth_change_mtu,
1906 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
1907 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
1908 #ifdef CONFIG_NET_POLL_CONTROLLER
1909 .ndo_poll_controller = cpsw_ndo_poll_controller,
1911 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1912 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
1915 static int cpsw_get_regs_len(struct net_device *ndev)
1917 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1919 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
1922 static void cpsw_get_regs(struct net_device *ndev,
1923 struct ethtool_regs *regs, void *p)
1926 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1928 /* update CPSW IP version */
1929 regs->version = cpsw->version;
1931 cpsw_ale_dump(cpsw->ale, reg);
1934 static void cpsw_get_drvinfo(struct net_device *ndev,
1935 struct ethtool_drvinfo *info)
1937 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1938 struct platform_device *pdev = to_platform_device(cpsw->dev);
1940 strlcpy(info->driver, "cpsw", sizeof(info->driver));
1941 strlcpy(info->version, "1.0", sizeof(info->version));
1942 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1945 static u32 cpsw_get_msglevel(struct net_device *ndev)
1947 struct cpsw_priv *priv = netdev_priv(ndev);
1948 return priv->msg_enable;
1951 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1953 struct cpsw_priv *priv = netdev_priv(ndev);
1954 priv->msg_enable = value;
1957 static int cpsw_get_ts_info(struct net_device *ndev,
1958 struct ethtool_ts_info *info)
1960 #ifdef CONFIG_TI_CPTS
1961 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1963 info->so_timestamping =
1964 SOF_TIMESTAMPING_TX_HARDWARE |
1965 SOF_TIMESTAMPING_TX_SOFTWARE |
1966 SOF_TIMESTAMPING_RX_HARDWARE |
1967 SOF_TIMESTAMPING_RX_SOFTWARE |
1968 SOF_TIMESTAMPING_SOFTWARE |
1969 SOF_TIMESTAMPING_RAW_HARDWARE;
1970 info->phc_index = cpsw->cpts->phc_index;
1972 (1 << HWTSTAMP_TX_OFF) |
1973 (1 << HWTSTAMP_TX_ON);
1975 (1 << HWTSTAMP_FILTER_NONE) |
1976 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1978 info->so_timestamping =
1979 SOF_TIMESTAMPING_TX_SOFTWARE |
1980 SOF_TIMESTAMPING_RX_SOFTWARE |
1981 SOF_TIMESTAMPING_SOFTWARE;
1982 info->phc_index = -1;
1984 info->rx_filters = 0;
1989 static int cpsw_get_settings(struct net_device *ndev,
1990 struct ethtool_cmd *ecmd)
1992 struct cpsw_priv *priv = netdev_priv(ndev);
1993 struct cpsw_common *cpsw = priv->cpsw;
1994 int slave_no = cpsw_slave_index(cpsw, priv);
1996 if (cpsw->slaves[slave_no].phy)
1997 return phy_ethtool_gset(cpsw->slaves[slave_no].phy, ecmd);
2002 static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
2004 struct cpsw_priv *priv = netdev_priv(ndev);
2005 struct cpsw_common *cpsw = priv->cpsw;
2006 int slave_no = cpsw_slave_index(cpsw, priv);
2008 if (cpsw->slaves[slave_no].phy)
2009 return phy_ethtool_sset(cpsw->slaves[slave_no].phy, ecmd);
2014 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2016 struct cpsw_priv *priv = netdev_priv(ndev);
2017 struct cpsw_common *cpsw = priv->cpsw;
2018 int slave_no = cpsw_slave_index(cpsw, priv);
2023 if (cpsw->slaves[slave_no].phy)
2024 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2027 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2029 struct cpsw_priv *priv = netdev_priv(ndev);
2030 struct cpsw_common *cpsw = priv->cpsw;
2031 int slave_no = cpsw_slave_index(cpsw, priv);
2033 if (cpsw->slaves[slave_no].phy)
2034 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2039 static void cpsw_get_pauseparam(struct net_device *ndev,
2040 struct ethtool_pauseparam *pause)
2042 struct cpsw_priv *priv = netdev_priv(ndev);
2044 pause->autoneg = AUTONEG_DISABLE;
2045 pause->rx_pause = priv->rx_pause ? true : false;
2046 pause->tx_pause = priv->tx_pause ? true : false;
2049 static int cpsw_set_pauseparam(struct net_device *ndev,
2050 struct ethtool_pauseparam *pause)
2052 struct cpsw_priv *priv = netdev_priv(ndev);
2055 priv->rx_pause = pause->rx_pause ? true : false;
2056 priv->tx_pause = pause->tx_pause ? true : false;
2058 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
2062 static int cpsw_ethtool_op_begin(struct net_device *ndev)
2064 struct cpsw_priv *priv = netdev_priv(ndev);
2065 struct cpsw_common *cpsw = priv->cpsw;
2068 ret = pm_runtime_get_sync(cpsw->dev);
2070 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
2071 pm_runtime_put_noidle(cpsw->dev);
2077 static void cpsw_ethtool_op_complete(struct net_device *ndev)
2079 struct cpsw_priv *priv = netdev_priv(ndev);
2082 ret = pm_runtime_put(priv->cpsw->dev);
2084 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2087 static void cpsw_get_channels(struct net_device *ndev,
2088 struct ethtool_channels *ch)
2090 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2092 ch->max_combined = 0;
2093 ch->max_rx = CPSW_MAX_QUEUES;
2094 ch->max_tx = CPSW_MAX_QUEUES;
2096 ch->other_count = 0;
2097 ch->rx_count = cpsw->rx_ch_num;
2098 ch->tx_count = cpsw->tx_ch_num;
2099 ch->combined_count = 0;
2102 static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2103 struct ethtool_channels *ch)
2105 if (ch->combined_count)
2108 /* verify we have at least one channel in each direction */
2109 if (!ch->rx_count || !ch->tx_count)
2112 if (ch->rx_count > cpsw->data.channels ||
2113 ch->tx_count > cpsw->data.channels)
2119 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2121 int (*poll)(struct napi_struct *, int);
2122 struct cpsw_common *cpsw = priv->cpsw;
2123 void (*handler)(void *, int, int);
2124 struct cpdma_chan **chan;
2128 ch = &cpsw->rx_ch_num;
2130 handler = cpsw_rx_handler;
2131 poll = cpsw_rx_poll;
2133 ch = &cpsw->tx_ch_num;
2135 handler = cpsw_tx_handler;
2136 poll = cpsw_tx_poll;
2139 while (*ch < ch_num) {
2140 chan[*ch] = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
2142 if (IS_ERR(chan[*ch]))
2143 return PTR_ERR(chan[*ch]);
2148 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2149 (rx ? "rx" : "tx"));
2153 while (*ch > ch_num) {
2156 ret = cpdma_chan_destroy(chan[*ch]);
2160 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2161 (rx ? "rx" : "tx"));
2167 static int cpsw_update_channels(struct cpsw_priv *priv,
2168 struct ethtool_channels *ch)
2172 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2176 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2183 static int cpsw_set_channels(struct net_device *ndev,
2184 struct ethtool_channels *chs)
2186 struct cpsw_priv *priv = netdev_priv(ndev);
2187 struct cpsw_common *cpsw = priv->cpsw;
2188 struct cpsw_slave *slave;
2191 ret = cpsw_check_ch_settings(cpsw, chs);
2195 /* Disable NAPI scheduling */
2196 cpsw_intr_disable(cpsw);
2198 /* Stop all transmit queues for every network device.
2199 * Disable re-using rx descriptors with dormant_on.
2201 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2202 if (!(slave->ndev && netif_running(slave->ndev)))
2205 netif_tx_stop_all_queues(slave->ndev);
2206 netif_dormant_on(slave->ndev);
2209 /* Handle rest of tx packets and stop cpdma channels */
2210 cpdma_ctlr_stop(cpsw->dma);
2211 ret = cpsw_update_channels(priv, chs);
2215 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2216 if (!(slave->ndev && netif_running(slave->ndev)))
2219 /* Inform stack about new count of queues */
2220 ret = netif_set_real_num_tx_queues(slave->ndev,
2223 dev_err(priv->dev, "cannot set real number of tx queues\n");
2227 ret = netif_set_real_num_rx_queues(slave->ndev,
2230 dev_err(priv->dev, "cannot set real number of rx queues\n");
2234 /* Enable rx packets handling */
2235 netif_dormant_off(slave->ndev);
2238 if (cpsw_common_res_usage_state(cpsw)) {
2239 ret = cpsw_fill_rx_channels(priv);
2243 /* After this receive is started */
2244 cpdma_ctlr_start(cpsw->dma);
2245 cpsw_intr_enable(cpsw);
2248 /* Resume transmit for every affected interface */
2249 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2250 if (!(slave->ndev && netif_running(slave->ndev)))
2252 netif_tx_start_all_queues(slave->ndev);
2256 dev_err(priv->dev, "cannot update channels number, closing device\n");
2261 static const struct ethtool_ops cpsw_ethtool_ops = {
2262 .get_drvinfo = cpsw_get_drvinfo,
2263 .get_msglevel = cpsw_get_msglevel,
2264 .set_msglevel = cpsw_set_msglevel,
2265 .get_link = ethtool_op_get_link,
2266 .get_ts_info = cpsw_get_ts_info,
2267 .get_settings = cpsw_get_settings,
2268 .set_settings = cpsw_set_settings,
2269 .get_coalesce = cpsw_get_coalesce,
2270 .set_coalesce = cpsw_set_coalesce,
2271 .get_sset_count = cpsw_get_sset_count,
2272 .get_strings = cpsw_get_strings,
2273 .get_ethtool_stats = cpsw_get_ethtool_stats,
2274 .get_pauseparam = cpsw_get_pauseparam,
2275 .set_pauseparam = cpsw_set_pauseparam,
2276 .get_wol = cpsw_get_wol,
2277 .set_wol = cpsw_set_wol,
2278 .get_regs_len = cpsw_get_regs_len,
2279 .get_regs = cpsw_get_regs,
2280 .begin = cpsw_ethtool_op_begin,
2281 .complete = cpsw_ethtool_op_complete,
2282 .get_channels = cpsw_get_channels,
2283 .set_channels = cpsw_set_channels,
2286 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
2287 u32 slave_reg_ofs, u32 sliver_reg_ofs)
2289 void __iomem *regs = cpsw->regs;
2290 int slave_num = slave->slave_num;
2291 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
2294 slave->regs = regs + slave_reg_ofs;
2295 slave->sliver = regs + sliver_reg_ofs;
2296 slave->port_vlan = data->dual_emac_res_vlan;
2299 static int cpsw_probe_dt(struct cpsw_platform_data *data,
2300 struct platform_device *pdev)
2302 struct device_node *node = pdev->dev.of_node;
2303 struct device_node *slave_node;
2310 if (of_property_read_u32(node, "slaves", &prop)) {
2311 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
2314 data->slaves = prop;
2316 if (of_property_read_u32(node, "active_slave", &prop)) {
2317 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
2320 data->active_slave = prop;
2322 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
2323 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
2326 data->cpts_clock_mult = prop;
2328 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
2329 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
2332 data->cpts_clock_shift = prop;
2334 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2335 * sizeof(struct cpsw_slave_data),
2337 if (!data->slave_data)
2340 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
2341 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
2344 data->channels = prop;
2346 if (of_property_read_u32(node, "ale_entries", &prop)) {
2347 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
2350 data->ale_entries = prop;
2352 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
2353 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
2356 data->bd_ram_size = prop;
2358 if (of_property_read_u32(node, "mac_control", &prop)) {
2359 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
2362 data->mac_control = prop;
2364 if (of_property_read_bool(node, "dual_emac"))
2365 data->dual_emac = 1;
2368 * Populate all the child nodes here...
2370 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2371 /* We do not want to force this, as in some cases may not have child */
2373 dev_warn(&pdev->dev, "Doesn't have any child node\n");
2375 for_each_available_child_of_node(node, slave_node) {
2376 struct cpsw_slave_data *slave_data = data->slave_data + i;
2377 const void *mac_addr = NULL;
2381 /* This is no slave child node, continue */
2382 if (strcmp(slave_node->name, "slave"))
2385 slave_data->phy_node = of_parse_phandle(slave_node,
2387 parp = of_get_property(slave_node, "phy_id", &lenp);
2388 if (slave_data->phy_node) {
2390 "slave[%d] using phy-handle=\"%s\"\n",
2391 i, slave_data->phy_node->full_name);
2392 } else if (of_phy_is_fixed_link(slave_node)) {
2393 /* In the case of a fixed PHY, the DT node associated
2394 * to the PHY is the Ethernet MAC DT node.
2396 ret = of_phy_register_fixed_link(slave_node);
2398 if (ret != -EPROBE_DEFER)
2399 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
2402 slave_data->phy_node = of_node_get(slave_node);
2405 struct device_node *mdio_node;
2406 struct platform_device *mdio;
2408 if (lenp != (sizeof(__be32) * 2)) {
2409 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2412 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2413 phyid = be32_to_cpup(parp+1);
2414 mdio = of_find_device_by_node(mdio_node);
2415 of_node_put(mdio_node);
2417 dev_err(&pdev->dev, "Missing mdio platform device\n");
2420 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2421 PHY_ID_FMT, mdio->name, phyid);
2422 put_device(&mdio->dev);
2425 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2429 slave_data->phy_if = of_get_phy_mode(slave_node);
2430 if (slave_data->phy_if < 0) {
2431 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2433 return slave_data->phy_if;
2437 mac_addr = of_get_mac_address(slave_node);
2439 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
2441 ret = ti_cm_get_macid(&pdev->dev, i,
2442 slave_data->mac_addr);
2446 if (data->dual_emac) {
2447 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
2449 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
2450 slave_data->dual_emac_res_vlan = i+1;
2451 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2452 slave_data->dual_emac_res_vlan, i);
2454 slave_data->dual_emac_res_vlan = prop;
2459 if (i == data->slaves)
2466 static void cpsw_remove_dt(struct platform_device *pdev)
2468 struct net_device *ndev = platform_get_drvdata(pdev);
2469 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2470 struct cpsw_platform_data *data = &cpsw->data;
2471 struct device_node *node = pdev->dev.of_node;
2472 struct device_node *slave_node;
2475 for_each_available_child_of_node(node, slave_node) {
2476 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2478 if (strcmp(slave_node->name, "slave"))
2481 if (of_phy_is_fixed_link(slave_node))
2482 of_phy_deregister_fixed_link(slave_node);
2484 of_node_put(slave_data->phy_node);
2487 if (i == data->slaves)
2491 of_platform_depopulate(&pdev->dev);
2494 static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
2496 struct cpsw_common *cpsw = priv->cpsw;
2497 struct cpsw_platform_data *data = &cpsw->data;
2498 struct net_device *ndev;
2499 struct cpsw_priv *priv_sl2;
2502 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2504 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
2508 priv_sl2 = netdev_priv(ndev);
2509 priv_sl2->cpsw = cpsw;
2510 priv_sl2->ndev = ndev;
2511 priv_sl2->dev = &ndev->dev;
2512 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2514 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2515 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2517 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2518 priv_sl2->mac_addr);
2520 random_ether_addr(priv_sl2->mac_addr);
2521 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2522 priv_sl2->mac_addr);
2524 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2526 priv_sl2->emac_port = 1;
2527 cpsw->slaves[1].ndev = ndev;
2528 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2530 ndev->netdev_ops = &cpsw_netdev_ops;
2531 ndev->ethtool_ops = &cpsw_ethtool_ops;
2533 /* register the network device */
2534 SET_NETDEV_DEV(ndev, cpsw->dev);
2535 ret = register_netdev(ndev);
2537 dev_err(cpsw->dev, "cpsw: error registering net device\n");
2545 #define CPSW_QUIRK_IRQ BIT(0)
2547 static struct platform_device_id cpsw_devtype[] = {
2549 /* keep it for existing comaptibles */
2551 .driver_data = CPSW_QUIRK_IRQ,
2553 .name = "am335x-cpsw",
2554 .driver_data = CPSW_QUIRK_IRQ,
2556 .name = "am4372-cpsw",
2559 .name = "dra7-cpsw",
2565 MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2574 static const struct of_device_id cpsw_of_mtable[] = {
2575 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2576 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2577 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2578 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2581 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2583 static int cpsw_probe(struct platform_device *pdev)
2586 struct cpsw_platform_data *data;
2587 struct net_device *ndev;
2588 struct cpsw_priv *priv;
2589 struct cpdma_params dma_params;
2590 struct cpsw_ale_params ale_params;
2591 void __iomem *ss_regs;
2592 struct resource *res, *ss_res;
2593 const struct of_device_id *of_id;
2594 struct gpio_descs *mode;
2595 u32 slave_offset, sliver_offset, slave_size;
2596 struct cpsw_common *cpsw;
2600 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
2604 cpsw->dev = &pdev->dev;
2606 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2608 dev_err(&pdev->dev, "error allocating net_device\n");
2612 platform_set_drvdata(pdev, ndev);
2613 priv = netdev_priv(ndev);
2616 priv->dev = &ndev->dev;
2617 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2618 cpsw->rx_packet_max = max(rx_packet_max, 128);
2619 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2621 dev_err(&pdev->dev, "error allocating cpts\n");
2623 goto clean_ndev_ret;
2626 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2628 ret = PTR_ERR(mode);
2629 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2630 goto clean_ndev_ret;
2634 * This may be required here for child devices.
2636 pm_runtime_enable(&pdev->dev);
2638 /* Select default pin state */
2639 pinctrl_pm_select_default_state(&pdev->dev);
2641 /* Need to enable clocks with runtime PM api to access module
2644 ret = pm_runtime_get_sync(&pdev->dev);
2646 pm_runtime_put_noidle(&pdev->dev);
2647 goto clean_runtime_disable_ret;
2650 ret = cpsw_probe_dt(&cpsw->data, pdev);
2655 cpsw->rx_ch_num = 1;
2656 cpsw->tx_ch_num = 1;
2658 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2659 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
2660 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
2662 eth_random_addr(priv->mac_addr);
2663 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
2666 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2668 cpsw->slaves = devm_kzalloc(&pdev->dev,
2669 sizeof(struct cpsw_slave) * data->slaves,
2671 if (!cpsw->slaves) {
2675 for (i = 0; i < data->slaves; i++)
2676 cpsw->slaves[i].slave_num = i;
2678 cpsw->slaves[0].ndev = ndev;
2679 priv->emac_port = 0;
2681 clk = devm_clk_get(&pdev->dev, "fck");
2683 dev_err(priv->dev, "fck is not found\n");
2687 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
2689 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2690 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2691 if (IS_ERR(ss_regs)) {
2692 ret = PTR_ERR(ss_regs);
2695 cpsw->regs = ss_regs;
2697 cpsw->version = readl(&cpsw->regs->id_ver);
2699 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2700 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2701 if (IS_ERR(cpsw->wr_regs)) {
2702 ret = PTR_ERR(cpsw->wr_regs);
2706 memset(&dma_params, 0, sizeof(dma_params));
2707 memset(&ale_params, 0, sizeof(ale_params));
2709 switch (cpsw->version) {
2710 case CPSW_VERSION_1:
2711 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
2712 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
2713 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
2714 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2715 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2716 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2717 slave_offset = CPSW1_SLAVE_OFFSET;
2718 slave_size = CPSW1_SLAVE_SIZE;
2719 sliver_offset = CPSW1_SLIVER_OFFSET;
2720 dma_params.desc_mem_phys = 0;
2722 case CPSW_VERSION_2:
2723 case CPSW_VERSION_3:
2724 case CPSW_VERSION_4:
2725 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
2726 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
2727 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
2728 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2729 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2730 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2731 slave_offset = CPSW2_SLAVE_OFFSET;
2732 slave_size = CPSW2_SLAVE_SIZE;
2733 sliver_offset = CPSW2_SLIVER_OFFSET;
2734 dma_params.desc_mem_phys =
2735 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
2738 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
2742 for (i = 0; i < cpsw->data.slaves; i++) {
2743 struct cpsw_slave *slave = &cpsw->slaves[i];
2745 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
2746 slave_offset += slave_size;
2747 sliver_offset += SLIVER_SIZE;
2750 dma_params.dev = &pdev->dev;
2751 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2752 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2753 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2754 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2755 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
2757 dma_params.num_chan = data->channels;
2758 dma_params.has_soft_reset = true;
2759 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2760 dma_params.desc_mem_size = data->bd_ram_size;
2761 dma_params.desc_align = 16;
2762 dma_params.has_ext_regs = true;
2763 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
2765 cpsw->dma = cpdma_ctlr_create(&dma_params);
2767 dev_err(priv->dev, "error initializing dma\n");
2772 cpsw->txch[0] = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
2773 cpsw->rxch[0] = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
2774 if (WARN_ON(!cpsw->rxch[0] || !cpsw->txch[0])) {
2775 dev_err(priv->dev, "error initializing dma channels\n");
2780 ale_params.dev = &ndev->dev;
2781 ale_params.ale_ageout = ale_ageout;
2782 ale_params.ale_entries = data->ale_entries;
2783 ale_params.ale_ports = data->slaves;
2785 cpsw->ale = cpsw_ale_create(&ale_params);
2787 dev_err(priv->dev, "error initializing ale engine\n");
2792 ndev->irq = platform_get_irq(pdev, 1);
2793 if (ndev->irq < 0) {
2794 dev_err(priv->dev, "error getting irq resource\n");
2799 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
2801 pdev->id_entry = of_id->data;
2802 if (pdev->id_entry->driver_data)
2803 cpsw->quirk_irq = true;
2806 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
2807 * MISC IRQs which are always kept disabled with this driver so
2808 * we will not request them.
2810 * If anyone wants to implement support for those, make sure to
2811 * first request and append them to irqs_table array.
2815 irq = platform_get_irq(pdev, 1);
2821 cpsw->irqs_table[0] = irq;
2822 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
2823 0, dev_name(&pdev->dev), cpsw);
2825 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2830 irq = platform_get_irq(pdev, 2);
2836 cpsw->irqs_table[1] = irq;
2837 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
2838 0, dev_name(&pdev->dev), cpsw);
2840 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2844 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2846 ndev->netdev_ops = &cpsw_netdev_ops;
2847 ndev->ethtool_ops = &cpsw_ethtool_ops;
2848 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
2849 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
2851 /* register the network device */
2852 SET_NETDEV_DEV(ndev, &pdev->dev);
2853 ret = register_netdev(ndev);
2855 dev_err(priv->dev, "error registering net device\n");
2860 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
2861 &ss_res->start, ndev->irq);
2863 if (cpsw->data.dual_emac) {
2864 ret = cpsw_probe_dual_emac(priv);
2866 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
2867 goto clean_unregister_netdev_ret;
2871 pm_runtime_put(&pdev->dev);
2875 clean_unregister_netdev_ret:
2876 unregister_netdev(ndev);
2878 cpsw_ale_destroy(cpsw->ale);
2880 cpdma_ctlr_destroy(cpsw->dma);
2882 cpsw_remove_dt(pdev);
2883 pm_runtime_put_sync(&pdev->dev);
2884 clean_runtime_disable_ret:
2885 pm_runtime_disable(&pdev->dev);
2887 free_netdev(priv->ndev);
2891 static int cpsw_remove(struct platform_device *pdev)
2893 struct net_device *ndev = platform_get_drvdata(pdev);
2894 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2897 ret = pm_runtime_get_sync(&pdev->dev);
2899 pm_runtime_put_noidle(&pdev->dev);
2903 if (cpsw->data.dual_emac)
2904 unregister_netdev(cpsw->slaves[1].ndev);
2905 unregister_netdev(ndev);
2907 cpsw_ale_destroy(cpsw->ale);
2908 cpdma_ctlr_destroy(cpsw->dma);
2909 cpsw_remove_dt(pdev);
2910 pm_runtime_put_sync(&pdev->dev);
2911 pm_runtime_disable(&pdev->dev);
2912 if (cpsw->data.dual_emac)
2913 free_netdev(cpsw->slaves[1].ndev);
2918 #ifdef CONFIG_PM_SLEEP
2919 static int cpsw_suspend(struct device *dev)
2921 struct platform_device *pdev = to_platform_device(dev);
2922 struct net_device *ndev = platform_get_drvdata(pdev);
2923 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2925 if (cpsw->data.dual_emac) {
2928 for (i = 0; i < cpsw->data.slaves; i++) {
2929 if (netif_running(cpsw->slaves[i].ndev))
2930 cpsw_ndo_stop(cpsw->slaves[i].ndev);
2933 if (netif_running(ndev))
2934 cpsw_ndo_stop(ndev);
2937 /* Select sleep pin state */
2938 pinctrl_pm_select_sleep_state(dev);
2943 static int cpsw_resume(struct device *dev)
2945 struct platform_device *pdev = to_platform_device(dev);
2946 struct net_device *ndev = platform_get_drvdata(pdev);
2947 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2949 /* Select default pin state */
2950 pinctrl_pm_select_default_state(dev);
2952 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2954 if (cpsw->data.dual_emac) {
2957 for (i = 0; i < cpsw->data.slaves; i++) {
2958 if (netif_running(cpsw->slaves[i].ndev))
2959 cpsw_ndo_open(cpsw->slaves[i].ndev);
2962 if (netif_running(ndev))
2963 cpsw_ndo_open(ndev);
2971 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2973 static struct platform_driver cpsw_driver = {
2977 .of_match_table = cpsw_of_mtable,
2979 .probe = cpsw_probe,
2980 .remove = cpsw_remove,
2983 module_platform_driver(cpsw_driver);
2985 MODULE_LICENSE("GPL");
2986 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
2987 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
2988 MODULE_DESCRIPTION("TI CPSW Ethernet driver");