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/phy/phy.h>
30 #include <linux/workqueue.h>
31 #include <linux/delay.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/gpio/consumer.h>
35 #include <linux/of_mdio.h>
36 #include <linux/of_net.h>
37 #include <linux/of_device.h>
38 #include <linux/if_vlan.h>
39 #include <linux/kmemleak.h>
40 #include <linux/sys_soc.h>
42 #include <linux/pinctrl/consumer.h>
43 #include <net/pkt_cls.h>
48 #include "davinci_cpdma.h"
50 #include <net/pkt_sched.h>
52 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
53 NETIF_MSG_DRV | NETIF_MSG_LINK | \
54 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
55 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
56 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
57 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
58 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
61 #define cpsw_info(priv, type, format, ...) \
63 if (netif_msg_##type(priv) && net_ratelimit()) \
64 dev_info(priv->dev, format, ## __VA_ARGS__); \
67 #define cpsw_err(priv, type, format, ...) \
69 if (netif_msg_##type(priv) && net_ratelimit()) \
70 dev_err(priv->dev, format, ## __VA_ARGS__); \
73 #define cpsw_dbg(priv, type, format, ...) \
75 if (netif_msg_##type(priv) && net_ratelimit()) \
76 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
79 #define cpsw_notice(priv, type, format, ...) \
81 if (netif_msg_##type(priv) && net_ratelimit()) \
82 dev_notice(priv->dev, format, ## __VA_ARGS__); \
85 #define ALE_ALL_PORTS 0x7
87 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
88 #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
89 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
91 #define CPSW_VERSION_1 0x19010a
92 #define CPSW_VERSION_2 0x19010c
93 #define CPSW_VERSION_3 0x19010f
94 #define CPSW_VERSION_4 0x190112
96 #define HOST_PORT_NUM 0
97 #define CPSW_ALE_PORTS_NUM 3
98 #define SLIVER_SIZE 0x40
100 #define CPSW1_HOST_PORT_OFFSET 0x028
101 #define CPSW1_SLAVE_OFFSET 0x050
102 #define CPSW1_SLAVE_SIZE 0x040
103 #define CPSW1_CPDMA_OFFSET 0x100
104 #define CPSW1_STATERAM_OFFSET 0x200
105 #define CPSW1_HW_STATS 0x400
106 #define CPSW1_CPTS_OFFSET 0x500
107 #define CPSW1_ALE_OFFSET 0x600
108 #define CPSW1_SLIVER_OFFSET 0x700
110 #define CPSW2_HOST_PORT_OFFSET 0x108
111 #define CPSW2_SLAVE_OFFSET 0x200
112 #define CPSW2_SLAVE_SIZE 0x100
113 #define CPSW2_CPDMA_OFFSET 0x800
114 #define CPSW2_HW_STATS 0x900
115 #define CPSW2_STATERAM_OFFSET 0xa00
116 #define CPSW2_CPTS_OFFSET 0xc00
117 #define CPSW2_ALE_OFFSET 0xd00
118 #define CPSW2_SLIVER_OFFSET 0xd80
119 #define CPSW2_BD_OFFSET 0x2000
121 #define CPDMA_RXTHRESH 0x0c0
122 #define CPDMA_RXFREE 0x0e0
123 #define CPDMA_TXHDP 0x00
124 #define CPDMA_RXHDP 0x20
125 #define CPDMA_TXCP 0x40
126 #define CPDMA_RXCP 0x60
128 #define CPSW_POLL_WEIGHT 64
129 #define CPSW_RX_VLAN_ENCAP_HDR_SIZE 4
130 #define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN)
131 #define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN +\
133 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
135 #define RX_PRIORITY_MAPPING 0x76543210
136 #define TX_PRIORITY_MAPPING 0x33221100
137 #define CPDMA_TX_PRIORITY_MAP 0x76543210
139 #define CPSW_VLAN_AWARE BIT(1)
140 #define CPSW_RX_VLAN_ENCAP BIT(2)
141 #define CPSW_ALE_VLAN_AWARE 1
143 #define CPSW_FIFO_NORMAL_MODE (0 << 16)
144 #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
145 #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
147 #define CPSW_INTPACEEN (0x3f << 16)
148 #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
149 #define CPSW_CMINTMAX_CNT 63
150 #define CPSW_CMINTMIN_CNT 2
151 #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
152 #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
154 #define cpsw_slave_index(cpsw, priv) \
155 ((cpsw->data.dual_emac) ? priv->emac_port : \
156 cpsw->data.active_slave)
158 #define CPSW_MAX_QUEUES 8
159 #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
160 #define CPSW_FIFO_QUEUE_TYPE_SHIFT 16
161 #define CPSW_FIFO_SHAPE_EN_SHIFT 16
162 #define CPSW_FIFO_RATE_EN_SHIFT 20
163 #define CPSW_TC_NUM 4
164 #define CPSW_FIFO_SHAPERS_NUM (CPSW_TC_NUM - 1)
165 #define CPSW_PCT_MASK 0x7f
167 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29
168 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0)
169 #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT 16
170 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT 8
171 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK GENMASK(1, 0)
173 CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
174 CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
175 CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
176 CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
179 static int debug_level;
180 module_param(debug_level, int, 0);
181 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
183 static int ale_ageout = 10;
184 module_param(ale_ageout, int, 0);
185 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
187 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
188 module_param(rx_packet_max, int, 0);
189 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
191 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
192 module_param(descs_pool_size, int, 0444);
193 MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
195 struct cpsw_wr_regs {
215 struct cpsw_ss_regs {
232 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
233 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
234 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
235 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
236 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
237 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
238 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
239 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
242 #define CPSW2_CONTROL 0x00 /* Control Register */
243 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
244 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
245 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
246 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
247 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
248 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
250 /* CPSW_PORT_V1 and V2 */
251 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
252 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
253 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
255 /* CPSW_PORT_V2 only */
256 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
257 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
258 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
259 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
260 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
261 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
262 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
263 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
265 /* Bit definitions for the CPSW2_CONTROL register */
266 #define PASS_PRI_TAGGED BIT(24) /* Pass Priority Tagged */
267 #define VLAN_LTYPE2_EN BIT(21) /* VLAN LTYPE 2 enable */
268 #define VLAN_LTYPE1_EN BIT(20) /* VLAN LTYPE 1 enable */
269 #define DSCP_PRI_EN BIT(16) /* DSCP Priority Enable */
270 #define TS_107 BIT(15) /* Tyme Sync Dest IP Address 107 */
271 #define TS_320 BIT(14) /* Time Sync Dest Port 320 enable */
272 #define TS_319 BIT(13) /* Time Sync Dest Port 319 enable */
273 #define TS_132 BIT(12) /* Time Sync Dest IP Addr 132 enable */
274 #define TS_131 BIT(11) /* Time Sync Dest IP Addr 131 enable */
275 #define TS_130 BIT(10) /* Time Sync Dest IP Addr 130 enable */
276 #define TS_129 BIT(9) /* Time Sync Dest IP Addr 129 enable */
277 #define TS_TTL_NONZERO BIT(8) /* Time Sync Time To Live Non-zero enable */
278 #define TS_ANNEX_F_EN BIT(6) /* Time Sync Annex F enable */
279 #define TS_ANNEX_D_EN BIT(4) /* Time Sync Annex D enable */
280 #define TS_LTYPE2_EN BIT(3) /* Time Sync LTYPE 2 enable */
281 #define TS_LTYPE1_EN BIT(2) /* Time Sync LTYPE 1 enable */
282 #define TS_TX_EN BIT(1) /* Time Sync Transmit Enable */
283 #define TS_RX_EN BIT(0) /* Time Sync Receive Enable */
285 #define CTRL_V2_TS_BITS \
286 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
287 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
289 #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
290 #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
291 #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
294 #define CTRL_V3_TS_BITS \
295 (TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
296 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
299 #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
300 #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
301 #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
303 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
304 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
305 #define TS_SEQ_ID_OFFSET_MASK (0x3f)
306 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
307 #define TS_MSG_TYPE_EN_MASK (0xffff)
309 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
310 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
312 /* Bit definitions for the CPSW1_TS_CTL register */
313 #define CPSW_V1_TS_RX_EN BIT(0)
314 #define CPSW_V1_TS_TX_EN BIT(4)
315 #define CPSW_V1_MSG_TYPE_OFS 16
317 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
318 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
320 #define CPSW_MAX_BLKS_TX 15
321 #define CPSW_MAX_BLKS_TX_SHIFT 4
322 #define CPSW_MAX_BLKS_RX 5
324 struct cpsw_host_regs {
330 u32 cpdma_tx_pri_map;
331 u32 cpdma_rx_chan_map;
334 struct cpsw_sliver_regs {
347 struct cpsw_hw_stats {
349 u32 rxbroadcastframes;
350 u32 rxmulticastframes;
353 u32 rxaligncodeerrors;
354 u32 rxoversizedframes;
356 u32 rxundersizedframes;
361 u32 txbroadcastframes;
362 u32 txmulticastframes;
364 u32 txdeferredframes;
365 u32 txcollisionframes;
366 u32 txsinglecollframes;
367 u32 txmultcollframes;
368 u32 txexcessivecollisions;
369 u32 txlatecollisions;
371 u32 txcarriersenseerrors;
374 u32 octetframes65t127;
375 u32 octetframes128t255;
376 u32 octetframes256t511;
377 u32 octetframes512t1023;
378 u32 octetframes1024tup;
385 struct cpsw_slave_data {
386 struct device_node *phy_node;
387 char phy_id[MII_BUS_ID_SIZE];
389 u8 mac_addr[ETH_ALEN];
390 u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */
394 struct cpsw_platform_data {
395 struct cpsw_slave_data *slave_data;
396 u32 ss_reg_ofs; /* Subsystem control register offset */
397 u32 channels; /* number of cpdma channels (symmetric) */
398 u32 slaves; /* number of slave cpgmac ports */
399 u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
400 u32 ale_entries; /* ale table size */
401 u32 bd_ram_size; /*buffer descriptor ram size */
402 u32 mac_control; /* Mac control register */
403 u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/
404 bool dual_emac; /* Enable Dual EMAC mode */
409 struct cpsw_sliver_regs __iomem *sliver;
412 struct cpsw_slave_data *data;
413 struct phy_device *phy;
414 struct net_device *ndev;
418 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
420 return readl_relaxed(slave->regs + offset);
423 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
425 writel_relaxed(val, slave->regs + offset);
429 struct cpdma_chan *ch;
435 struct cpsw_platform_data data;
436 struct napi_struct napi_rx;
437 struct napi_struct napi_tx;
438 struct cpsw_ss_regs __iomem *regs;
439 struct cpsw_wr_regs __iomem *wr_regs;
440 u8 __iomem *hw_stats;
441 struct cpsw_host_regs __iomem *host_port_regs;
446 struct cpsw_slave *slaves;
447 struct cpdma_ctlr *dma;
448 struct cpsw_vector txv[CPSW_MAX_QUEUES];
449 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
450 struct cpsw_ale *ale;
452 bool rx_irq_disabled;
453 bool tx_irq_disabled;
454 u32 irqs_table[IRQ_NUM];
456 int rx_ch_num, tx_ch_num;
462 struct net_device *ndev;
465 u8 mac_addr[ETH_ALEN];
469 int fifo_bw[CPSW_TC_NUM];
472 struct cpsw_common *cpsw;
476 char stat_string[ETH_GSTRING_LEN];
488 #define CPSW_STAT(m) CPSW_STATS, \
489 FIELD_SIZEOF(struct cpsw_hw_stats, m), \
490 offsetof(struct cpsw_hw_stats, m)
491 #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
492 FIELD_SIZEOF(struct cpdma_chan_stats, m), \
493 offsetof(struct cpdma_chan_stats, m)
494 #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
495 FIELD_SIZEOF(struct cpdma_chan_stats, m), \
496 offsetof(struct cpdma_chan_stats, m)
498 static const struct cpsw_stats cpsw_gstrings_stats[] = {
499 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
500 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
501 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
502 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
503 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
504 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
505 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
506 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
507 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
508 { "Rx Fragments", CPSW_STAT(rxfragments) },
509 { "Rx Octets", CPSW_STAT(rxoctets) },
510 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
511 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
512 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
513 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
514 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
515 { "Collisions", CPSW_STAT(txcollisionframes) },
516 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
517 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
518 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
519 { "Late Collisions", CPSW_STAT(txlatecollisions) },
520 { "Tx Underrun", CPSW_STAT(txunderrun) },
521 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
522 { "Tx Octets", CPSW_STAT(txoctets) },
523 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
524 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
525 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
526 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
527 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
528 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
529 { "Net Octets", CPSW_STAT(netoctets) },
530 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
531 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
532 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
535 static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
536 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
537 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
538 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
539 { "misqueued", CPDMA_RX_STAT(misqueued) },
540 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
541 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
542 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
543 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
544 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
545 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
546 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
547 { "requeue", CPDMA_RX_STAT(requeue) },
548 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
551 #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
552 #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
554 #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
555 #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
556 #define for_each_slave(priv, func, arg...) \
558 struct cpsw_slave *slave; \
559 struct cpsw_common *cpsw = (priv)->cpsw; \
561 if (cpsw->data.dual_emac) \
562 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
564 for (n = cpsw->data.slaves, \
565 slave = cpsw->slaves; \
567 (func)(slave++, ##arg); \
570 static inline int cpsw_get_slave_port(u32 slave_num)
572 return slave_num + 1;
575 static void cpsw_add_mcast(struct cpsw_priv *priv, const u8 *addr)
577 struct cpsw_common *cpsw = priv->cpsw;
579 if (cpsw->data.dual_emac) {
580 struct cpsw_slave *slave = cpsw->slaves + priv->emac_port;
582 cpsw_ale_add_mcast(cpsw->ale, addr, ALE_PORT_HOST,
583 ALE_VLAN, slave->port_vlan, 0);
587 cpsw_ale_add_mcast(cpsw->ale, addr, ALE_ALL_PORTS, 0, 0, 0);
590 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
592 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
593 struct cpsw_ale *ale = cpsw->ale;
596 if (cpsw->data.dual_emac) {
599 /* Enabling promiscuous mode for one interface will be
600 * common for both the interface as the interface shares
601 * the same hardware resource.
603 for (i = 0; i < cpsw->data.slaves; i++)
604 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
607 if (!enable && flag) {
609 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
614 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
616 dev_dbg(&ndev->dev, "promiscuity enabled\n");
619 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
620 dev_dbg(&ndev->dev, "promiscuity disabled\n");
624 unsigned long timeout = jiffies + HZ;
626 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
627 for (i = 0; i <= cpsw->data.slaves; i++) {
628 cpsw_ale_control_set(ale, i,
629 ALE_PORT_NOLEARN, 1);
630 cpsw_ale_control_set(ale, i,
631 ALE_PORT_NO_SA_UPDATE, 1);
634 /* Clear All Untouched entries */
635 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
638 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
640 } while (time_after(timeout, jiffies));
641 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
643 /* Clear all mcast from ALE */
644 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
645 __dev_mc_unsync(ndev, NULL);
647 /* Flood All Unicast Packets to Host port */
648 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
649 dev_dbg(&ndev->dev, "promiscuity enabled\n");
651 /* Don't Flood All Unicast Packets to Host port */
652 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
654 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
655 for (i = 0; i <= cpsw->data.slaves; i++) {
656 cpsw_ale_control_set(ale, i,
657 ALE_PORT_NOLEARN, 0);
658 cpsw_ale_control_set(ale, i,
659 ALE_PORT_NO_SA_UPDATE, 0);
661 dev_dbg(&ndev->dev, "promiscuity disabled\n");
666 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr)
668 struct cpsw_priv *priv = netdev_priv(ndev);
670 cpsw_add_mcast(priv, addr);
674 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr)
676 struct cpsw_priv *priv = netdev_priv(ndev);
677 struct cpsw_common *cpsw = priv->cpsw;
680 if (cpsw->data.dual_emac) {
681 vid = cpsw->slaves[priv->emac_port].port_vlan;
688 cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
692 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
694 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
696 if (ndev->flags & IFF_PROMISC) {
697 /* Enable promiscuous mode */
698 cpsw_set_promiscious(ndev, true);
699 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
702 /* Disable promiscuous mode */
703 cpsw_set_promiscious(ndev, false);
706 /* Restore allmulti on vlans if necessary */
707 cpsw_ale_set_allmulti(cpsw->ale, ndev->flags & IFF_ALLMULTI);
709 __dev_mc_sync(ndev, cpsw_add_mc_addr, cpsw_del_mc_addr);
712 static void cpsw_intr_enable(struct cpsw_common *cpsw)
714 writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
715 writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
717 cpdma_ctlr_int_ctrl(cpsw->dma, true);
721 static void cpsw_intr_disable(struct cpsw_common *cpsw)
723 writel_relaxed(0, &cpsw->wr_regs->tx_en);
724 writel_relaxed(0, &cpsw->wr_regs->rx_en);
726 cpdma_ctlr_int_ctrl(cpsw->dma, false);
730 static void cpsw_tx_handler(void *token, int len, int status)
732 struct netdev_queue *txq;
733 struct sk_buff *skb = token;
734 struct net_device *ndev = skb->dev;
735 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
737 /* Check whether the queue is stopped due to stalled tx dma, if the
738 * queue is stopped then start the queue as we have free desc for tx
740 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
741 if (unlikely(netif_tx_queue_stopped(txq)))
742 netif_tx_wake_queue(txq);
744 cpts_tx_timestamp(cpsw->cpts, skb);
745 ndev->stats.tx_packets++;
746 ndev->stats.tx_bytes += len;
747 dev_kfree_skb_any(skb);
750 static void cpsw_rx_vlan_encap(struct sk_buff *skb)
752 struct cpsw_priv *priv = netdev_priv(skb->dev);
753 struct cpsw_common *cpsw = priv->cpsw;
754 u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
755 u16 vtag, vid, prio, pkt_type;
757 /* Remove VLAN header encapsulation word */
758 skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
760 pkt_type = (rx_vlan_encap_hdr >>
761 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
762 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
763 /* Ignore unknown & Priority-tagged packets*/
764 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
765 pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
768 vid = (rx_vlan_encap_hdr >>
769 CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
771 /* Ignore vid 0 and pass packet as is */
774 /* Ignore default vlans in dual mac mode */
775 if (cpsw->data.dual_emac &&
776 vid == cpsw->slaves[priv->emac_port].port_vlan)
779 prio = (rx_vlan_encap_hdr >>
780 CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
781 CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
783 vtag = (prio << VLAN_PRIO_SHIFT) | vid;
784 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
786 /* strip vlan tag for VLAN-tagged packet */
787 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
788 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
789 skb_pull(skb, VLAN_HLEN);
793 static void cpsw_rx_handler(void *token, int len, int status)
795 struct cpdma_chan *ch;
796 struct sk_buff *skb = token;
797 struct sk_buff *new_skb;
798 struct net_device *ndev = skb->dev;
800 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
802 if (cpsw->data.dual_emac) {
803 port = CPDMA_RX_SOURCE_PORT(status);
805 ndev = cpsw->slaves[--port].ndev;
810 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
811 /* In dual emac mode check for all interfaces */
812 if (cpsw->data.dual_emac && cpsw->usage_count &&
814 /* The packet received is for the interface which
815 * is already down and the other interface is up
816 * and running, instead of freeing which results
817 * in reducing of the number of rx descriptor in
818 * DMA engine, requeue skb back to cpdma.
824 /* the interface is going down, skbs are purged */
825 dev_kfree_skb_any(skb);
829 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
831 skb_copy_queue_mapping(new_skb, skb);
833 if (status & CPDMA_RX_VLAN_ENCAP)
834 cpsw_rx_vlan_encap(skb);
835 cpts_rx_timestamp(cpsw->cpts, skb);
836 skb->protocol = eth_type_trans(skb, ndev);
837 netif_receive_skb(skb);
838 ndev->stats.rx_bytes += len;
839 ndev->stats.rx_packets++;
840 kmemleak_not_leak(new_skb);
842 ndev->stats.rx_dropped++;
847 if (netif_dormant(ndev)) {
848 dev_kfree_skb_any(new_skb);
852 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
853 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
854 skb_tailroom(new_skb), 0);
855 if (WARN_ON(ret < 0))
856 dev_kfree_skb_any(new_skb);
859 static void cpsw_split_res(struct net_device *ndev)
861 struct cpsw_priv *priv = netdev_priv(ndev);
862 u32 consumed_rate = 0, bigest_rate = 0;
863 struct cpsw_common *cpsw = priv->cpsw;
864 struct cpsw_vector *txv = cpsw->txv;
865 int i, ch_weight, rlim_ch_num = 0;
866 int budget, bigest_rate_ch = 0;
867 u32 ch_rate, max_rate;
870 for (i = 0; i < cpsw->tx_ch_num; i++) {
871 ch_rate = cpdma_chan_get_rate(txv[i].ch);
876 consumed_rate += ch_rate;
879 if (cpsw->tx_ch_num == rlim_ch_num) {
880 max_rate = consumed_rate;
881 } else if (!rlim_ch_num) {
882 ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
884 max_rate = consumed_rate;
886 max_rate = cpsw->speed * 1000;
888 /* if max_rate is less then expected due to reduced link speed,
889 * split proportionally according next potential max speed
891 if (max_rate < consumed_rate)
894 if (max_rate < consumed_rate)
897 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
898 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
899 (cpsw->tx_ch_num - rlim_ch_num);
900 bigest_rate = (max_rate - consumed_rate) /
901 (cpsw->tx_ch_num - rlim_ch_num);
904 /* split tx weight/budget */
905 budget = CPSW_POLL_WEIGHT;
906 for (i = 0; i < cpsw->tx_ch_num; i++) {
907 ch_rate = cpdma_chan_get_rate(txv[i].ch);
909 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
912 if (ch_rate > bigest_rate) {
914 bigest_rate = ch_rate;
917 ch_weight = (ch_rate * 100) / max_rate;
920 cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
922 txv[i].budget = ch_budget;
925 cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
928 budget -= txv[i].budget;
932 txv[bigest_rate_ch].budget += budget;
934 /* split rx budget */
935 budget = CPSW_POLL_WEIGHT;
936 ch_budget = budget / cpsw->rx_ch_num;
937 for (i = 0; i < cpsw->rx_ch_num; i++) {
938 cpsw->rxv[i].budget = ch_budget;
943 cpsw->rxv[0].budget += budget;
946 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
948 struct cpsw_common *cpsw = dev_id;
950 writel(0, &cpsw->wr_regs->tx_en);
951 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
953 if (cpsw->quirk_irq) {
954 disable_irq_nosync(cpsw->irqs_table[1]);
955 cpsw->tx_irq_disabled = true;
958 napi_schedule(&cpsw->napi_tx);
962 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
964 struct cpsw_common *cpsw = dev_id;
966 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
967 writel(0, &cpsw->wr_regs->rx_en);
969 if (cpsw->quirk_irq) {
970 disable_irq_nosync(cpsw->irqs_table[0]);
971 cpsw->rx_irq_disabled = true;
974 napi_schedule(&cpsw->napi_rx);
978 static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
981 int num_tx, cur_budget, ch;
982 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
983 struct cpsw_vector *txv;
985 /* process every unprocessed channel */
986 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
987 for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
988 if (!(ch_map & 0x80))
991 txv = &cpsw->txv[ch];
992 if (unlikely(txv->budget > budget - num_tx))
993 cur_budget = budget - num_tx;
995 cur_budget = txv->budget;
997 num_tx += cpdma_chan_process(txv->ch, cur_budget);
998 if (num_tx >= budget)
1002 if (num_tx < budget) {
1003 napi_complete(napi_tx);
1004 writel(0xff, &cpsw->wr_regs->tx_en);
1010 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
1012 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
1015 num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
1016 if (num_tx < budget) {
1017 napi_complete(napi_tx);
1018 writel(0xff, &cpsw->wr_regs->tx_en);
1019 if (cpsw->tx_irq_disabled) {
1020 cpsw->tx_irq_disabled = false;
1021 enable_irq(cpsw->irqs_table[1]);
1028 static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
1031 int num_rx, cur_budget, ch;
1032 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
1033 struct cpsw_vector *rxv;
1035 /* process every unprocessed channel */
1036 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1037 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1038 if (!(ch_map & 0x01))
1041 rxv = &cpsw->rxv[ch];
1042 if (unlikely(rxv->budget > budget - num_rx))
1043 cur_budget = budget - num_rx;
1045 cur_budget = rxv->budget;
1047 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1048 if (num_rx >= budget)
1052 if (num_rx < budget) {
1053 napi_complete_done(napi_rx, num_rx);
1054 writel(0xff, &cpsw->wr_regs->rx_en);
1060 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
1062 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
1065 num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
1066 if (num_rx < budget) {
1067 napi_complete_done(napi_rx, num_rx);
1068 writel(0xff, &cpsw->wr_regs->rx_en);
1069 if (cpsw->rx_irq_disabled) {
1070 cpsw->rx_irq_disabled = false;
1071 enable_irq(cpsw->irqs_table[0]);
1078 static inline void soft_reset(const char *module, void __iomem *reg)
1080 unsigned long timeout = jiffies + HZ;
1082 writel_relaxed(1, reg);
1085 } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1087 WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1090 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1091 struct cpsw_priv *priv)
1093 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
1094 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1097 static bool cpsw_shp_is_off(struct cpsw_priv *priv)
1099 struct cpsw_common *cpsw = priv->cpsw;
1100 struct cpsw_slave *slave;
1101 u32 shift, mask, val;
1103 val = readl_relaxed(&cpsw->regs->ptype);
1105 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1106 shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1113 static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
1115 struct cpsw_common *cpsw = priv->cpsw;
1116 struct cpsw_slave *slave;
1117 u32 shift, mask, val;
1119 val = readl_relaxed(&cpsw->regs->ptype);
1121 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1122 shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1123 mask = (1 << --fifo) << shift;
1124 val = on ? val | mask : val & ~mask;
1126 writel_relaxed(val, &cpsw->regs->ptype);
1129 static void _cpsw_adjust_link(struct cpsw_slave *slave,
1130 struct cpsw_priv *priv, bool *link)
1132 struct phy_device *phy = slave->phy;
1133 u32 mac_control = 0;
1135 struct cpsw_common *cpsw = priv->cpsw;
1140 slave_port = cpsw_get_slave_port(slave->slave_num);
1143 mac_control = cpsw->data.mac_control;
1145 /* enable forwarding */
1146 cpsw_ale_control_set(cpsw->ale, slave_port,
1147 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1149 if (phy->speed == 1000)
1150 mac_control |= BIT(7); /* GIGABITEN */
1152 mac_control |= BIT(0); /* FULLDUPLEXEN */
1154 /* set speed_in input in case RMII mode is used in 100Mbps */
1155 if (phy->speed == 100)
1156 mac_control |= BIT(15);
1157 /* in band mode only works in 10Mbps RGMII mode */
1158 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1159 mac_control |= BIT(18); /* In Band mode */
1162 mac_control |= BIT(3);
1165 mac_control |= BIT(4);
1169 if (priv->shp_cfg_speed &&
1170 priv->shp_cfg_speed != slave->phy->speed &&
1171 !cpsw_shp_is_off(priv))
1173 "Speed was changed, CBS shaper speeds are changed!");
1176 /* disable forwarding */
1177 cpsw_ale_control_set(cpsw->ale, slave_port,
1178 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1181 if (mac_control != slave->mac_control) {
1182 phy_print_status(phy);
1183 writel_relaxed(mac_control, &slave->sliver->mac_control);
1186 slave->mac_control = mac_control;
1189 static int cpsw_get_common_speed(struct cpsw_common *cpsw)
1193 for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
1194 if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
1195 speed += cpsw->slaves[i].phy->speed;
1200 static int cpsw_need_resplit(struct cpsw_common *cpsw)
1205 /* re-split resources only in case speed was changed */
1206 speed = cpsw_get_common_speed(cpsw);
1207 if (speed == cpsw->speed || !speed)
1210 cpsw->speed = speed;
1212 for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
1213 ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
1220 /* cases not dependent on speed */
1221 if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
1227 static void cpsw_adjust_link(struct net_device *ndev)
1229 struct cpsw_priv *priv = netdev_priv(ndev);
1230 struct cpsw_common *cpsw = priv->cpsw;
1233 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1236 if (cpsw_need_resplit(cpsw))
1237 cpsw_split_res(ndev);
1239 netif_carrier_on(ndev);
1240 if (netif_running(ndev))
1241 netif_tx_wake_all_queues(ndev);
1243 netif_carrier_off(ndev);
1244 netif_tx_stop_all_queues(ndev);
1248 static int cpsw_get_coalesce(struct net_device *ndev,
1249 struct ethtool_coalesce *coal)
1251 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1253 coal->rx_coalesce_usecs = cpsw->coal_intvl;
1257 static int cpsw_set_coalesce(struct net_device *ndev,
1258 struct ethtool_coalesce *coal)
1260 struct cpsw_priv *priv = netdev_priv(ndev);
1262 u32 num_interrupts = 0;
1266 struct cpsw_common *cpsw = priv->cpsw;
1268 coal_intvl = coal->rx_coalesce_usecs;
1270 int_ctrl = readl(&cpsw->wr_regs->int_control);
1271 prescale = cpsw->bus_freq_mhz * 4;
1273 if (!coal->rx_coalesce_usecs) {
1274 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1278 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1279 coal_intvl = CPSW_CMINTMIN_INTVL;
1281 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1282 /* Interrupt pacer works with 4us Pulse, we can
1283 * throttle further by dilating the 4us pulse.
1285 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1287 if (addnl_dvdr > 1) {
1288 prescale *= addnl_dvdr;
1289 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1290 coal_intvl = (CPSW_CMINTMAX_INTVL
1294 coal_intvl = CPSW_CMINTMAX_INTVL;
1298 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1299 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1300 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1302 int_ctrl |= CPSW_INTPACEEN;
1303 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1304 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1307 writel(int_ctrl, &cpsw->wr_regs->int_control);
1309 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
1310 cpsw->coal_intvl = coal_intvl;
1315 static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1317 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1321 return (CPSW_STATS_COMMON_LEN +
1322 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1329 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1335 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1336 for (i = 0; i < ch_stats_len; i++) {
1337 line = i % CPSW_STATS_CH_LEN;
1338 snprintf(*p, ETH_GSTRING_LEN,
1339 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1340 (long)(i / CPSW_STATS_CH_LEN),
1341 cpsw_gstrings_ch_stats[line].stat_string);
1342 *p += ETH_GSTRING_LEN;
1346 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1348 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1352 switch (stringset) {
1354 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1355 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1357 p += ETH_GSTRING_LEN;
1360 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1361 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1366 static void cpsw_get_ethtool_stats(struct net_device *ndev,
1367 struct ethtool_stats *stats, u64 *data)
1370 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1371 struct cpdma_chan_stats ch_stats;
1374 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
1375 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1376 data[l] = readl(cpsw->hw_stats +
1377 cpsw_gstrings_stats[l].stat_offset);
1379 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1380 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1381 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1382 p = (u8 *)&ch_stats +
1383 cpsw_gstrings_ch_stats[i].stat_offset;
1384 data[l] = *(u32 *)p;
1388 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1389 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1390 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1391 p = (u8 *)&ch_stats +
1392 cpsw_gstrings_ch_stats[i].stat_offset;
1393 data[l] = *(u32 *)p;
1398 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1399 struct sk_buff *skb,
1400 struct cpdma_chan *txch)
1402 struct cpsw_common *cpsw = priv->cpsw;
1404 skb_tx_timestamp(skb);
1405 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1406 priv->emac_port + cpsw->data.dual_emac);
1409 static inline void cpsw_add_dual_emac_def_ale_entries(
1410 struct cpsw_priv *priv, struct cpsw_slave *slave,
1413 struct cpsw_common *cpsw = priv->cpsw;
1414 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1416 if (cpsw->version == CPSW_VERSION_1)
1417 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1419 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
1420 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1421 port_mask, port_mask, 0);
1422 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1423 ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
1424 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1425 HOST_PORT_NUM, ALE_VLAN |
1426 ALE_SECURE, slave->port_vlan);
1427 cpsw_ale_control_set(cpsw->ale, slave_port,
1428 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1431 static void soft_reset_slave(struct cpsw_slave *slave)
1435 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1436 soft_reset(name, &slave->sliver->soft_reset);
1439 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1442 struct phy_device *phy;
1443 struct cpsw_common *cpsw = priv->cpsw;
1445 soft_reset_slave(slave);
1447 /* setup priority mapping */
1448 writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
1450 switch (cpsw->version) {
1451 case CPSW_VERSION_1:
1452 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1453 /* Increase RX FIFO size to 5 for supporting fullduplex
1457 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1458 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
1460 case CPSW_VERSION_2:
1461 case CPSW_VERSION_3:
1462 case CPSW_VERSION_4:
1463 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1464 /* Increase RX FIFO size to 5 for supporting fullduplex
1468 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1469 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
1473 /* setup max packet size, and mac address */
1474 writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1475 cpsw_set_slave_mac(slave, priv);
1477 slave->mac_control = 0; /* no link yet */
1479 slave_port = cpsw_get_slave_port(slave->slave_num);
1481 if (cpsw->data.dual_emac)
1482 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1484 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1485 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1487 if (slave->data->phy_node) {
1488 phy = of_phy_connect(priv->ndev, slave->data->phy_node,
1489 &cpsw_adjust_link, 0, slave->data->phy_if);
1491 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1492 slave->data->phy_node,
1497 phy = phy_connect(priv->ndev, slave->data->phy_id,
1498 &cpsw_adjust_link, slave->data->phy_if);
1501 "phy \"%s\" not found on slave %d, err %ld\n",
1502 slave->data->phy_id, slave->slave_num,
1510 phy_attached_info(slave->phy);
1512 phy_start(slave->phy);
1514 /* Configure GMII_SEL register */
1515 if (!IS_ERR(slave->data->ifphy))
1516 phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
1517 slave->data->phy_if);
1519 cpsw_phy_sel(cpsw->dev, slave->phy->interface,
1523 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1525 struct cpsw_common *cpsw = priv->cpsw;
1526 const int vlan = cpsw->data.default_vlan;
1529 int unreg_mcast_mask;
1531 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
1534 writel(vlan, &cpsw->host_port_regs->port_vlan);
1536 for (i = 0; i < cpsw->data.slaves; i++)
1537 slave_write(cpsw->slaves + i, vlan, reg);
1539 if (priv->ndev->flags & IFF_ALLMULTI)
1540 unreg_mcast_mask = ALE_ALL_PORTS;
1542 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1544 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
1545 ALE_ALL_PORTS, ALE_ALL_PORTS,
1549 static void cpsw_init_host_port(struct cpsw_priv *priv)
1553 struct cpsw_common *cpsw = priv->cpsw;
1555 /* soft reset the controller and initialize ale */
1556 soft_reset("cpsw", &cpsw->regs->soft_reset);
1557 cpsw_ale_start(cpsw->ale);
1559 /* switch to vlan unaware mode */
1560 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
1561 CPSW_ALE_VLAN_AWARE);
1562 control_reg = readl(&cpsw->regs->control);
1563 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
1564 writel(control_reg, &cpsw->regs->control);
1565 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1566 CPSW_FIFO_NORMAL_MODE;
1567 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1569 /* setup host port priority mapping */
1570 writel_relaxed(CPDMA_TX_PRIORITY_MAP,
1571 &cpsw->host_port_regs->cpdma_tx_pri_map);
1572 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1574 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1575 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1577 if (!cpsw->data.dual_emac) {
1578 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1580 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1581 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1585 static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1587 struct cpsw_common *cpsw = priv->cpsw;
1588 struct sk_buff *skb;
1592 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1593 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
1594 for (i = 0; i < ch_buf_num; i++) {
1595 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1596 cpsw->rx_packet_max,
1599 cpsw_err(priv, ifup, "cannot allocate skb\n");
1603 skb_set_queue_mapping(skb, ch);
1604 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1605 skb->data, skb_tailroom(skb),
1608 cpsw_err(priv, ifup,
1609 "cannot submit skb to channel %d rx, error %d\n",
1614 kmemleak_not_leak(skb);
1617 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1624 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1628 slave_port = cpsw_get_slave_port(slave->slave_num);
1632 phy_stop(slave->phy);
1633 phy_disconnect(slave->phy);
1635 cpsw_ale_control_set(cpsw->ale, slave_port,
1636 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1637 soft_reset_slave(slave);
1640 static int cpsw_tc_to_fifo(int tc, int num_tc)
1642 if (tc == num_tc - 1)
1645 return CPSW_FIFO_SHAPERS_NUM - tc;
1648 static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
1650 struct cpsw_common *cpsw = priv->cpsw;
1651 u32 val = 0, send_pct, shift;
1652 struct cpsw_slave *slave;
1655 if (bw > priv->shp_cfg_speed * 1000)
1658 /* shaping has to stay enabled for highest fifos linearly
1659 * and fifo bw no more then interface can allow
1661 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1662 send_pct = slave_read(slave, SEND_PERCENT);
1663 for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
1665 if (i >= fifo || !priv->fifo_bw[i])
1668 dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
1672 if (!priv->fifo_bw[i] && i > fifo) {
1673 dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
1677 shift = (i - 1) * 8;
1679 send_pct &= ~(CPSW_PCT_MASK << shift);
1680 val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
1684 send_pct |= val << shift;
1689 if (priv->fifo_bw[i])
1690 pct += (send_pct >> shift) & CPSW_PCT_MASK;
1696 slave_write(slave, send_pct, SEND_PERCENT);
1697 priv->fifo_bw[fifo] = bw;
1699 dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
1700 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
1704 dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
1708 static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
1710 struct cpsw_common *cpsw = priv->cpsw;
1711 struct cpsw_slave *slave;
1712 u32 tx_in_ctl_rg, val;
1715 ret = cpsw_set_fifo_bw(priv, fifo, bw);
1719 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1720 tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
1721 CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
1724 cpsw_fifo_shp_on(priv, fifo, bw);
1726 val = slave_read(slave, tx_in_ctl_rg);
1727 if (cpsw_shp_is_off(priv)) {
1728 /* disable FIFOs rate limited queues */
1729 val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
1731 /* set type of FIFO queues to normal priority mode */
1732 val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
1734 /* set type of FIFO queues to be rate limited */
1736 val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
1738 priv->shp_cfg_speed = 0;
1741 /* toggle a FIFO rate limited queue */
1743 val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1745 val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1746 slave_write(slave, val, tx_in_ctl_rg);
1748 /* FIFO transmit shape enable */
1749 cpsw_fifo_shp_on(priv, fifo, bw);
1756 * shaping for class A should be set first
1758 static int cpsw_set_cbs(struct net_device *ndev,
1759 struct tc_cbs_qopt_offload *qopt)
1761 struct cpsw_priv *priv = netdev_priv(ndev);
1762 struct cpsw_common *cpsw = priv->cpsw;
1763 struct cpsw_slave *slave;
1768 tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
1770 /* enable channels in backward order, as highest FIFOs must be rate
1771 * limited first and for compliance with CPDMA rate limited channels
1772 * that also used in bacward order. FIFO0 cannot be rate limited.
1774 fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
1776 dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
1780 /* do nothing, it's disabled anyway */
1781 if (!qopt->enable && !priv->fifo_bw[fifo])
1784 /* shapers can be set if link speed is known */
1785 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1786 if (slave->phy && slave->phy->link) {
1787 if (priv->shp_cfg_speed &&
1788 priv->shp_cfg_speed != slave->phy->speed)
1789 prev_speed = priv->shp_cfg_speed;
1791 priv->shp_cfg_speed = slave->phy->speed;
1794 if (!priv->shp_cfg_speed) {
1795 dev_err(priv->dev, "Link speed is not known");
1799 ret = pm_runtime_get_sync(cpsw->dev);
1801 pm_runtime_put_noidle(cpsw->dev);
1805 bw = qopt->enable ? qopt->idleslope : 0;
1806 ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
1808 priv->shp_cfg_speed = prev_speed;
1812 if (bw && prev_speed)
1814 "Speed was changed, CBS shaper speeds are changed!");
1816 pm_runtime_put_sync(cpsw->dev);
1820 static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1824 for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
1825 bw = priv->fifo_bw[fifo];
1829 cpsw_set_fifo_rlimit(priv, fifo, bw);
1833 static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1835 struct cpsw_common *cpsw = priv->cpsw;
1836 u32 tx_prio_map = 0;
1840 if (!priv->mqprio_hw)
1843 for (i = 0; i < 8; i++) {
1844 tc = netdev_get_prio_tc_map(priv->ndev, i);
1845 fifo = CPSW_FIFO_SHAPERS_NUM - tc;
1846 tx_prio_map |= fifo << (4 * i);
1849 tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
1850 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
1852 slave_write(slave, tx_prio_map, tx_prio_rg);
1855 /* restore resources after port reset */
1856 static void cpsw_restore(struct cpsw_priv *priv)
1858 /* restore MQPRIO offload */
1859 for_each_slave(priv, cpsw_mqprio_resume, priv);
1861 /* restore CBS offload */
1862 for_each_slave(priv, cpsw_cbs_resume, priv);
1865 static int cpsw_ndo_open(struct net_device *ndev)
1867 struct cpsw_priv *priv = netdev_priv(ndev);
1868 struct cpsw_common *cpsw = priv->cpsw;
1872 ret = pm_runtime_get_sync(cpsw->dev);
1874 pm_runtime_put_noidle(cpsw->dev);
1878 netif_carrier_off(ndev);
1880 /* Notify the stack of the actual queue counts. */
1881 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1883 dev_err(priv->dev, "cannot set real number of tx queues\n");
1887 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1889 dev_err(priv->dev, "cannot set real number of rx queues\n");
1893 reg = cpsw->version;
1895 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1896 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1897 CPSW_RTL_VERSION(reg));
1899 /* Initialize host and slave ports */
1900 if (!cpsw->usage_count)
1901 cpsw_init_host_port(priv);
1902 for_each_slave(priv, cpsw_slave_open, priv);
1904 /* Add default VLAN */
1905 if (!cpsw->data.dual_emac)
1906 cpsw_add_default_vlan(priv);
1908 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
1909 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
1911 /* initialize shared resources for every ndev */
1912 if (!cpsw->usage_count) {
1913 /* disable priority elevation */
1914 writel_relaxed(0, &cpsw->regs->ptype);
1916 /* enable statistics collection only on all ports */
1917 writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1919 /* Enable internal fifo flow control */
1920 writel(0x7, &cpsw->regs->flow_control);
1922 napi_enable(&cpsw->napi_rx);
1923 napi_enable(&cpsw->napi_tx);
1925 if (cpsw->tx_irq_disabled) {
1926 cpsw->tx_irq_disabled = false;
1927 enable_irq(cpsw->irqs_table[1]);
1930 if (cpsw->rx_irq_disabled) {
1931 cpsw->rx_irq_disabled = false;
1932 enable_irq(cpsw->irqs_table[0]);
1935 ret = cpsw_fill_rx_channels(priv);
1939 if (cpts_register(cpsw->cpts))
1940 dev_err(priv->dev, "error registering cpts device\n");
1946 /* Enable Interrupt pacing if configured */
1947 if (cpsw->coal_intvl != 0) {
1948 struct ethtool_coalesce coal;
1950 coal.rx_coalesce_usecs = cpsw->coal_intvl;
1951 cpsw_set_coalesce(ndev, &coal);
1954 cpdma_ctlr_start(cpsw->dma);
1955 cpsw_intr_enable(cpsw);
1956 cpsw->usage_count++;
1961 cpdma_ctlr_stop(cpsw->dma);
1962 for_each_slave(priv, cpsw_slave_stop, cpsw);
1963 pm_runtime_put_sync(cpsw->dev);
1964 netif_carrier_off(priv->ndev);
1968 static int cpsw_ndo_stop(struct net_device *ndev)
1970 struct cpsw_priv *priv = netdev_priv(ndev);
1971 struct cpsw_common *cpsw = priv->cpsw;
1973 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1974 __dev_mc_unsync(priv->ndev, cpsw_del_mc_addr);
1975 netif_tx_stop_all_queues(priv->ndev);
1976 netif_carrier_off(priv->ndev);
1978 if (cpsw->usage_count <= 1) {
1979 napi_disable(&cpsw->napi_rx);
1980 napi_disable(&cpsw->napi_tx);
1981 cpts_unregister(cpsw->cpts);
1982 cpsw_intr_disable(cpsw);
1983 cpdma_ctlr_stop(cpsw->dma);
1984 cpsw_ale_stop(cpsw->ale);
1986 for_each_slave(priv, cpsw_slave_stop, cpsw);
1988 if (cpsw_need_resplit(cpsw))
1989 cpsw_split_res(ndev);
1991 cpsw->usage_count--;
1992 pm_runtime_put_sync(cpsw->dev);
1996 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1997 struct net_device *ndev)
1999 struct cpsw_priv *priv = netdev_priv(ndev);
2000 struct cpsw_common *cpsw = priv->cpsw;
2001 struct cpts *cpts = cpsw->cpts;
2002 struct netdev_queue *txq;
2003 struct cpdma_chan *txch;
2006 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
2007 cpsw_err(priv, tx_err, "packet pad failed\n");
2008 ndev->stats.tx_dropped++;
2009 return NET_XMIT_DROP;
2012 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2013 cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
2014 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2016 q_idx = skb_get_queue_mapping(skb);
2017 if (q_idx >= cpsw->tx_ch_num)
2018 q_idx = q_idx % cpsw->tx_ch_num;
2020 txch = cpsw->txv[q_idx].ch;
2021 txq = netdev_get_tx_queue(ndev, q_idx);
2022 ret = cpsw_tx_packet_submit(priv, skb, txch);
2023 if (unlikely(ret != 0)) {
2024 cpsw_err(priv, tx_err, "desc submit failed\n");
2028 /* If there is no more tx desc left free then we need to
2029 * tell the kernel to stop sending us tx frames.
2031 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
2032 netif_tx_stop_queue(txq);
2034 /* Barrier, so that stop_queue visible to other cpus */
2035 smp_mb__after_atomic();
2037 if (cpdma_check_free_tx_desc(txch))
2038 netif_tx_wake_queue(txq);
2041 return NETDEV_TX_OK;
2043 ndev->stats.tx_dropped++;
2044 netif_tx_stop_queue(txq);
2046 /* Barrier, so that stop_queue visible to other cpus */
2047 smp_mb__after_atomic();
2049 if (cpdma_check_free_tx_desc(txch))
2050 netif_tx_wake_queue(txq);
2052 return NETDEV_TX_BUSY;
2055 #if IS_ENABLED(CONFIG_TI_CPTS)
2057 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
2059 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
2062 if (!cpts_is_tx_enabled(cpsw->cpts) &&
2063 !cpts_is_rx_enabled(cpsw->cpts)) {
2064 slave_write(slave, 0, CPSW1_TS_CTL);
2068 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
2069 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
2071 if (cpts_is_tx_enabled(cpsw->cpts))
2072 ts_en |= CPSW_V1_TS_TX_EN;
2074 if (cpts_is_rx_enabled(cpsw->cpts))
2075 ts_en |= CPSW_V1_TS_RX_EN;
2077 slave_write(slave, ts_en, CPSW1_TS_CTL);
2078 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
2081 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
2083 struct cpsw_slave *slave;
2084 struct cpsw_common *cpsw = priv->cpsw;
2087 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2089 ctrl = slave_read(slave, CPSW2_CONTROL);
2090 switch (cpsw->version) {
2091 case CPSW_VERSION_2:
2092 ctrl &= ~CTRL_V2_ALL_TS_MASK;
2094 if (cpts_is_tx_enabled(cpsw->cpts))
2095 ctrl |= CTRL_V2_TX_TS_BITS;
2097 if (cpts_is_rx_enabled(cpsw->cpts))
2098 ctrl |= CTRL_V2_RX_TS_BITS;
2100 case CPSW_VERSION_3:
2102 ctrl &= ~CTRL_V3_ALL_TS_MASK;
2104 if (cpts_is_tx_enabled(cpsw->cpts))
2105 ctrl |= CTRL_V3_TX_TS_BITS;
2107 if (cpts_is_rx_enabled(cpsw->cpts))
2108 ctrl |= CTRL_V3_RX_TS_BITS;
2112 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
2114 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
2115 slave_write(slave, ctrl, CPSW2_CONTROL);
2116 writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
2119 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2121 struct cpsw_priv *priv = netdev_priv(dev);
2122 struct hwtstamp_config cfg;
2123 struct cpsw_common *cpsw = priv->cpsw;
2124 struct cpts *cpts = cpsw->cpts;
2126 if (cpsw->version != CPSW_VERSION_1 &&
2127 cpsw->version != CPSW_VERSION_2 &&
2128 cpsw->version != CPSW_VERSION_3)
2131 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
2134 /* reserved for future extensions */
2138 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
2141 switch (cfg.rx_filter) {
2142 case HWTSTAMP_FILTER_NONE:
2143 cpts_rx_enable(cpts, 0);
2145 case HWTSTAMP_FILTER_ALL:
2146 case HWTSTAMP_FILTER_NTP_ALL:
2148 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2149 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2150 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2151 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
2152 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
2154 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2155 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2156 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2157 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2158 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2159 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2160 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2161 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2162 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2163 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
2164 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2170 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
2172 switch (cpsw->version) {
2173 case CPSW_VERSION_1:
2174 cpsw_hwtstamp_v1(cpsw);
2176 case CPSW_VERSION_2:
2177 case CPSW_VERSION_3:
2178 cpsw_hwtstamp_v2(priv);
2184 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2187 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2189 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
2190 struct cpts *cpts = cpsw->cpts;
2191 struct hwtstamp_config cfg;
2193 if (cpsw->version != CPSW_VERSION_1 &&
2194 cpsw->version != CPSW_VERSION_2 &&
2195 cpsw->version != CPSW_VERSION_3)
2199 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
2200 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2201 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
2202 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
2204 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2207 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2212 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2216 #endif /*CONFIG_TI_CPTS*/
2218 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2220 struct cpsw_priv *priv = netdev_priv(dev);
2221 struct cpsw_common *cpsw = priv->cpsw;
2222 int slave_no = cpsw_slave_index(cpsw, priv);
2224 if (!netif_running(dev))
2229 return cpsw_hwtstamp_set(dev, req);
2231 return cpsw_hwtstamp_get(dev, req);
2234 if (!cpsw->slaves[slave_no].phy)
2236 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
2239 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
2241 struct cpsw_priv *priv = netdev_priv(ndev);
2242 struct cpsw_common *cpsw = priv->cpsw;
2245 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
2246 ndev->stats.tx_errors++;
2247 cpsw_intr_disable(cpsw);
2248 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
2249 cpdma_chan_stop(cpsw->txv[ch].ch);
2250 cpdma_chan_start(cpsw->txv[ch].ch);
2253 cpsw_intr_enable(cpsw);
2254 netif_trans_update(ndev);
2255 netif_tx_wake_all_queues(ndev);
2258 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
2260 struct cpsw_priv *priv = netdev_priv(ndev);
2261 struct sockaddr *addr = (struct sockaddr *)p;
2262 struct cpsw_common *cpsw = priv->cpsw;
2267 if (!is_valid_ether_addr(addr->sa_data))
2268 return -EADDRNOTAVAIL;
2270 ret = pm_runtime_get_sync(cpsw->dev);
2272 pm_runtime_put_noidle(cpsw->dev);
2276 if (cpsw->data.dual_emac) {
2277 vid = cpsw->slaves[priv->emac_port].port_vlan;
2281 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
2283 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
2286 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2287 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2288 for_each_slave(priv, cpsw_set_slave_mac, priv);
2290 pm_runtime_put(cpsw->dev);
2295 #ifdef CONFIG_NET_POLL_CONTROLLER
2296 static void cpsw_ndo_poll_controller(struct net_device *ndev)
2298 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2300 cpsw_intr_disable(cpsw);
2301 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2302 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2303 cpsw_intr_enable(cpsw);
2307 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
2311 int unreg_mcast_mask = 0;
2314 struct cpsw_common *cpsw = priv->cpsw;
2316 if (cpsw->data.dual_emac) {
2317 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
2319 mcast_mask = ALE_PORT_HOST;
2320 if (priv->ndev->flags & IFF_ALLMULTI)
2321 unreg_mcast_mask = mcast_mask;
2323 port_mask = ALE_ALL_PORTS;
2324 mcast_mask = port_mask;
2326 if (priv->ndev->flags & IFF_ALLMULTI)
2327 unreg_mcast_mask = ALE_ALL_PORTS;
2329 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
2332 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
2337 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
2338 HOST_PORT_NUM, ALE_VLAN, vid);
2342 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
2343 mcast_mask, ALE_VLAN, vid, 0);
2345 goto clean_vlan_ucast;
2349 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2350 HOST_PORT_NUM, ALE_VLAN, vid);
2352 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2356 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
2357 __be16 proto, u16 vid)
2359 struct cpsw_priv *priv = netdev_priv(ndev);
2360 struct cpsw_common *cpsw = priv->cpsw;
2363 if (vid == cpsw->data.default_vlan)
2366 ret = pm_runtime_get_sync(cpsw->dev);
2368 pm_runtime_put_noidle(cpsw->dev);
2372 if (cpsw->data.dual_emac) {
2373 /* In dual EMAC, reserved VLAN id should not be used for
2374 * creating VLAN interfaces as this can break the dual
2375 * EMAC port separation
2379 for (i = 0; i < cpsw->data.slaves; i++) {
2380 if (vid == cpsw->slaves[i].port_vlan) {
2387 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2388 ret = cpsw_add_vlan_ale_entry(priv, vid);
2390 pm_runtime_put(cpsw->dev);
2394 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
2395 __be16 proto, u16 vid)
2397 struct cpsw_priv *priv = netdev_priv(ndev);
2398 struct cpsw_common *cpsw = priv->cpsw;
2401 if (vid == cpsw->data.default_vlan)
2404 ret = pm_runtime_get_sync(cpsw->dev);
2406 pm_runtime_put_noidle(cpsw->dev);
2410 if (cpsw->data.dual_emac) {
2413 for (i = 0; i < cpsw->data.slaves; i++) {
2414 if (vid == cpsw->slaves[i].port_vlan)
2419 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
2420 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2421 ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2422 HOST_PORT_NUM, ALE_VLAN, vid);
2423 ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
2426 pm_runtime_put(cpsw->dev);
2430 static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
2432 struct cpsw_priv *priv = netdev_priv(ndev);
2433 struct cpsw_common *cpsw = priv->cpsw;
2434 struct cpsw_slave *slave;
2439 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
2440 if (ch_rate == rate)
2443 ch_rate = rate * 1000;
2444 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2445 if ((ch_rate < min_rate && ch_rate)) {
2446 dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
2451 if (rate > cpsw->speed) {
2452 dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
2456 ret = pm_runtime_get_sync(cpsw->dev);
2458 pm_runtime_put_noidle(cpsw->dev);
2462 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
2463 pm_runtime_put(cpsw->dev);
2468 /* update rates for slaves tx queues */
2469 for (i = 0; i < cpsw->data.slaves; i++) {
2470 slave = &cpsw->slaves[i];
2474 netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
2477 cpsw_split_res(ndev);
2481 static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
2483 struct tc_mqprio_qopt_offload *mqprio = type_data;
2484 struct cpsw_priv *priv = netdev_priv(ndev);
2485 struct cpsw_common *cpsw = priv->cpsw;
2486 int fifo, num_tc, count, offset;
2487 struct cpsw_slave *slave;
2488 u32 tx_prio_map = 0;
2491 num_tc = mqprio->qopt.num_tc;
2492 if (num_tc > CPSW_TC_NUM)
2495 if (mqprio->mode != TC_MQPRIO_MODE_DCB)
2498 ret = pm_runtime_get_sync(cpsw->dev);
2500 pm_runtime_put_noidle(cpsw->dev);
2505 for (i = 0; i < 8; i++) {
2506 tc = mqprio->qopt.prio_tc_map[i];
2507 fifo = cpsw_tc_to_fifo(tc, num_tc);
2508 tx_prio_map |= fifo << (4 * i);
2511 netdev_set_num_tc(ndev, num_tc);
2512 for (i = 0; i < num_tc; i++) {
2513 count = mqprio->qopt.count[i];
2514 offset = mqprio->qopt.offset[i];
2515 netdev_set_tc_queue(ndev, i, count, offset);
2519 if (!mqprio->qopt.hw) {
2520 /* restore default configuration */
2521 netdev_reset_tc(ndev);
2522 tx_prio_map = TX_PRIORITY_MAPPING;
2525 priv->mqprio_hw = mqprio->qopt.hw;
2527 offset = cpsw->version == CPSW_VERSION_1 ?
2528 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
2530 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2531 slave_write(slave, tx_prio_map, offset);
2533 pm_runtime_put_sync(cpsw->dev);
2538 static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
2542 case TC_SETUP_QDISC_CBS:
2543 return cpsw_set_cbs(ndev, type_data);
2545 case TC_SETUP_QDISC_MQPRIO:
2546 return cpsw_set_mqprio(ndev, type_data);
2553 static const struct net_device_ops cpsw_netdev_ops = {
2554 .ndo_open = cpsw_ndo_open,
2555 .ndo_stop = cpsw_ndo_stop,
2556 .ndo_start_xmit = cpsw_ndo_start_xmit,
2557 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
2558 .ndo_do_ioctl = cpsw_ndo_ioctl,
2559 .ndo_validate_addr = eth_validate_addr,
2560 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
2561 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
2562 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
2563 #ifdef CONFIG_NET_POLL_CONTROLLER
2564 .ndo_poll_controller = cpsw_ndo_poll_controller,
2566 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2567 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
2568 .ndo_setup_tc = cpsw_ndo_setup_tc,
2571 static int cpsw_get_regs_len(struct net_device *ndev)
2573 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2575 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
2578 static void cpsw_get_regs(struct net_device *ndev,
2579 struct ethtool_regs *regs, void *p)
2582 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2584 /* update CPSW IP version */
2585 regs->version = cpsw->version;
2587 cpsw_ale_dump(cpsw->ale, reg);
2590 static void cpsw_get_drvinfo(struct net_device *ndev,
2591 struct ethtool_drvinfo *info)
2593 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2594 struct platform_device *pdev = to_platform_device(cpsw->dev);
2596 strlcpy(info->driver, "cpsw", sizeof(info->driver));
2597 strlcpy(info->version, "1.0", sizeof(info->version));
2598 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2601 static u32 cpsw_get_msglevel(struct net_device *ndev)
2603 struct cpsw_priv *priv = netdev_priv(ndev);
2604 return priv->msg_enable;
2607 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2609 struct cpsw_priv *priv = netdev_priv(ndev);
2610 priv->msg_enable = value;
2613 #if IS_ENABLED(CONFIG_TI_CPTS)
2614 static int cpsw_get_ts_info(struct net_device *ndev,
2615 struct ethtool_ts_info *info)
2617 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2619 info->so_timestamping =
2620 SOF_TIMESTAMPING_TX_HARDWARE |
2621 SOF_TIMESTAMPING_TX_SOFTWARE |
2622 SOF_TIMESTAMPING_RX_HARDWARE |
2623 SOF_TIMESTAMPING_RX_SOFTWARE |
2624 SOF_TIMESTAMPING_SOFTWARE |
2625 SOF_TIMESTAMPING_RAW_HARDWARE;
2626 info->phc_index = cpsw->cpts->phc_index;
2628 (1 << HWTSTAMP_TX_OFF) |
2629 (1 << HWTSTAMP_TX_ON);
2631 (1 << HWTSTAMP_FILTER_NONE) |
2632 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2633 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2637 static int cpsw_get_ts_info(struct net_device *ndev,
2638 struct ethtool_ts_info *info)
2640 info->so_timestamping =
2641 SOF_TIMESTAMPING_TX_SOFTWARE |
2642 SOF_TIMESTAMPING_RX_SOFTWARE |
2643 SOF_TIMESTAMPING_SOFTWARE;
2644 info->phc_index = -1;
2646 info->rx_filters = 0;
2651 static int cpsw_get_link_ksettings(struct net_device *ndev,
2652 struct ethtool_link_ksettings *ecmd)
2654 struct cpsw_priv *priv = netdev_priv(ndev);
2655 struct cpsw_common *cpsw = priv->cpsw;
2656 int slave_no = cpsw_slave_index(cpsw, priv);
2658 if (!cpsw->slaves[slave_no].phy)
2661 phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
2665 static int cpsw_set_link_ksettings(struct net_device *ndev,
2666 const struct ethtool_link_ksettings *ecmd)
2668 struct cpsw_priv *priv = netdev_priv(ndev);
2669 struct cpsw_common *cpsw = priv->cpsw;
2670 int slave_no = cpsw_slave_index(cpsw, priv);
2672 if (cpsw->slaves[slave_no].phy)
2673 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2679 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2681 struct cpsw_priv *priv = netdev_priv(ndev);
2682 struct cpsw_common *cpsw = priv->cpsw;
2683 int slave_no = cpsw_slave_index(cpsw, priv);
2688 if (cpsw->slaves[slave_no].phy)
2689 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2692 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2694 struct cpsw_priv *priv = netdev_priv(ndev);
2695 struct cpsw_common *cpsw = priv->cpsw;
2696 int slave_no = cpsw_slave_index(cpsw, priv);
2698 if (cpsw->slaves[slave_no].phy)
2699 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2704 static void cpsw_get_pauseparam(struct net_device *ndev,
2705 struct ethtool_pauseparam *pause)
2707 struct cpsw_priv *priv = netdev_priv(ndev);
2709 pause->autoneg = AUTONEG_DISABLE;
2710 pause->rx_pause = priv->rx_pause ? true : false;
2711 pause->tx_pause = priv->tx_pause ? true : false;
2714 static int cpsw_set_pauseparam(struct net_device *ndev,
2715 struct ethtool_pauseparam *pause)
2717 struct cpsw_priv *priv = netdev_priv(ndev);
2720 priv->rx_pause = pause->rx_pause ? true : false;
2721 priv->tx_pause = pause->tx_pause ? true : false;
2723 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
2727 static int cpsw_ethtool_op_begin(struct net_device *ndev)
2729 struct cpsw_priv *priv = netdev_priv(ndev);
2730 struct cpsw_common *cpsw = priv->cpsw;
2733 ret = pm_runtime_get_sync(cpsw->dev);
2735 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
2736 pm_runtime_put_noidle(cpsw->dev);
2742 static void cpsw_ethtool_op_complete(struct net_device *ndev)
2744 struct cpsw_priv *priv = netdev_priv(ndev);
2747 ret = pm_runtime_put(priv->cpsw->dev);
2749 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2752 static void cpsw_get_channels(struct net_device *ndev,
2753 struct ethtool_channels *ch)
2755 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2757 ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2758 ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2759 ch->max_combined = 0;
2761 ch->other_count = 0;
2762 ch->rx_count = cpsw->rx_ch_num;
2763 ch->tx_count = cpsw->tx_ch_num;
2764 ch->combined_count = 0;
2767 static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2768 struct ethtool_channels *ch)
2770 if (cpsw->quirk_irq) {
2771 dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
2775 if (ch->combined_count)
2778 /* verify we have at least one channel in each direction */
2779 if (!ch->rx_count || !ch->tx_count)
2782 if (ch->rx_count > cpsw->data.channels ||
2783 ch->tx_count > cpsw->data.channels)
2789 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2791 struct cpsw_common *cpsw = priv->cpsw;
2792 void (*handler)(void *, int, int);
2793 struct netdev_queue *queue;
2794 struct cpsw_vector *vec;
2798 ch = &cpsw->rx_ch_num;
2800 handler = cpsw_rx_handler;
2802 ch = &cpsw->tx_ch_num;
2804 handler = cpsw_tx_handler;
2807 while (*ch < ch_num) {
2808 vch = rx ? *ch : 7 - *ch;
2809 vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
2810 queue = netdev_get_tx_queue(priv->ndev, *ch);
2811 queue->tx_maxrate = 0;
2813 if (IS_ERR(vec[*ch].ch))
2814 return PTR_ERR(vec[*ch].ch);
2819 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2820 (rx ? "rx" : "tx"));
2824 while (*ch > ch_num) {
2827 ret = cpdma_chan_destroy(vec[*ch].ch);
2831 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2832 (rx ? "rx" : "tx"));
2838 static int cpsw_update_channels(struct cpsw_priv *priv,
2839 struct ethtool_channels *ch)
2843 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2847 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2854 static void cpsw_suspend_data_pass(struct net_device *ndev)
2856 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2857 struct cpsw_slave *slave;
2860 /* Disable NAPI scheduling */
2861 cpsw_intr_disable(cpsw);
2863 /* Stop all transmit queues for every network device.
2864 * Disable re-using rx descriptors with dormant_on.
2866 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2867 if (!(slave->ndev && netif_running(slave->ndev)))
2870 netif_tx_stop_all_queues(slave->ndev);
2871 netif_dormant_on(slave->ndev);
2874 /* Handle rest of tx packets and stop cpdma channels */
2875 cpdma_ctlr_stop(cpsw->dma);
2878 static int cpsw_resume_data_pass(struct net_device *ndev)
2880 struct cpsw_priv *priv = netdev_priv(ndev);
2881 struct cpsw_common *cpsw = priv->cpsw;
2882 struct cpsw_slave *slave;
2885 /* Allow rx packets handling */
2886 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2887 if (slave->ndev && netif_running(slave->ndev))
2888 netif_dormant_off(slave->ndev);
2890 /* After this receive is started */
2891 if (cpsw->usage_count) {
2892 ret = cpsw_fill_rx_channels(priv);
2896 cpdma_ctlr_start(cpsw->dma);
2897 cpsw_intr_enable(cpsw);
2900 /* Resume transmit for every affected interface */
2901 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2902 if (slave->ndev && netif_running(slave->ndev))
2903 netif_tx_start_all_queues(slave->ndev);
2908 static int cpsw_set_channels(struct net_device *ndev,
2909 struct ethtool_channels *chs)
2911 struct cpsw_priv *priv = netdev_priv(ndev);
2912 struct cpsw_common *cpsw = priv->cpsw;
2913 struct cpsw_slave *slave;
2916 ret = cpsw_check_ch_settings(cpsw, chs);
2920 cpsw_suspend_data_pass(ndev);
2921 ret = cpsw_update_channels(priv, chs);
2925 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2926 if (!(slave->ndev && netif_running(slave->ndev)))
2929 /* Inform stack about new count of queues */
2930 ret = netif_set_real_num_tx_queues(slave->ndev,
2933 dev_err(priv->dev, "cannot set real number of tx queues\n");
2937 ret = netif_set_real_num_rx_queues(slave->ndev,
2940 dev_err(priv->dev, "cannot set real number of rx queues\n");
2945 if (cpsw->usage_count)
2946 cpsw_split_res(ndev);
2948 ret = cpsw_resume_data_pass(ndev);
2952 dev_err(priv->dev, "cannot update channels number, closing device\n");
2957 static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2959 struct cpsw_priv *priv = netdev_priv(ndev);
2960 struct cpsw_common *cpsw = priv->cpsw;
2961 int slave_no = cpsw_slave_index(cpsw, priv);
2963 if (cpsw->slaves[slave_no].phy)
2964 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2969 static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2971 struct cpsw_priv *priv = netdev_priv(ndev);
2972 struct cpsw_common *cpsw = priv->cpsw;
2973 int slave_no = cpsw_slave_index(cpsw, priv);
2975 if (cpsw->slaves[slave_no].phy)
2976 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2981 static int cpsw_nway_reset(struct net_device *ndev)
2983 struct cpsw_priv *priv = netdev_priv(ndev);
2984 struct cpsw_common *cpsw = priv->cpsw;
2985 int slave_no = cpsw_slave_index(cpsw, priv);
2987 if (cpsw->slaves[slave_no].phy)
2988 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2993 static void cpsw_get_ringparam(struct net_device *ndev,
2994 struct ethtool_ringparam *ering)
2996 struct cpsw_priv *priv = netdev_priv(ndev);
2997 struct cpsw_common *cpsw = priv->cpsw;
3000 ering->tx_max_pending = 0;
3001 ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
3002 ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
3003 ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
3006 static int cpsw_set_ringparam(struct net_device *ndev,
3007 struct ethtool_ringparam *ering)
3009 struct cpsw_priv *priv = netdev_priv(ndev);
3010 struct cpsw_common *cpsw = priv->cpsw;
3013 /* ignore ering->tx_pending - only rx_pending adjustment is supported */
3015 if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
3016 ering->rx_pending < CPSW_MAX_QUEUES ||
3017 ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
3020 if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
3023 cpsw_suspend_data_pass(ndev);
3025 cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
3027 if (cpsw->usage_count)
3028 cpdma_chan_split_pool(cpsw->dma);
3030 ret = cpsw_resume_data_pass(ndev);
3034 dev_err(&ndev->dev, "cannot set ring params, closing device\n");
3039 static const struct ethtool_ops cpsw_ethtool_ops = {
3040 .get_drvinfo = cpsw_get_drvinfo,
3041 .get_msglevel = cpsw_get_msglevel,
3042 .set_msglevel = cpsw_set_msglevel,
3043 .get_link = ethtool_op_get_link,
3044 .get_ts_info = cpsw_get_ts_info,
3045 .get_coalesce = cpsw_get_coalesce,
3046 .set_coalesce = cpsw_set_coalesce,
3047 .get_sset_count = cpsw_get_sset_count,
3048 .get_strings = cpsw_get_strings,
3049 .get_ethtool_stats = cpsw_get_ethtool_stats,
3050 .get_pauseparam = cpsw_get_pauseparam,
3051 .set_pauseparam = cpsw_set_pauseparam,
3052 .get_wol = cpsw_get_wol,
3053 .set_wol = cpsw_set_wol,
3054 .get_regs_len = cpsw_get_regs_len,
3055 .get_regs = cpsw_get_regs,
3056 .begin = cpsw_ethtool_op_begin,
3057 .complete = cpsw_ethtool_op_complete,
3058 .get_channels = cpsw_get_channels,
3059 .set_channels = cpsw_set_channels,
3060 .get_link_ksettings = cpsw_get_link_ksettings,
3061 .set_link_ksettings = cpsw_set_link_ksettings,
3062 .get_eee = cpsw_get_eee,
3063 .set_eee = cpsw_set_eee,
3064 .nway_reset = cpsw_nway_reset,
3065 .get_ringparam = cpsw_get_ringparam,
3066 .set_ringparam = cpsw_set_ringparam,
3069 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
3070 u32 slave_reg_ofs, u32 sliver_reg_ofs)
3072 void __iomem *regs = cpsw->regs;
3073 int slave_num = slave->slave_num;
3074 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
3077 slave->regs = regs + slave_reg_ofs;
3078 slave->sliver = regs + sliver_reg_ofs;
3079 slave->port_vlan = data->dual_emac_res_vlan;
3082 static int cpsw_probe_dt(struct cpsw_platform_data *data,
3083 struct platform_device *pdev)
3085 struct device_node *node = pdev->dev.of_node;
3086 struct device_node *slave_node;
3093 if (of_property_read_u32(node, "slaves", &prop)) {
3094 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
3097 data->slaves = prop;
3099 if (of_property_read_u32(node, "active_slave", &prop)) {
3100 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
3103 data->active_slave = prop;
3105 data->slave_data = devm_kcalloc(&pdev->dev,
3107 sizeof(struct cpsw_slave_data),
3109 if (!data->slave_data)
3112 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
3113 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
3116 data->channels = prop;
3118 if (of_property_read_u32(node, "ale_entries", &prop)) {
3119 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
3122 data->ale_entries = prop;
3124 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
3125 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
3128 data->bd_ram_size = prop;
3130 if (of_property_read_u32(node, "mac_control", &prop)) {
3131 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
3134 data->mac_control = prop;
3136 if (of_property_read_bool(node, "dual_emac"))
3137 data->dual_emac = 1;
3140 * Populate all the child nodes here...
3142 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
3143 /* We do not want to force this, as in some cases may not have child */
3145 dev_warn(&pdev->dev, "Doesn't have any child node\n");
3147 for_each_available_child_of_node(node, slave_node) {
3148 struct cpsw_slave_data *slave_data = data->slave_data + i;
3149 const void *mac_addr = NULL;
3153 /* This is no slave child node, continue */
3154 if (strcmp(slave_node->name, "slave"))
3157 slave_data->ifphy = devm_of_phy_get(&pdev->dev, slave_node,
3159 if (!IS_ENABLED(CONFIG_TI_CPSW_PHY_SEL) &&
3160 IS_ERR(slave_data->ifphy)) {
3161 ret = PTR_ERR(slave_data->ifphy);
3163 "%d: Error retrieving port phy: %d\n", i, ret);
3167 slave_data->phy_node = of_parse_phandle(slave_node,
3169 parp = of_get_property(slave_node, "phy_id", &lenp);
3170 if (slave_data->phy_node) {
3172 "slave[%d] using phy-handle=\"%pOF\"\n",
3173 i, slave_data->phy_node);
3174 } else if (of_phy_is_fixed_link(slave_node)) {
3175 /* In the case of a fixed PHY, the DT node associated
3176 * to the PHY is the Ethernet MAC DT node.
3178 ret = of_phy_register_fixed_link(slave_node);
3180 if (ret != -EPROBE_DEFER)
3181 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
3184 slave_data->phy_node = of_node_get(slave_node);
3187 struct device_node *mdio_node;
3188 struct platform_device *mdio;
3190 if (lenp != (sizeof(__be32) * 2)) {
3191 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
3194 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
3195 phyid = be32_to_cpup(parp+1);
3196 mdio = of_find_device_by_node(mdio_node);
3197 of_node_put(mdio_node);
3199 dev_err(&pdev->dev, "Missing mdio platform device\n");
3202 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
3203 PHY_ID_FMT, mdio->name, phyid);
3204 put_device(&mdio->dev);
3207 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
3211 slave_data->phy_if = of_get_phy_mode(slave_node);
3212 if (slave_data->phy_if < 0) {
3213 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
3215 return slave_data->phy_if;
3219 mac_addr = of_get_mac_address(slave_node);
3221 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
3223 ret = ti_cm_get_macid(&pdev->dev, i,
3224 slave_data->mac_addr);
3228 if (data->dual_emac) {
3229 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
3231 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
3232 slave_data->dual_emac_res_vlan = i+1;
3233 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
3234 slave_data->dual_emac_res_vlan, i);
3236 slave_data->dual_emac_res_vlan = prop;
3241 if (i == data->slaves)
3248 static void cpsw_remove_dt(struct platform_device *pdev)
3250 struct net_device *ndev = platform_get_drvdata(pdev);
3251 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3252 struct cpsw_platform_data *data = &cpsw->data;
3253 struct device_node *node = pdev->dev.of_node;
3254 struct device_node *slave_node;
3257 for_each_available_child_of_node(node, slave_node) {
3258 struct cpsw_slave_data *slave_data = &data->slave_data[i];
3260 if (strcmp(slave_node->name, "slave"))
3263 if (of_phy_is_fixed_link(slave_node))
3264 of_phy_deregister_fixed_link(slave_node);
3266 of_node_put(slave_data->phy_node);
3269 if (i == data->slaves)
3273 of_platform_depopulate(&pdev->dev);
3276 static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
3278 struct cpsw_common *cpsw = priv->cpsw;
3279 struct cpsw_platform_data *data = &cpsw->data;
3280 struct net_device *ndev;
3281 struct cpsw_priv *priv_sl2;
3284 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3286 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
3290 priv_sl2 = netdev_priv(ndev);
3291 priv_sl2->cpsw = cpsw;
3292 priv_sl2->ndev = ndev;
3293 priv_sl2->dev = &ndev->dev;
3294 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3296 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3297 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3299 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
3300 priv_sl2->mac_addr);
3302 eth_random_addr(priv_sl2->mac_addr);
3303 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
3304 priv_sl2->mac_addr);
3306 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3308 priv_sl2->emac_port = 1;
3309 cpsw->slaves[1].ndev = ndev;
3310 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3312 ndev->netdev_ops = &cpsw_netdev_ops;
3313 ndev->ethtool_ops = &cpsw_ethtool_ops;
3315 /* register the network device */
3316 SET_NETDEV_DEV(ndev, cpsw->dev);
3317 ret = register_netdev(ndev);
3319 dev_err(cpsw->dev, "cpsw: error registering net device\n");
3327 static const struct of_device_id cpsw_of_mtable[] = {
3328 { .compatible = "ti,cpsw"},
3329 { .compatible = "ti,am335x-cpsw"},
3330 { .compatible = "ti,am4372-cpsw"},
3331 { .compatible = "ti,dra7-cpsw"},
3334 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
3336 static const struct soc_device_attribute cpsw_soc_devices[] = {
3337 { .family = "AM33xx", .revision = "ES1.0"},
3341 static int cpsw_probe(struct platform_device *pdev)
3344 struct cpsw_platform_data *data;
3345 struct net_device *ndev;
3346 struct cpsw_priv *priv;
3347 struct cpdma_params dma_params;
3348 struct cpsw_ale_params ale_params;
3349 void __iomem *ss_regs;
3350 void __iomem *cpts_regs;
3351 struct resource *res, *ss_res;
3352 struct gpio_descs *mode;
3353 u32 slave_offset, sliver_offset, slave_size;
3354 const struct soc_device_attribute *soc;
3355 struct cpsw_common *cpsw;
3359 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
3363 cpsw->dev = &pdev->dev;
3365 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3367 dev_err(&pdev->dev, "error allocating net_device\n");
3371 platform_set_drvdata(pdev, ndev);
3372 priv = netdev_priv(ndev);
3375 priv->dev = &ndev->dev;
3376 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3377 cpsw->rx_packet_max = max(rx_packet_max, 128);
3379 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
3381 ret = PTR_ERR(mode);
3382 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
3383 goto clean_ndev_ret;
3387 * This may be required here for child devices.
3389 pm_runtime_enable(&pdev->dev);
3391 /* Select default pin state */
3392 pinctrl_pm_select_default_state(&pdev->dev);
3394 /* Need to enable clocks with runtime PM api to access module
3397 ret = pm_runtime_get_sync(&pdev->dev);
3399 pm_runtime_put_noidle(&pdev->dev);
3400 goto clean_runtime_disable_ret;
3403 ret = cpsw_probe_dt(&cpsw->data, pdev);
3408 cpsw->rx_ch_num = 1;
3409 cpsw->tx_ch_num = 1;
3411 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3412 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
3413 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3415 eth_random_addr(priv->mac_addr);
3416 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3419 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3421 cpsw->slaves = devm_kcalloc(&pdev->dev,
3422 data->slaves, sizeof(struct cpsw_slave),
3424 if (!cpsw->slaves) {
3428 for (i = 0; i < data->slaves; i++)
3429 cpsw->slaves[i].slave_num = i;
3431 cpsw->slaves[0].ndev = ndev;
3432 priv->emac_port = 0;
3434 clk = devm_clk_get(&pdev->dev, "fck");
3436 dev_err(priv->dev, "fck is not found\n");
3440 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3442 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3443 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3444 if (IS_ERR(ss_regs)) {
3445 ret = PTR_ERR(ss_regs);
3448 cpsw->regs = ss_regs;
3450 cpsw->version = readl(&cpsw->regs->id_ver);
3452 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3453 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
3454 if (IS_ERR(cpsw->wr_regs)) {
3455 ret = PTR_ERR(cpsw->wr_regs);
3459 memset(&dma_params, 0, sizeof(dma_params));
3460 memset(&ale_params, 0, sizeof(ale_params));
3462 switch (cpsw->version) {
3463 case CPSW_VERSION_1:
3464 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
3465 cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
3466 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
3467 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
3468 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
3469 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
3470 slave_offset = CPSW1_SLAVE_OFFSET;
3471 slave_size = CPSW1_SLAVE_SIZE;
3472 sliver_offset = CPSW1_SLIVER_OFFSET;
3473 dma_params.desc_mem_phys = 0;
3475 case CPSW_VERSION_2:
3476 case CPSW_VERSION_3:
3477 case CPSW_VERSION_4:
3478 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
3479 cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
3480 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
3481 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
3482 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
3483 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
3484 slave_offset = CPSW2_SLAVE_OFFSET;
3485 slave_size = CPSW2_SLAVE_SIZE;
3486 sliver_offset = CPSW2_SLIVER_OFFSET;
3487 dma_params.desc_mem_phys =
3488 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3491 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3495 for (i = 0; i < cpsw->data.slaves; i++) {
3496 struct cpsw_slave *slave = &cpsw->slaves[i];
3498 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3499 slave_offset += slave_size;
3500 sliver_offset += SLIVER_SIZE;
3503 dma_params.dev = &pdev->dev;
3504 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
3505 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
3506 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
3507 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
3508 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
3510 dma_params.num_chan = data->channels;
3511 dma_params.has_soft_reset = true;
3512 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
3513 dma_params.desc_mem_size = data->bd_ram_size;
3514 dma_params.desc_align = 16;
3515 dma_params.has_ext_regs = true;
3516 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
3517 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
3518 dma_params.descs_pool_size = descs_pool_size;
3520 cpsw->dma = cpdma_ctlr_create(&dma_params);
3522 dev_err(priv->dev, "error initializing dma\n");
3527 soc = soc_device_match(cpsw_soc_devices);
3529 cpsw->quirk_irq = 1;
3531 ch = cpsw->quirk_irq ? 0 : 7;
3532 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
3533 if (IS_ERR(cpsw->txv[0].ch)) {
3534 dev_err(priv->dev, "error initializing tx dma channel\n");
3535 ret = PTR_ERR(cpsw->txv[0].ch);
3539 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
3540 if (IS_ERR(cpsw->rxv[0].ch)) {
3541 dev_err(priv->dev, "error initializing rx dma channel\n");
3542 ret = PTR_ERR(cpsw->rxv[0].ch);
3546 ale_params.dev = &pdev->dev;
3547 ale_params.ale_ageout = ale_ageout;
3548 ale_params.ale_entries = data->ale_entries;
3549 ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
3551 cpsw->ale = cpsw_ale_create(&ale_params);
3553 dev_err(priv->dev, "error initializing ale engine\n");
3558 cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
3559 if (IS_ERR(cpsw->cpts)) {
3560 ret = PTR_ERR(cpsw->cpts);
3564 ndev->irq = platform_get_irq(pdev, 1);
3565 if (ndev->irq < 0) {
3566 dev_err(priv->dev, "error getting irq resource\n");
3571 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3573 ndev->netdev_ops = &cpsw_netdev_ops;
3574 ndev->ethtool_ops = &cpsw_ethtool_ops;
3575 netif_napi_add(ndev, &cpsw->napi_rx,
3576 cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
3578 netif_tx_napi_add(ndev, &cpsw->napi_tx,
3579 cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
3581 cpsw_split_res(ndev);
3583 /* register the network device */
3584 SET_NETDEV_DEV(ndev, &pdev->dev);
3585 ret = register_netdev(ndev);
3587 dev_err(priv->dev, "error registering net device\n");
3592 if (cpsw->data.dual_emac) {
3593 ret = cpsw_probe_dual_emac(priv);
3595 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3596 goto clean_unregister_netdev_ret;
3600 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3601 * MISC IRQs which are always kept disabled with this driver so
3602 * we will not request them.
3604 * If anyone wants to implement support for those, make sure to
3605 * first request and append them to irqs_table array.
3609 irq = platform_get_irq(pdev, 1);
3615 cpsw->irqs_table[0] = irq;
3616 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3617 0, dev_name(&pdev->dev), cpsw);
3619 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3624 irq = platform_get_irq(pdev, 2);
3630 cpsw->irqs_table[1] = irq;
3631 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3632 0, dev_name(&pdev->dev), cpsw);
3634 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3638 cpsw_notice(priv, probe,
3639 "initialized device (regs %pa, irq %d, pool size %d)\n",
3640 &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3642 pm_runtime_put(&pdev->dev);
3646 clean_unregister_netdev_ret:
3647 unregister_netdev(ndev);
3649 cpdma_ctlr_destroy(cpsw->dma);
3651 cpsw_remove_dt(pdev);
3652 pm_runtime_put_sync(&pdev->dev);
3653 clean_runtime_disable_ret:
3654 pm_runtime_disable(&pdev->dev);
3656 free_netdev(priv->ndev);
3660 static int cpsw_remove(struct platform_device *pdev)
3662 struct net_device *ndev = platform_get_drvdata(pdev);
3663 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3666 ret = pm_runtime_get_sync(&pdev->dev);
3668 pm_runtime_put_noidle(&pdev->dev);
3672 if (cpsw->data.dual_emac)
3673 unregister_netdev(cpsw->slaves[1].ndev);
3674 unregister_netdev(ndev);
3676 cpts_release(cpsw->cpts);
3677 cpdma_ctlr_destroy(cpsw->dma);
3678 cpsw_remove_dt(pdev);
3679 pm_runtime_put_sync(&pdev->dev);
3680 pm_runtime_disable(&pdev->dev);
3681 if (cpsw->data.dual_emac)
3682 free_netdev(cpsw->slaves[1].ndev);
3687 #ifdef CONFIG_PM_SLEEP
3688 static int cpsw_suspend(struct device *dev)
3690 struct net_device *ndev = dev_get_drvdata(dev);
3691 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3693 if (cpsw->data.dual_emac) {
3696 for (i = 0; i < cpsw->data.slaves; i++) {
3697 if (netif_running(cpsw->slaves[i].ndev))
3698 cpsw_ndo_stop(cpsw->slaves[i].ndev);
3701 if (netif_running(ndev))
3702 cpsw_ndo_stop(ndev);
3705 /* Select sleep pin state */
3706 pinctrl_pm_select_sleep_state(dev);
3711 static int cpsw_resume(struct device *dev)
3713 struct net_device *ndev = dev_get_drvdata(dev);
3714 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3716 /* Select default pin state */
3717 pinctrl_pm_select_default_state(dev);
3719 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3721 if (cpsw->data.dual_emac) {
3724 for (i = 0; i < cpsw->data.slaves; i++) {
3725 if (netif_running(cpsw->slaves[i].ndev))
3726 cpsw_ndo_open(cpsw->slaves[i].ndev);
3729 if (netif_running(ndev))
3730 cpsw_ndo_open(ndev);
3738 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3740 static struct platform_driver cpsw_driver = {
3744 .of_match_table = cpsw_of_mtable,
3746 .probe = cpsw_probe,
3747 .remove = cpsw_remove,
3750 module_platform_driver(cpsw_driver);
3752 MODULE_LICENSE("GPL");
3753 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3754 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3755 MODULE_DESCRIPTION("TI CPSW Ethernet driver");