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);
593 __dev_mc_unsync(ndev, NULL);
595 /* Flood All Unicast Packets to Host port */
596 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
597 dev_dbg(&ndev->dev, "promiscuity enabled\n");
599 /* Don't Flood All Unicast Packets to Host port */
600 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
602 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
603 for (i = 0; i <= cpsw->data.slaves; i++) {
604 cpsw_ale_control_set(ale, i,
605 ALE_PORT_NOLEARN, 0);
606 cpsw_ale_control_set(ale, i,
607 ALE_PORT_NO_SA_UPDATE, 0);
609 dev_dbg(&ndev->dev, "promiscuity disabled\n");
614 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
616 struct cpsw_priv *priv = netdev_priv(ndev);
617 struct cpsw_common *cpsw = priv->cpsw;
620 if (cpsw->data.dual_emac)
621 vid = cpsw->slaves[priv->emac_port].port_vlan;
623 vid = cpsw->data.default_vlan;
625 if (ndev->flags & IFF_PROMISC) {
626 /* Enable promiscuous mode */
627 cpsw_set_promiscious(ndev, true);
628 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
631 /* Disable promiscuous mode */
632 cpsw_set_promiscious(ndev, false);
635 /* Restore allmulti on vlans if necessary */
636 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
638 /* Clear all mcast from ALE */
639 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
641 if (!netdev_mc_empty(ndev)) {
642 struct netdev_hw_addr *ha;
644 /* program multicast address list into ALE register */
645 netdev_for_each_mc_addr(ha, ndev) {
646 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
651 static void cpsw_intr_enable(struct cpsw_common *cpsw)
653 __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
654 __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
656 cpdma_ctlr_int_ctrl(cpsw->dma, true);
660 static void cpsw_intr_disable(struct cpsw_common *cpsw)
662 __raw_writel(0, &cpsw->wr_regs->tx_en);
663 __raw_writel(0, &cpsw->wr_regs->rx_en);
665 cpdma_ctlr_int_ctrl(cpsw->dma, false);
669 static void cpsw_tx_handler(void *token, int len, int status)
671 struct netdev_queue *txq;
672 struct sk_buff *skb = token;
673 struct net_device *ndev = skb->dev;
674 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
676 /* Check whether the queue is stopped due to stalled tx dma, if the
677 * queue is stopped then start the queue as we have free desc for tx
679 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
680 if (unlikely(netif_tx_queue_stopped(txq)))
681 netif_tx_wake_queue(txq);
683 cpts_tx_timestamp(cpsw->cpts, skb);
684 ndev->stats.tx_packets++;
685 ndev->stats.tx_bytes += len;
686 dev_kfree_skb_any(skb);
689 static void cpsw_rx_handler(void *token, int len, int status)
691 struct cpdma_chan *ch;
692 struct sk_buff *skb = token;
693 struct sk_buff *new_skb;
694 struct net_device *ndev = skb->dev;
696 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
698 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
700 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
701 bool ndev_status = false;
702 struct cpsw_slave *slave = cpsw->slaves;
705 if (cpsw->data.dual_emac) {
706 /* In dual emac mode check for all interfaces */
707 for (n = cpsw->data.slaves; n; n--, slave++)
708 if (netif_running(slave->ndev))
712 if (ndev_status && (status >= 0)) {
713 /* The packet received is for the interface which
714 * is already down and the other interface is up
715 * and running, instead of freeing which results
716 * in reducing of the number of rx descriptor in
717 * DMA engine, requeue skb back to cpdma.
723 /* the interface is going down, skbs are purged */
724 dev_kfree_skb_any(skb);
728 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
730 skb_copy_queue_mapping(new_skb, skb);
732 cpts_rx_timestamp(cpsw->cpts, skb);
733 skb->protocol = eth_type_trans(skb, ndev);
734 netif_receive_skb(skb);
735 ndev->stats.rx_bytes += len;
736 ndev->stats.rx_packets++;
737 kmemleak_not_leak(new_skb);
739 ndev->stats.rx_dropped++;
744 if (netif_dormant(ndev)) {
745 dev_kfree_skb_any(new_skb);
749 ch = cpsw->rxch[skb_get_queue_mapping(new_skb)];
750 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
751 skb_tailroom(new_skb), 0);
752 if (WARN_ON(ret < 0))
753 dev_kfree_skb_any(new_skb);
756 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
758 struct cpsw_common *cpsw = dev_id;
760 writel(0, &cpsw->wr_regs->tx_en);
761 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
763 if (cpsw->quirk_irq) {
764 disable_irq_nosync(cpsw->irqs_table[1]);
765 cpsw->tx_irq_disabled = true;
768 napi_schedule(&cpsw->napi_tx);
772 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
774 struct cpsw_common *cpsw = dev_id;
776 writel(0, &cpsw->wr_regs->rx_en);
777 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
779 if (cpsw->quirk_irq) {
780 disable_irq_nosync(cpsw->irqs_table[0]);
781 cpsw->rx_irq_disabled = true;
784 napi_schedule(&cpsw->napi_rx);
788 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
792 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
794 /* process every unprocessed channel */
795 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
796 for (ch = 0, num_tx = 0; num_tx < budget; ch_map >>= 1, ch++) {
798 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
805 if (!(ch_map & 0x01))
808 num_tx += cpdma_chan_process(cpsw->txch[ch], budget - num_tx);
811 if (num_tx < budget) {
812 napi_complete(napi_tx);
813 writel(0xff, &cpsw->wr_regs->tx_en);
814 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
815 cpsw->tx_irq_disabled = false;
816 enable_irq(cpsw->irqs_table[1]);
823 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
827 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
829 /* process every unprocessed channel */
830 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
831 for (ch = 0, num_rx = 0; num_rx < budget; ch_map >>= 1, ch++) {
833 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
840 if (!(ch_map & 0x01))
843 num_rx += cpdma_chan_process(cpsw->rxch[ch], budget - num_rx);
846 if (num_rx < budget) {
847 napi_complete(napi_rx);
848 writel(0xff, &cpsw->wr_regs->rx_en);
849 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
850 cpsw->rx_irq_disabled = false;
851 enable_irq(cpsw->irqs_table[0]);
858 static inline void soft_reset(const char *module, void __iomem *reg)
860 unsigned long timeout = jiffies + HZ;
862 __raw_writel(1, reg);
865 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
867 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
870 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
871 ((mac)[2] << 16) | ((mac)[3] << 24))
872 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
874 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
875 struct cpsw_priv *priv)
877 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
878 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
881 static void _cpsw_adjust_link(struct cpsw_slave *slave,
882 struct cpsw_priv *priv, bool *link)
884 struct phy_device *phy = slave->phy;
887 struct cpsw_common *cpsw = priv->cpsw;
892 slave_port = cpsw_get_slave_port(slave->slave_num);
895 mac_control = cpsw->data.mac_control;
897 /* enable forwarding */
898 cpsw_ale_control_set(cpsw->ale, slave_port,
899 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
901 if (phy->speed == 1000)
902 mac_control |= BIT(7); /* GIGABITEN */
904 mac_control |= BIT(0); /* FULLDUPLEXEN */
906 /* set speed_in input in case RMII mode is used in 100Mbps */
907 if (phy->speed == 100)
908 mac_control |= BIT(15);
909 /* in band mode only works in 10Mbps RGMII mode */
910 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
911 mac_control |= BIT(18); /* In Band mode */
914 mac_control |= BIT(3);
917 mac_control |= BIT(4);
922 /* disable forwarding */
923 cpsw_ale_control_set(cpsw->ale, slave_port,
924 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
927 if (mac_control != slave->mac_control) {
928 phy_print_status(phy);
929 __raw_writel(mac_control, &slave->sliver->mac_control);
932 slave->mac_control = mac_control;
935 static void cpsw_adjust_link(struct net_device *ndev)
937 struct cpsw_priv *priv = netdev_priv(ndev);
940 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
943 netif_carrier_on(ndev);
944 if (netif_running(ndev))
945 netif_tx_wake_all_queues(ndev);
947 netif_carrier_off(ndev);
948 netif_tx_stop_all_queues(ndev);
952 static int cpsw_get_coalesce(struct net_device *ndev,
953 struct ethtool_coalesce *coal)
955 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
957 coal->rx_coalesce_usecs = cpsw->coal_intvl;
961 static int cpsw_set_coalesce(struct net_device *ndev,
962 struct ethtool_coalesce *coal)
964 struct cpsw_priv *priv = netdev_priv(ndev);
966 u32 num_interrupts = 0;
970 struct cpsw_common *cpsw = priv->cpsw;
972 coal_intvl = coal->rx_coalesce_usecs;
974 int_ctrl = readl(&cpsw->wr_regs->int_control);
975 prescale = cpsw->bus_freq_mhz * 4;
977 if (!coal->rx_coalesce_usecs) {
978 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
982 if (coal_intvl < CPSW_CMINTMIN_INTVL)
983 coal_intvl = CPSW_CMINTMIN_INTVL;
985 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
986 /* Interrupt pacer works with 4us Pulse, we can
987 * throttle further by dilating the 4us pulse.
989 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
991 if (addnl_dvdr > 1) {
992 prescale *= addnl_dvdr;
993 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
994 coal_intvl = (CPSW_CMINTMAX_INTVL
998 coal_intvl = CPSW_CMINTMAX_INTVL;
1002 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1003 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1004 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1006 int_ctrl |= CPSW_INTPACEEN;
1007 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1008 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1011 writel(int_ctrl, &cpsw->wr_regs->int_control);
1013 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
1014 cpsw->coal_intvl = coal_intvl;
1019 static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1021 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1025 return (CPSW_STATS_COMMON_LEN +
1026 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1033 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1039 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1040 for (i = 0; i < ch_stats_len; i++) {
1041 line = i % CPSW_STATS_CH_LEN;
1042 snprintf(*p, ETH_GSTRING_LEN,
1043 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1044 i / CPSW_STATS_CH_LEN,
1045 cpsw_gstrings_ch_stats[line].stat_string);
1046 *p += ETH_GSTRING_LEN;
1050 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1052 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1056 switch (stringset) {
1058 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1059 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1061 p += ETH_GSTRING_LEN;
1064 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1065 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1070 static void cpsw_get_ethtool_stats(struct net_device *ndev,
1071 struct ethtool_stats *stats, u64 *data)
1074 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1075 struct cpdma_chan_stats ch_stats;
1078 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
1079 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1080 data[l] = readl(cpsw->hw_stats +
1081 cpsw_gstrings_stats[l].stat_offset);
1083 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1084 cpdma_chan_get_stats(cpsw->rxch[ch], &ch_stats);
1085 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1086 p = (u8 *)&ch_stats +
1087 cpsw_gstrings_ch_stats[i].stat_offset;
1088 data[l] = *(u32 *)p;
1092 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1093 cpdma_chan_get_stats(cpsw->txch[ch], &ch_stats);
1094 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1095 p = (u8 *)&ch_stats +
1096 cpsw_gstrings_ch_stats[i].stat_offset;
1097 data[l] = *(u32 *)p;
1102 static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
1105 u32 usage_count = 0;
1107 if (!cpsw->data.dual_emac)
1110 for (i = 0; i < cpsw->data.slaves; i++)
1111 if (cpsw->slaves[i].open_stat)
1117 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1118 struct sk_buff *skb,
1119 struct cpdma_chan *txch)
1121 struct cpsw_common *cpsw = priv->cpsw;
1123 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1124 priv->emac_port + cpsw->data.dual_emac);
1127 static inline void cpsw_add_dual_emac_def_ale_entries(
1128 struct cpsw_priv *priv, struct cpsw_slave *slave,
1131 struct cpsw_common *cpsw = priv->cpsw;
1132 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1134 if (cpsw->version == CPSW_VERSION_1)
1135 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1137 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
1138 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1139 port_mask, port_mask, 0);
1140 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1141 port_mask, ALE_VLAN, slave->port_vlan, 0);
1142 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1143 HOST_PORT_NUM, ALE_VLAN |
1144 ALE_SECURE, slave->port_vlan);
1145 cpsw_ale_control_set(cpsw->ale, slave_port,
1146 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1149 static void soft_reset_slave(struct cpsw_slave *slave)
1153 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1154 soft_reset(name, &slave->sliver->soft_reset);
1157 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1160 struct cpsw_common *cpsw = priv->cpsw;
1162 soft_reset_slave(slave);
1164 /* setup priority mapping */
1165 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
1167 switch (cpsw->version) {
1168 case CPSW_VERSION_1:
1169 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1170 /* Increase RX FIFO size to 5 for supporting fullduplex
1174 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1175 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
1177 case CPSW_VERSION_2:
1178 case CPSW_VERSION_3:
1179 case CPSW_VERSION_4:
1180 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1181 /* Increase RX FIFO size to 5 for supporting fullduplex
1185 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1186 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
1190 /* setup max packet size, and mac address */
1191 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1192 cpsw_set_slave_mac(slave, priv);
1194 slave->mac_control = 0; /* no link yet */
1196 slave_port = cpsw_get_slave_port(slave->slave_num);
1198 if (cpsw->data.dual_emac)
1199 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1201 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1202 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1204 if (slave->data->phy_node) {
1205 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
1206 &cpsw_adjust_link, 0, slave->data->phy_if);
1208 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1209 slave->data->phy_node->full_name,
1214 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
1215 &cpsw_adjust_link, slave->data->phy_if);
1216 if (IS_ERR(slave->phy)) {
1218 "phy \"%s\" not found on slave %d, err %ld\n",
1219 slave->data->phy_id, slave->slave_num,
1220 PTR_ERR(slave->phy));
1226 phy_attached_info(slave->phy);
1228 phy_start(slave->phy);
1230 /* Configure GMII_SEL register */
1231 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1234 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1236 struct cpsw_common *cpsw = priv->cpsw;
1237 const int vlan = cpsw->data.default_vlan;
1240 int unreg_mcast_mask;
1242 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
1245 writel(vlan, &cpsw->host_port_regs->port_vlan);
1247 for (i = 0; i < cpsw->data.slaves; i++)
1248 slave_write(cpsw->slaves + i, vlan, reg);
1250 if (priv->ndev->flags & IFF_ALLMULTI)
1251 unreg_mcast_mask = ALE_ALL_PORTS;
1253 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1255 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
1256 ALE_ALL_PORTS, ALE_ALL_PORTS,
1260 static void cpsw_init_host_port(struct cpsw_priv *priv)
1264 struct cpsw_common *cpsw = priv->cpsw;
1266 /* soft reset the controller and initialize ale */
1267 soft_reset("cpsw", &cpsw->regs->soft_reset);
1268 cpsw_ale_start(cpsw->ale);
1270 /* switch to vlan unaware mode */
1271 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
1272 CPSW_ALE_VLAN_AWARE);
1273 control_reg = readl(&cpsw->regs->control);
1274 control_reg |= CPSW_VLAN_AWARE;
1275 writel(control_reg, &cpsw->regs->control);
1276 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1277 CPSW_FIFO_NORMAL_MODE;
1278 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1280 /* setup host port priority mapping */
1281 __raw_writel(CPDMA_TX_PRIORITY_MAP,
1282 &cpsw->host_port_regs->cpdma_tx_pri_map);
1283 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1285 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1286 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1288 if (!cpsw->data.dual_emac) {
1289 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1291 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1292 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1296 static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1298 struct cpsw_common *cpsw = priv->cpsw;
1299 struct sk_buff *skb;
1303 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1304 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxch[ch]);
1305 for (i = 0; i < ch_buf_num; i++) {
1306 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1307 cpsw->rx_packet_max,
1310 cpsw_err(priv, ifup, "cannot allocate skb\n");
1314 skb_set_queue_mapping(skb, ch);
1315 ret = cpdma_chan_submit(cpsw->rxch[ch], skb, skb->data,
1316 skb_tailroom(skb), 0);
1318 cpsw_err(priv, ifup,
1319 "cannot submit skb to channel %d rx, error %d\n",
1324 kmemleak_not_leak(skb);
1327 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1334 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1338 slave_port = cpsw_get_slave_port(slave->slave_num);
1342 phy_stop(slave->phy);
1343 phy_disconnect(slave->phy);
1345 cpsw_ale_control_set(cpsw->ale, slave_port,
1346 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1347 soft_reset_slave(slave);
1350 static int cpsw_ndo_open(struct net_device *ndev)
1352 struct cpsw_priv *priv = netdev_priv(ndev);
1353 struct cpsw_common *cpsw = priv->cpsw;
1357 ret = pm_runtime_get_sync(cpsw->dev);
1359 pm_runtime_put_noidle(cpsw->dev);
1363 if (!cpsw_common_res_usage_state(cpsw))
1364 cpsw_intr_disable(cpsw);
1365 netif_carrier_off(ndev);
1367 /* Notify the stack of the actual queue counts. */
1368 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1370 dev_err(priv->dev, "cannot set real number of tx queues\n");
1374 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1376 dev_err(priv->dev, "cannot set real number of rx queues\n");
1380 reg = cpsw->version;
1382 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1383 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1384 CPSW_RTL_VERSION(reg));
1386 /* initialize host and slave ports */
1387 if (!cpsw_common_res_usage_state(cpsw))
1388 cpsw_init_host_port(priv);
1389 for_each_slave(priv, cpsw_slave_open, priv);
1391 /* Add default VLAN */
1392 if (!cpsw->data.dual_emac)
1393 cpsw_add_default_vlan(priv);
1395 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
1396 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
1398 if (!cpsw_common_res_usage_state(cpsw)) {
1399 /* setup tx dma to fixed prio and zero offset */
1400 cpdma_control_set(cpsw->dma, CPDMA_TX_PRIO_FIXED, 1);
1401 cpdma_control_set(cpsw->dma, CPDMA_RX_BUFFER_OFFSET, 0);
1403 /* disable priority elevation */
1404 __raw_writel(0, &cpsw->regs->ptype);
1406 /* enable statistics collection only on all ports */
1407 __raw_writel(0x7, &cpsw->regs->stat_port_en);
1409 /* Enable internal fifo flow control */
1410 writel(0x7, &cpsw->regs->flow_control);
1412 napi_enable(&cpsw->napi_rx);
1413 napi_enable(&cpsw->napi_tx);
1415 if (cpsw->tx_irq_disabled) {
1416 cpsw->tx_irq_disabled = false;
1417 enable_irq(cpsw->irqs_table[1]);
1420 if (cpsw->rx_irq_disabled) {
1421 cpsw->rx_irq_disabled = false;
1422 enable_irq(cpsw->irqs_table[0]);
1425 ret = cpsw_fill_rx_channels(priv);
1429 if (cpts_register(cpsw->dev, cpsw->cpts,
1430 cpsw->data.cpts_clock_mult,
1431 cpsw->data.cpts_clock_shift))
1432 dev_err(priv->dev, "error registering cpts device\n");
1436 /* Enable Interrupt pacing if configured */
1437 if (cpsw->coal_intvl != 0) {
1438 struct ethtool_coalesce coal;
1440 coal.rx_coalesce_usecs = cpsw->coal_intvl;
1441 cpsw_set_coalesce(ndev, &coal);
1444 cpdma_ctlr_start(cpsw->dma);
1445 cpsw_intr_enable(cpsw);
1447 if (cpsw->data.dual_emac)
1448 cpsw->slaves[priv->emac_port].open_stat = true;
1450 netif_tx_start_all_queues(ndev);
1455 cpdma_ctlr_stop(cpsw->dma);
1456 for_each_slave(priv, cpsw_slave_stop, cpsw);
1457 pm_runtime_put_sync(cpsw->dev);
1458 netif_carrier_off(priv->ndev);
1462 static int cpsw_ndo_stop(struct net_device *ndev)
1464 struct cpsw_priv *priv = netdev_priv(ndev);
1465 struct cpsw_common *cpsw = priv->cpsw;
1467 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1468 netif_tx_stop_all_queues(priv->ndev);
1469 netif_carrier_off(priv->ndev);
1471 if (cpsw_common_res_usage_state(cpsw) <= 1) {
1472 napi_disable(&cpsw->napi_rx);
1473 napi_disable(&cpsw->napi_tx);
1474 cpts_unregister(cpsw->cpts);
1475 cpsw_intr_disable(cpsw);
1476 cpdma_ctlr_stop(cpsw->dma);
1477 cpsw_ale_stop(cpsw->ale);
1479 for_each_slave(priv, cpsw_slave_stop, cpsw);
1480 pm_runtime_put_sync(cpsw->dev);
1481 if (cpsw->data.dual_emac)
1482 cpsw->slaves[priv->emac_port].open_stat = false;
1486 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1487 struct net_device *ndev)
1489 struct cpsw_priv *priv = netdev_priv(ndev);
1490 struct cpsw_common *cpsw = priv->cpsw;
1491 struct netdev_queue *txq;
1492 struct cpdma_chan *txch;
1495 netif_trans_update(ndev);
1497 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1498 cpsw_err(priv, tx_err, "packet pad failed\n");
1499 ndev->stats.tx_dropped++;
1500 return NETDEV_TX_OK;
1503 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
1504 cpsw->cpts->tx_enable)
1505 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1507 skb_tx_timestamp(skb);
1509 q_idx = skb_get_queue_mapping(skb);
1510 if (q_idx >= cpsw->tx_ch_num)
1511 q_idx = q_idx % cpsw->tx_ch_num;
1513 txch = cpsw->txch[q_idx];
1514 ret = cpsw_tx_packet_submit(priv, skb, txch);
1515 if (unlikely(ret != 0)) {
1516 cpsw_err(priv, tx_err, "desc submit failed\n");
1520 /* If there is no more tx desc left free then we need to
1521 * tell the kernel to stop sending us tx frames.
1523 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1524 txq = netdev_get_tx_queue(ndev, q_idx);
1525 netif_tx_stop_queue(txq);
1528 return NETDEV_TX_OK;
1530 ndev->stats.tx_dropped++;
1531 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
1532 netif_tx_stop_queue(txq);
1533 return NETDEV_TX_BUSY;
1536 #ifdef CONFIG_TI_CPTS
1538 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
1540 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
1543 if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) {
1544 slave_write(slave, 0, CPSW1_TS_CTL);
1548 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1549 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1551 if (cpsw->cpts->tx_enable)
1552 ts_en |= CPSW_V1_TS_TX_EN;
1554 if (cpsw->cpts->rx_enable)
1555 ts_en |= CPSW_V1_TS_RX_EN;
1557 slave_write(slave, ts_en, CPSW1_TS_CTL);
1558 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1561 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1563 struct cpsw_slave *slave;
1564 struct cpsw_common *cpsw = priv->cpsw;
1567 if (cpsw->data.dual_emac)
1568 slave = &cpsw->slaves[priv->emac_port];
1570 slave = &cpsw->slaves[cpsw->data.active_slave];
1572 ctrl = slave_read(slave, CPSW2_CONTROL);
1573 switch (cpsw->version) {
1574 case CPSW_VERSION_2:
1575 ctrl &= ~CTRL_V2_ALL_TS_MASK;
1577 if (cpsw->cpts->tx_enable)
1578 ctrl |= CTRL_V2_TX_TS_BITS;
1580 if (cpsw->cpts->rx_enable)
1581 ctrl |= CTRL_V2_RX_TS_BITS;
1583 case CPSW_VERSION_3:
1585 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1587 if (cpsw->cpts->tx_enable)
1588 ctrl |= CTRL_V3_TX_TS_BITS;
1590 if (cpsw->cpts->rx_enable)
1591 ctrl |= CTRL_V3_RX_TS_BITS;
1595 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1597 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1598 slave_write(slave, ctrl, CPSW2_CONTROL);
1599 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
1602 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1604 struct cpsw_priv *priv = netdev_priv(dev);
1605 struct hwtstamp_config cfg;
1606 struct cpsw_common *cpsw = priv->cpsw;
1607 struct cpts *cpts = cpsw->cpts;
1609 if (cpsw->version != CPSW_VERSION_1 &&
1610 cpsw->version != CPSW_VERSION_2 &&
1611 cpsw->version != CPSW_VERSION_3)
1614 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1617 /* reserved for future extensions */
1621 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
1624 switch (cfg.rx_filter) {
1625 case HWTSTAMP_FILTER_NONE:
1626 cpts->rx_enable = 0;
1628 case HWTSTAMP_FILTER_ALL:
1629 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1630 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1631 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1633 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1634 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1635 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1636 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1637 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1638 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1639 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1640 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1641 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1642 cpts->rx_enable = 1;
1643 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1649 cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
1651 switch (cpsw->version) {
1652 case CPSW_VERSION_1:
1653 cpsw_hwtstamp_v1(cpsw);
1655 case CPSW_VERSION_2:
1656 case CPSW_VERSION_3:
1657 cpsw_hwtstamp_v2(priv);
1663 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1666 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1668 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1669 struct cpts *cpts = cpsw->cpts;
1670 struct hwtstamp_config cfg;
1672 if (cpsw->version != CPSW_VERSION_1 &&
1673 cpsw->version != CPSW_VERSION_2 &&
1674 cpsw->version != CPSW_VERSION_3)
1678 cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1679 cfg.rx_filter = (cpts->rx_enable ?
1680 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1682 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1685 #endif /*CONFIG_TI_CPTS*/
1687 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1689 struct cpsw_priv *priv = netdev_priv(dev);
1690 struct cpsw_common *cpsw = priv->cpsw;
1691 int slave_no = cpsw_slave_index(cpsw, priv);
1693 if (!netif_running(dev))
1697 #ifdef CONFIG_TI_CPTS
1699 return cpsw_hwtstamp_set(dev, req);
1701 return cpsw_hwtstamp_get(dev, req);
1705 if (!cpsw->slaves[slave_no].phy)
1707 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
1710 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1712 struct cpsw_priv *priv = netdev_priv(ndev);
1713 struct cpsw_common *cpsw = priv->cpsw;
1716 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
1717 ndev->stats.tx_errors++;
1718 cpsw_intr_disable(cpsw);
1719 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1720 cpdma_chan_stop(cpsw->txch[ch]);
1721 cpdma_chan_start(cpsw->txch[ch]);
1724 cpsw_intr_enable(cpsw);
1727 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1729 struct cpsw_priv *priv = netdev_priv(ndev);
1730 struct sockaddr *addr = (struct sockaddr *)p;
1731 struct cpsw_common *cpsw = priv->cpsw;
1736 if (!is_valid_ether_addr(addr->sa_data))
1737 return -EADDRNOTAVAIL;
1739 ret = pm_runtime_get_sync(cpsw->dev);
1741 pm_runtime_put_noidle(cpsw->dev);
1745 if (cpsw->data.dual_emac) {
1746 vid = cpsw->slaves[priv->emac_port].port_vlan;
1750 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1752 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1755 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1756 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1757 for_each_slave(priv, cpsw_set_slave_mac, priv);
1759 pm_runtime_put(cpsw->dev);
1764 #ifdef CONFIG_NET_POLL_CONTROLLER
1765 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1767 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1769 cpsw_intr_disable(cpsw);
1770 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1771 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1772 cpsw_intr_enable(cpsw);
1776 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1780 int unreg_mcast_mask = 0;
1782 struct cpsw_common *cpsw = priv->cpsw;
1784 if (cpsw->data.dual_emac) {
1785 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
1787 if (priv->ndev->flags & IFF_ALLMULTI)
1788 unreg_mcast_mask = port_mask;
1790 port_mask = ALE_ALL_PORTS;
1792 if (priv->ndev->flags & IFF_ALLMULTI)
1793 unreg_mcast_mask = ALE_ALL_PORTS;
1795 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1798 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
1803 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1804 HOST_PORT_NUM, ALE_VLAN, vid);
1808 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1809 port_mask, ALE_VLAN, vid, 0);
1811 goto clean_vlan_ucast;
1815 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1816 HOST_PORT_NUM, ALE_VLAN, vid);
1818 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1822 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
1823 __be16 proto, u16 vid)
1825 struct cpsw_priv *priv = netdev_priv(ndev);
1826 struct cpsw_common *cpsw = priv->cpsw;
1829 if (vid == cpsw->data.default_vlan)
1832 ret = pm_runtime_get_sync(cpsw->dev);
1834 pm_runtime_put_noidle(cpsw->dev);
1838 if (cpsw->data.dual_emac) {
1839 /* In dual EMAC, reserved VLAN id should not be used for
1840 * creating VLAN interfaces as this can break the dual
1841 * EMAC port separation
1845 for (i = 0; i < cpsw->data.slaves; i++) {
1846 if (vid == cpsw->slaves[i].port_vlan)
1851 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1852 ret = cpsw_add_vlan_ale_entry(priv, vid);
1854 pm_runtime_put(cpsw->dev);
1858 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1859 __be16 proto, u16 vid)
1861 struct cpsw_priv *priv = netdev_priv(ndev);
1862 struct cpsw_common *cpsw = priv->cpsw;
1865 if (vid == cpsw->data.default_vlan)
1868 ret = pm_runtime_get_sync(cpsw->dev);
1870 pm_runtime_put_noidle(cpsw->dev);
1874 if (cpsw->data.dual_emac) {
1877 for (i = 0; i < cpsw->data.slaves; i++) {
1878 if (vid == cpsw->slaves[i].port_vlan)
1883 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1884 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1888 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1889 HOST_PORT_NUM, ALE_VLAN, vid);
1893 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1895 pm_runtime_put(cpsw->dev);
1899 static const struct net_device_ops cpsw_netdev_ops = {
1900 .ndo_open = cpsw_ndo_open,
1901 .ndo_stop = cpsw_ndo_stop,
1902 .ndo_start_xmit = cpsw_ndo_start_xmit,
1903 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
1904 .ndo_do_ioctl = cpsw_ndo_ioctl,
1905 .ndo_validate_addr = eth_validate_addr,
1906 .ndo_change_mtu = eth_change_mtu,
1907 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
1908 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
1909 #ifdef CONFIG_NET_POLL_CONTROLLER
1910 .ndo_poll_controller = cpsw_ndo_poll_controller,
1912 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1913 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
1916 static int cpsw_get_regs_len(struct net_device *ndev)
1918 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1920 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
1923 static void cpsw_get_regs(struct net_device *ndev,
1924 struct ethtool_regs *regs, void *p)
1927 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1929 /* update CPSW IP version */
1930 regs->version = cpsw->version;
1932 cpsw_ale_dump(cpsw->ale, reg);
1935 static void cpsw_get_drvinfo(struct net_device *ndev,
1936 struct ethtool_drvinfo *info)
1938 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1939 struct platform_device *pdev = to_platform_device(cpsw->dev);
1941 strlcpy(info->driver, "cpsw", sizeof(info->driver));
1942 strlcpy(info->version, "1.0", sizeof(info->version));
1943 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1946 static u32 cpsw_get_msglevel(struct net_device *ndev)
1948 struct cpsw_priv *priv = netdev_priv(ndev);
1949 return priv->msg_enable;
1952 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1954 struct cpsw_priv *priv = netdev_priv(ndev);
1955 priv->msg_enable = value;
1958 static int cpsw_get_ts_info(struct net_device *ndev,
1959 struct ethtool_ts_info *info)
1961 #ifdef CONFIG_TI_CPTS
1962 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1964 info->so_timestamping =
1965 SOF_TIMESTAMPING_TX_HARDWARE |
1966 SOF_TIMESTAMPING_TX_SOFTWARE |
1967 SOF_TIMESTAMPING_RX_HARDWARE |
1968 SOF_TIMESTAMPING_RX_SOFTWARE |
1969 SOF_TIMESTAMPING_SOFTWARE |
1970 SOF_TIMESTAMPING_RAW_HARDWARE;
1971 info->phc_index = cpsw->cpts->phc_index;
1973 (1 << HWTSTAMP_TX_OFF) |
1974 (1 << HWTSTAMP_TX_ON);
1976 (1 << HWTSTAMP_FILTER_NONE) |
1977 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1979 info->so_timestamping =
1980 SOF_TIMESTAMPING_TX_SOFTWARE |
1981 SOF_TIMESTAMPING_RX_SOFTWARE |
1982 SOF_TIMESTAMPING_SOFTWARE;
1983 info->phc_index = -1;
1985 info->rx_filters = 0;
1990 static int cpsw_get_settings(struct net_device *ndev,
1991 struct ethtool_cmd *ecmd)
1993 struct cpsw_priv *priv = netdev_priv(ndev);
1994 struct cpsw_common *cpsw = priv->cpsw;
1995 int slave_no = cpsw_slave_index(cpsw, priv);
1997 if (cpsw->slaves[slave_no].phy)
1998 return phy_ethtool_gset(cpsw->slaves[slave_no].phy, ecmd);
2003 static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
2005 struct cpsw_priv *priv = netdev_priv(ndev);
2006 struct cpsw_common *cpsw = priv->cpsw;
2007 int slave_no = cpsw_slave_index(cpsw, priv);
2009 if (cpsw->slaves[slave_no].phy)
2010 return phy_ethtool_sset(cpsw->slaves[slave_no].phy, ecmd);
2015 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2017 struct cpsw_priv *priv = netdev_priv(ndev);
2018 struct cpsw_common *cpsw = priv->cpsw;
2019 int slave_no = cpsw_slave_index(cpsw, priv);
2024 if (cpsw->slaves[slave_no].phy)
2025 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2028 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2030 struct cpsw_priv *priv = netdev_priv(ndev);
2031 struct cpsw_common *cpsw = priv->cpsw;
2032 int slave_no = cpsw_slave_index(cpsw, priv);
2034 if (cpsw->slaves[slave_no].phy)
2035 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2040 static void cpsw_get_pauseparam(struct net_device *ndev,
2041 struct ethtool_pauseparam *pause)
2043 struct cpsw_priv *priv = netdev_priv(ndev);
2045 pause->autoneg = AUTONEG_DISABLE;
2046 pause->rx_pause = priv->rx_pause ? true : false;
2047 pause->tx_pause = priv->tx_pause ? true : false;
2050 static int cpsw_set_pauseparam(struct net_device *ndev,
2051 struct ethtool_pauseparam *pause)
2053 struct cpsw_priv *priv = netdev_priv(ndev);
2056 priv->rx_pause = pause->rx_pause ? true : false;
2057 priv->tx_pause = pause->tx_pause ? true : false;
2059 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
2063 static int cpsw_ethtool_op_begin(struct net_device *ndev)
2065 struct cpsw_priv *priv = netdev_priv(ndev);
2066 struct cpsw_common *cpsw = priv->cpsw;
2069 ret = pm_runtime_get_sync(cpsw->dev);
2071 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
2072 pm_runtime_put_noidle(cpsw->dev);
2078 static void cpsw_ethtool_op_complete(struct net_device *ndev)
2080 struct cpsw_priv *priv = netdev_priv(ndev);
2083 ret = pm_runtime_put(priv->cpsw->dev);
2085 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2088 static void cpsw_get_channels(struct net_device *ndev,
2089 struct ethtool_channels *ch)
2091 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2093 ch->max_combined = 0;
2094 ch->max_rx = CPSW_MAX_QUEUES;
2095 ch->max_tx = CPSW_MAX_QUEUES;
2097 ch->other_count = 0;
2098 ch->rx_count = cpsw->rx_ch_num;
2099 ch->tx_count = cpsw->tx_ch_num;
2100 ch->combined_count = 0;
2103 static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2104 struct ethtool_channels *ch)
2106 if (ch->combined_count)
2109 /* verify we have at least one channel in each direction */
2110 if (!ch->rx_count || !ch->tx_count)
2113 if (ch->rx_count > cpsw->data.channels ||
2114 ch->tx_count > cpsw->data.channels)
2120 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2122 int (*poll)(struct napi_struct *, int);
2123 struct cpsw_common *cpsw = priv->cpsw;
2124 void (*handler)(void *, int, int);
2125 struct cpdma_chan **chan;
2129 ch = &cpsw->rx_ch_num;
2131 handler = cpsw_rx_handler;
2132 poll = cpsw_rx_poll;
2134 ch = &cpsw->tx_ch_num;
2136 handler = cpsw_tx_handler;
2137 poll = cpsw_tx_poll;
2140 while (*ch < ch_num) {
2141 chan[*ch] = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
2143 if (IS_ERR(chan[*ch]))
2144 return PTR_ERR(chan[*ch]);
2149 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2150 (rx ? "rx" : "tx"));
2154 while (*ch > ch_num) {
2157 ret = cpdma_chan_destroy(chan[*ch]);
2161 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2162 (rx ? "rx" : "tx"));
2168 static int cpsw_update_channels(struct cpsw_priv *priv,
2169 struct ethtool_channels *ch)
2173 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2177 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2184 static int cpsw_set_channels(struct net_device *ndev,
2185 struct ethtool_channels *chs)
2187 struct cpsw_priv *priv = netdev_priv(ndev);
2188 struct cpsw_common *cpsw = priv->cpsw;
2189 struct cpsw_slave *slave;
2192 ret = cpsw_check_ch_settings(cpsw, chs);
2196 /* Disable NAPI scheduling */
2197 cpsw_intr_disable(cpsw);
2199 /* Stop all transmit queues for every network device.
2200 * Disable re-using rx descriptors with dormant_on.
2202 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2203 if (!(slave->ndev && netif_running(slave->ndev)))
2206 netif_tx_stop_all_queues(slave->ndev);
2207 netif_dormant_on(slave->ndev);
2210 /* Handle rest of tx packets and stop cpdma channels */
2211 cpdma_ctlr_stop(cpsw->dma);
2212 ret = cpsw_update_channels(priv, chs);
2216 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2217 if (!(slave->ndev && netif_running(slave->ndev)))
2220 /* Inform stack about new count of queues */
2221 ret = netif_set_real_num_tx_queues(slave->ndev,
2224 dev_err(priv->dev, "cannot set real number of tx queues\n");
2228 ret = netif_set_real_num_rx_queues(slave->ndev,
2231 dev_err(priv->dev, "cannot set real number of rx queues\n");
2235 /* Enable rx packets handling */
2236 netif_dormant_off(slave->ndev);
2239 if (cpsw_common_res_usage_state(cpsw)) {
2240 ret = cpsw_fill_rx_channels(priv);
2244 /* After this receive is started */
2245 cpdma_ctlr_start(cpsw->dma);
2246 cpsw_intr_enable(cpsw);
2249 /* Resume transmit for every affected interface */
2250 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2251 if (!(slave->ndev && netif_running(slave->ndev)))
2253 netif_tx_start_all_queues(slave->ndev);
2257 dev_err(priv->dev, "cannot update channels number, closing device\n");
2262 static const struct ethtool_ops cpsw_ethtool_ops = {
2263 .get_drvinfo = cpsw_get_drvinfo,
2264 .get_msglevel = cpsw_get_msglevel,
2265 .set_msglevel = cpsw_set_msglevel,
2266 .get_link = ethtool_op_get_link,
2267 .get_ts_info = cpsw_get_ts_info,
2268 .get_settings = cpsw_get_settings,
2269 .set_settings = cpsw_set_settings,
2270 .get_coalesce = cpsw_get_coalesce,
2271 .set_coalesce = cpsw_set_coalesce,
2272 .get_sset_count = cpsw_get_sset_count,
2273 .get_strings = cpsw_get_strings,
2274 .get_ethtool_stats = cpsw_get_ethtool_stats,
2275 .get_pauseparam = cpsw_get_pauseparam,
2276 .set_pauseparam = cpsw_set_pauseparam,
2277 .get_wol = cpsw_get_wol,
2278 .set_wol = cpsw_set_wol,
2279 .get_regs_len = cpsw_get_regs_len,
2280 .get_regs = cpsw_get_regs,
2281 .begin = cpsw_ethtool_op_begin,
2282 .complete = cpsw_ethtool_op_complete,
2283 .get_channels = cpsw_get_channels,
2284 .set_channels = cpsw_set_channels,
2287 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
2288 u32 slave_reg_ofs, u32 sliver_reg_ofs)
2290 void __iomem *regs = cpsw->regs;
2291 int slave_num = slave->slave_num;
2292 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
2295 slave->regs = regs + slave_reg_ofs;
2296 slave->sliver = regs + sliver_reg_ofs;
2297 slave->port_vlan = data->dual_emac_res_vlan;
2300 static int cpsw_probe_dt(struct cpsw_platform_data *data,
2301 struct platform_device *pdev)
2303 struct device_node *node = pdev->dev.of_node;
2304 struct device_node *slave_node;
2311 if (of_property_read_u32(node, "slaves", &prop)) {
2312 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
2315 data->slaves = prop;
2317 if (of_property_read_u32(node, "active_slave", &prop)) {
2318 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
2321 data->active_slave = prop;
2323 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
2324 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
2327 data->cpts_clock_mult = prop;
2329 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
2330 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
2333 data->cpts_clock_shift = prop;
2335 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2336 * sizeof(struct cpsw_slave_data),
2338 if (!data->slave_data)
2341 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
2342 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
2345 data->channels = prop;
2347 if (of_property_read_u32(node, "ale_entries", &prop)) {
2348 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
2351 data->ale_entries = prop;
2353 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
2354 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
2357 data->bd_ram_size = prop;
2359 if (of_property_read_u32(node, "mac_control", &prop)) {
2360 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
2363 data->mac_control = prop;
2365 if (of_property_read_bool(node, "dual_emac"))
2366 data->dual_emac = 1;
2369 * Populate all the child nodes here...
2371 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2372 /* We do not want to force this, as in some cases may not have child */
2374 dev_warn(&pdev->dev, "Doesn't have any child node\n");
2376 for_each_available_child_of_node(node, slave_node) {
2377 struct cpsw_slave_data *slave_data = data->slave_data + i;
2378 const void *mac_addr = NULL;
2382 /* This is no slave child node, continue */
2383 if (strcmp(slave_node->name, "slave"))
2386 slave_data->phy_node = of_parse_phandle(slave_node,
2388 parp = of_get_property(slave_node, "phy_id", &lenp);
2389 if (slave_data->phy_node) {
2391 "slave[%d] using phy-handle=\"%s\"\n",
2392 i, slave_data->phy_node->full_name);
2393 } else if (of_phy_is_fixed_link(slave_node)) {
2394 /* In the case of a fixed PHY, the DT node associated
2395 * to the PHY is the Ethernet MAC DT node.
2397 ret = of_phy_register_fixed_link(slave_node);
2399 if (ret != -EPROBE_DEFER)
2400 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
2403 slave_data->phy_node = of_node_get(slave_node);
2406 struct device_node *mdio_node;
2407 struct platform_device *mdio;
2409 if (lenp != (sizeof(__be32) * 2)) {
2410 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2413 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2414 phyid = be32_to_cpup(parp+1);
2415 mdio = of_find_device_by_node(mdio_node);
2416 of_node_put(mdio_node);
2418 dev_err(&pdev->dev, "Missing mdio platform device\n");
2421 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2422 PHY_ID_FMT, mdio->name, phyid);
2423 put_device(&mdio->dev);
2426 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2430 slave_data->phy_if = of_get_phy_mode(slave_node);
2431 if (slave_data->phy_if < 0) {
2432 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2434 return slave_data->phy_if;
2438 mac_addr = of_get_mac_address(slave_node);
2440 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
2442 ret = ti_cm_get_macid(&pdev->dev, i,
2443 slave_data->mac_addr);
2447 if (data->dual_emac) {
2448 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
2450 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
2451 slave_data->dual_emac_res_vlan = i+1;
2452 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2453 slave_data->dual_emac_res_vlan, i);
2455 slave_data->dual_emac_res_vlan = prop;
2460 if (i == data->slaves)
2467 static void cpsw_remove_dt(struct platform_device *pdev)
2469 struct net_device *ndev = platform_get_drvdata(pdev);
2470 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2471 struct cpsw_platform_data *data = &cpsw->data;
2472 struct device_node *node = pdev->dev.of_node;
2473 struct device_node *slave_node;
2476 for_each_available_child_of_node(node, slave_node) {
2477 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2479 if (strcmp(slave_node->name, "slave"))
2482 if (of_phy_is_fixed_link(slave_node))
2483 of_phy_deregister_fixed_link(slave_node);
2485 of_node_put(slave_data->phy_node);
2488 if (i == data->slaves)
2492 of_platform_depopulate(&pdev->dev);
2495 static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
2497 struct cpsw_common *cpsw = priv->cpsw;
2498 struct cpsw_platform_data *data = &cpsw->data;
2499 struct net_device *ndev;
2500 struct cpsw_priv *priv_sl2;
2503 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2505 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
2509 priv_sl2 = netdev_priv(ndev);
2510 priv_sl2->cpsw = cpsw;
2511 priv_sl2->ndev = ndev;
2512 priv_sl2->dev = &ndev->dev;
2513 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2515 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2516 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2518 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2519 priv_sl2->mac_addr);
2521 random_ether_addr(priv_sl2->mac_addr);
2522 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2523 priv_sl2->mac_addr);
2525 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2527 priv_sl2->emac_port = 1;
2528 cpsw->slaves[1].ndev = ndev;
2529 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2531 ndev->netdev_ops = &cpsw_netdev_ops;
2532 ndev->ethtool_ops = &cpsw_ethtool_ops;
2534 /* register the network device */
2535 SET_NETDEV_DEV(ndev, cpsw->dev);
2536 ret = register_netdev(ndev);
2538 dev_err(cpsw->dev, "cpsw: error registering net device\n");
2546 #define CPSW_QUIRK_IRQ BIT(0)
2548 static struct platform_device_id cpsw_devtype[] = {
2550 /* keep it for existing comaptibles */
2552 .driver_data = CPSW_QUIRK_IRQ,
2554 .name = "am335x-cpsw",
2555 .driver_data = CPSW_QUIRK_IRQ,
2557 .name = "am4372-cpsw",
2560 .name = "dra7-cpsw",
2566 MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2575 static const struct of_device_id cpsw_of_mtable[] = {
2576 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2577 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2578 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2579 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2582 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2584 static int cpsw_probe(struct platform_device *pdev)
2587 struct cpsw_platform_data *data;
2588 struct net_device *ndev;
2589 struct cpsw_priv *priv;
2590 struct cpdma_params dma_params;
2591 struct cpsw_ale_params ale_params;
2592 void __iomem *ss_regs;
2593 struct resource *res, *ss_res;
2594 const struct of_device_id *of_id;
2595 struct gpio_descs *mode;
2596 u32 slave_offset, sliver_offset, slave_size;
2597 struct cpsw_common *cpsw;
2601 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
2605 cpsw->dev = &pdev->dev;
2607 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2609 dev_err(&pdev->dev, "error allocating net_device\n");
2613 platform_set_drvdata(pdev, ndev);
2614 priv = netdev_priv(ndev);
2617 priv->dev = &ndev->dev;
2618 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2619 cpsw->rx_packet_max = max(rx_packet_max, 128);
2620 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2622 dev_err(&pdev->dev, "error allocating cpts\n");
2624 goto clean_ndev_ret;
2627 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2629 ret = PTR_ERR(mode);
2630 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2631 goto clean_ndev_ret;
2635 * This may be required here for child devices.
2637 pm_runtime_enable(&pdev->dev);
2639 /* Select default pin state */
2640 pinctrl_pm_select_default_state(&pdev->dev);
2642 /* Need to enable clocks with runtime PM api to access module
2645 ret = pm_runtime_get_sync(&pdev->dev);
2647 pm_runtime_put_noidle(&pdev->dev);
2648 goto clean_runtime_disable_ret;
2651 ret = cpsw_probe_dt(&cpsw->data, pdev);
2656 cpsw->rx_ch_num = 1;
2657 cpsw->tx_ch_num = 1;
2659 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2660 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
2661 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
2663 eth_random_addr(priv->mac_addr);
2664 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
2667 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2669 cpsw->slaves = devm_kzalloc(&pdev->dev,
2670 sizeof(struct cpsw_slave) * data->slaves,
2672 if (!cpsw->slaves) {
2676 for (i = 0; i < data->slaves; i++)
2677 cpsw->slaves[i].slave_num = i;
2679 cpsw->slaves[0].ndev = ndev;
2680 priv->emac_port = 0;
2682 clk = devm_clk_get(&pdev->dev, "fck");
2684 dev_err(priv->dev, "fck is not found\n");
2688 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
2690 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2691 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2692 if (IS_ERR(ss_regs)) {
2693 ret = PTR_ERR(ss_regs);
2696 cpsw->regs = ss_regs;
2698 cpsw->version = readl(&cpsw->regs->id_ver);
2700 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2701 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2702 if (IS_ERR(cpsw->wr_regs)) {
2703 ret = PTR_ERR(cpsw->wr_regs);
2707 memset(&dma_params, 0, sizeof(dma_params));
2708 memset(&ale_params, 0, sizeof(ale_params));
2710 switch (cpsw->version) {
2711 case CPSW_VERSION_1:
2712 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
2713 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
2714 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
2715 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2716 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2717 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2718 slave_offset = CPSW1_SLAVE_OFFSET;
2719 slave_size = CPSW1_SLAVE_SIZE;
2720 sliver_offset = CPSW1_SLIVER_OFFSET;
2721 dma_params.desc_mem_phys = 0;
2723 case CPSW_VERSION_2:
2724 case CPSW_VERSION_3:
2725 case CPSW_VERSION_4:
2726 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
2727 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
2728 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
2729 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2730 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2731 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2732 slave_offset = CPSW2_SLAVE_OFFSET;
2733 slave_size = CPSW2_SLAVE_SIZE;
2734 sliver_offset = CPSW2_SLIVER_OFFSET;
2735 dma_params.desc_mem_phys =
2736 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
2739 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
2743 for (i = 0; i < cpsw->data.slaves; i++) {
2744 struct cpsw_slave *slave = &cpsw->slaves[i];
2746 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
2747 slave_offset += slave_size;
2748 sliver_offset += SLIVER_SIZE;
2751 dma_params.dev = &pdev->dev;
2752 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2753 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2754 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2755 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2756 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
2758 dma_params.num_chan = data->channels;
2759 dma_params.has_soft_reset = true;
2760 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2761 dma_params.desc_mem_size = data->bd_ram_size;
2762 dma_params.desc_align = 16;
2763 dma_params.has_ext_regs = true;
2764 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
2766 cpsw->dma = cpdma_ctlr_create(&dma_params);
2768 dev_err(priv->dev, "error initializing dma\n");
2773 cpsw->txch[0] = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
2774 cpsw->rxch[0] = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
2775 if (WARN_ON(!cpsw->rxch[0] || !cpsw->txch[0])) {
2776 dev_err(priv->dev, "error initializing dma channels\n");
2781 ale_params.dev = &ndev->dev;
2782 ale_params.ale_ageout = ale_ageout;
2783 ale_params.ale_entries = data->ale_entries;
2784 ale_params.ale_ports = data->slaves;
2786 cpsw->ale = cpsw_ale_create(&ale_params);
2788 dev_err(priv->dev, "error initializing ale engine\n");
2793 ndev->irq = platform_get_irq(pdev, 1);
2794 if (ndev->irq < 0) {
2795 dev_err(priv->dev, "error getting irq resource\n");
2800 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
2802 pdev->id_entry = of_id->data;
2803 if (pdev->id_entry->driver_data)
2804 cpsw->quirk_irq = true;
2807 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
2808 * MISC IRQs which are always kept disabled with this driver so
2809 * we will not request them.
2811 * If anyone wants to implement support for those, make sure to
2812 * first request and append them to irqs_table array.
2816 irq = platform_get_irq(pdev, 1);
2822 cpsw->irqs_table[0] = irq;
2823 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
2824 0, dev_name(&pdev->dev), cpsw);
2826 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2831 irq = platform_get_irq(pdev, 2);
2837 cpsw->irqs_table[1] = irq;
2838 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
2839 0, dev_name(&pdev->dev), cpsw);
2841 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2845 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2847 ndev->netdev_ops = &cpsw_netdev_ops;
2848 ndev->ethtool_ops = &cpsw_ethtool_ops;
2849 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
2850 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
2852 /* register the network device */
2853 SET_NETDEV_DEV(ndev, &pdev->dev);
2854 ret = register_netdev(ndev);
2856 dev_err(priv->dev, "error registering net device\n");
2861 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
2862 &ss_res->start, ndev->irq);
2864 if (cpsw->data.dual_emac) {
2865 ret = cpsw_probe_dual_emac(priv);
2867 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
2868 goto clean_unregister_netdev_ret;
2872 pm_runtime_put(&pdev->dev);
2876 clean_unregister_netdev_ret:
2877 unregister_netdev(ndev);
2879 cpsw_ale_destroy(cpsw->ale);
2881 cpdma_ctlr_destroy(cpsw->dma);
2883 cpsw_remove_dt(pdev);
2884 pm_runtime_put_sync(&pdev->dev);
2885 clean_runtime_disable_ret:
2886 pm_runtime_disable(&pdev->dev);
2888 free_netdev(priv->ndev);
2892 static int cpsw_remove(struct platform_device *pdev)
2894 struct net_device *ndev = platform_get_drvdata(pdev);
2895 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2898 ret = pm_runtime_get_sync(&pdev->dev);
2900 pm_runtime_put_noidle(&pdev->dev);
2904 if (cpsw->data.dual_emac)
2905 unregister_netdev(cpsw->slaves[1].ndev);
2906 unregister_netdev(ndev);
2908 cpsw_ale_destroy(cpsw->ale);
2909 cpdma_ctlr_destroy(cpsw->dma);
2910 cpsw_remove_dt(pdev);
2911 pm_runtime_put_sync(&pdev->dev);
2912 pm_runtime_disable(&pdev->dev);
2913 if (cpsw->data.dual_emac)
2914 free_netdev(cpsw->slaves[1].ndev);
2919 #ifdef CONFIG_PM_SLEEP
2920 static int cpsw_suspend(struct device *dev)
2922 struct platform_device *pdev = to_platform_device(dev);
2923 struct net_device *ndev = platform_get_drvdata(pdev);
2924 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2926 if (cpsw->data.dual_emac) {
2929 for (i = 0; i < cpsw->data.slaves; i++) {
2930 if (netif_running(cpsw->slaves[i].ndev))
2931 cpsw_ndo_stop(cpsw->slaves[i].ndev);
2934 if (netif_running(ndev))
2935 cpsw_ndo_stop(ndev);
2938 /* Select sleep pin state */
2939 pinctrl_pm_select_sleep_state(dev);
2944 static int cpsw_resume(struct device *dev)
2946 struct platform_device *pdev = to_platform_device(dev);
2947 struct net_device *ndev = platform_get_drvdata(pdev);
2948 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2950 /* Select default pin state */
2951 pinctrl_pm_select_default_state(dev);
2953 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2955 if (cpsw->data.dual_emac) {
2958 for (i = 0; i < cpsw->data.slaves; i++) {
2959 if (netif_running(cpsw->slaves[i].ndev))
2960 cpsw_ndo_open(cpsw->slaves[i].ndev);
2963 if (netif_running(ndev))
2964 cpsw_ndo_open(ndev);
2972 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2974 static struct platform_driver cpsw_driver = {
2978 .of_match_table = cpsw_of_mtable,
2980 .probe = cpsw_probe,
2981 .remove = cpsw_remove,
2984 module_platform_driver(cpsw_driver);
2986 MODULE_LICENSE("GPL");
2987 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
2988 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
2989 MODULE_DESCRIPTION("TI CPSW Ethernet driver");