1 // SPDX-License-Identifier: GPL-2.0
2 /* Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver
4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
9 #include <linux/etherdevice.h>
10 #include <linux/if_vlan.h>
11 #include <linux/interrupt.h>
12 #include <linux/irqdomain.h>
13 #include <linux/kernel.h>
14 #include <linux/kmemleak.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/net_tstamp.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_net.h>
21 #include <linux/of_device.h>
22 #include <linux/of_platform.h>
23 #include <linux/phylink.h>
24 #include <linux/phy/phy.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regmap.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/mfd/syscon.h>
30 #include <linux/sys_soc.h>
31 #include <linux/dma/ti-cppi5.h>
32 #include <linux/dma/k3-udma-glue.h>
33 #include <net/switchdev.h>
37 #include "am65-cpsw-nuss.h"
38 #include "am65-cpsw-switchdev.h"
39 #include "k3-cppi-desc-pool.h"
40 #include "am65-cpts.h"
42 #define AM65_CPSW_SS_BASE 0x0
43 #define AM65_CPSW_SGMII_BASE 0x100
44 #define AM65_CPSW_XGMII_BASE 0x2100
45 #define AM65_CPSW_CPSW_NU_BASE 0x20000
46 #define AM65_CPSW_NU_PORTS_BASE 0x1000
47 #define AM65_CPSW_NU_FRAM_BASE 0x12000
48 #define AM65_CPSW_NU_STATS_BASE 0x1a000
49 #define AM65_CPSW_NU_ALE_BASE 0x1e000
50 #define AM65_CPSW_NU_CPTS_BASE 0x1d000
52 #define AM65_CPSW_NU_PORTS_OFFSET 0x1000
53 #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200
54 #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200
56 #define AM65_CPSW_MAX_PORTS 8
58 #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN
59 #define AM65_CPSW_MAX_PACKET_SIZE 2024
61 #define AM65_CPSW_REG_CTL 0x004
62 #define AM65_CPSW_REG_STAT_PORT_EN 0x014
63 #define AM65_CPSW_REG_PTYPE 0x018
65 #define AM65_CPSW_P0_REG_CTL 0x004
66 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008
68 #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c
69 #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020
70 #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024
72 #define AM65_CPSW_PORTN_REG_SA_L 0x308
73 #define AM65_CPSW_PORTN_REG_SA_H 0x30c
74 #define AM65_CPSW_PORTN_REG_TS_CTL 0x310
75 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314
76 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318
77 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C
79 #define AM65_CPSW_SGMII_CONTROL_REG 0x010
80 #define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG 0x018
81 #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0)
83 #define AM65_CPSW_CTL_VLAN_AWARE BIT(1)
84 #define AM65_CPSW_CTL_P0_ENABLE BIT(2)
85 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13)
86 #define AM65_CPSW_CTL_P0_RX_PAD BIT(14)
88 /* AM65_CPSW_P0_REG_CTL */
89 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0)
90 #define AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN BIT(16)
92 /* AM65_CPSW_PORT_REG_PRI_CTL */
93 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8)
95 /* AM65_CPSW_PN_TS_CTL register fields */
96 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4)
97 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5)
98 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6)
99 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7)
100 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10)
101 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11)
102 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16
104 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
105 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16
107 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
108 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16)
109 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17)
110 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18)
111 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19)
112 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20)
113 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21)
114 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22)
115 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
117 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
118 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
120 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
122 #define AM65_CPSW_TS_TX_ANX_ALL_EN \
123 (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \
124 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \
125 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
127 #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30
128 /* Number of TX/RX descriptors */
129 #define AM65_CPSW_MAX_TX_DESC 500
130 #define AM65_CPSW_MAX_RX_DESC 500
132 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
133 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
135 #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
136 NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
137 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
139 #define AM65_CPSW_DEFAULT_TX_CHNS 8
141 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
144 u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
145 (dev_addr[2] << 16) | (dev_addr[3] << 24);
146 u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
148 writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
149 writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
152 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
154 cpsw_sl_reset(port->slave.mac_sl, 100);
155 /* Max length register has to be restored after MAC SL reset */
156 writel(AM65_CPSW_MAX_PACKET_SIZE,
157 port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
160 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
162 common->nuss_ver = readl(common->ss_base);
163 common->cpsw_ver = readl(common->cpsw_base);
164 dev_info(common->dev,
165 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
168 common->port_num + 1,
169 common->pdata.quirks);
172 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
173 __be16 proto, u16 vid)
175 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
176 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
177 u32 port_mask, unreg_mcast = 0;
180 if (!common->is_emac_mode)
183 if (!netif_running(ndev) || !vid)
186 ret = pm_runtime_resume_and_get(common->dev);
190 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
192 unreg_mcast = port_mask;
193 dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
194 ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask,
195 unreg_mcast, port_mask, 0);
197 pm_runtime_put(common->dev);
201 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
202 __be16 proto, u16 vid)
204 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
205 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
208 if (!common->is_emac_mode)
211 if (!netif_running(ndev) || !vid)
214 ret = pm_runtime_resume_and_get(common->dev);
218 dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
219 ret = cpsw_ale_del_vlan(common->ale, vid,
220 BIT(port->port_id) | ALE_PORT_HOST);
222 pm_runtime_put(common->dev);
226 static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port,
229 struct am65_cpsw_common *common = port->common;
231 if (promisc && !common->is_emac_mode) {
232 dev_dbg(common->dev, "promisc mode requested in switch mode");
237 /* Enable promiscuous mode */
238 cpsw_ale_control_set(common->ale, port->port_id,
239 ALE_PORT_MACONLY_CAF, 1);
240 dev_dbg(common->dev, "promisc enabled\n");
242 /* Disable promiscuous mode */
243 cpsw_ale_control_set(common->ale, port->port_id,
244 ALE_PORT_MACONLY_CAF, 0);
245 dev_dbg(common->dev, "promisc disabled\n");
249 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
251 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
252 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
256 promisc = !!(ndev->flags & IFF_PROMISC);
257 am65_cpsw_slave_set_promisc(port, promisc);
262 /* Restore allmulti on vlans if necessary */
263 cpsw_ale_set_allmulti(common->ale,
264 ndev->flags & IFF_ALLMULTI, port->port_id);
266 port_mask = ALE_PORT_HOST;
267 /* Clear all mcast from ALE */
268 cpsw_ale_flush_multicast(common->ale, port_mask, -1);
270 if (!netdev_mc_empty(ndev)) {
271 struct netdev_hw_addr *ha;
273 /* program multicast address list into ALE register */
274 netdev_for_each_mc_addr(ha, ndev) {
275 cpsw_ale_add_mcast(common->ale, ha->addr,
281 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
282 unsigned int txqueue)
284 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
285 struct am65_cpsw_tx_chn *tx_chn;
286 struct netdev_queue *netif_txq;
287 unsigned long trans_start;
289 netif_txq = netdev_get_tx_queue(ndev, txqueue);
290 tx_chn = &common->tx_chns[txqueue];
291 trans_start = READ_ONCE(netif_txq->trans_start);
293 netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
295 netif_tx_queue_stopped(netif_txq),
296 jiffies_to_msecs(jiffies - trans_start),
297 netdev_queue_dql_avail(netif_txq),
298 k3_cppi_desc_pool_avail(tx_chn->desc_pool));
300 if (netif_tx_queue_stopped(netif_txq)) {
301 /* try recover if stopped by us */
302 txq_trans_update(netif_txq);
303 netif_tx_wake_queue(netif_txq);
307 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
310 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
311 struct cppi5_host_desc_t *desc_rx;
312 struct device *dev = common->dev;
313 u32 pkt_len = skb_tailroom(skb);
318 desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
320 dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
323 desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
325 buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len,
327 if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) {
328 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
329 dev_err(dev, "Failed to map rx skb buffer\n");
333 cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
334 AM65_CPSW_NAV_PS_DATA_SIZE);
335 k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma);
336 cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb));
337 swdata = cppi5_hdesc_get_swdata(desc_rx);
338 *((void **)swdata) = skb;
340 return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
343 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
345 struct am65_cpsw_host *host_p = am65_common_get_host(common);
348 /* P0 set Receive Priority Type */
349 val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
351 if (common->pf_p0_rx_ptype_rrobin) {
352 val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
353 /* Enet Ports fifos works in fixed priority mode only, so
354 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
358 val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
359 /* restore P0_Rx_Pri_Map */
360 pri_map = 0x76543210;
363 writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
364 writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
367 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common);
368 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common);
369 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port);
370 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port);
372 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
374 struct am65_cpsw_rx_chn *rx_chn = data;
375 struct cppi5_host_desc_t *desc_rx;
381 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
382 swdata = cppi5_hdesc_get_swdata(desc_rx);
384 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
385 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
387 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
388 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
390 dev_kfree_skb_any(skb);
393 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
394 struct cppi5_host_desc_t *desc)
396 struct cppi5_host_desc_t *first_desc, *next_desc;
397 dma_addr_t buf_dma, next_desc_dma;
401 next_desc = first_desc;
403 cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
404 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
406 dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
408 next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
409 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
410 while (next_desc_dma) {
411 next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
413 cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
414 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
416 dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len,
419 next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
420 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
422 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
425 k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
428 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
430 struct am65_cpsw_tx_chn *tx_chn = data;
431 struct cppi5_host_desc_t *desc_tx;
435 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
436 swdata = cppi5_hdesc_get_swdata(desc_tx);
438 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
440 dev_kfree_skb_any(skb);
443 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common)
445 struct am65_cpsw_host *host_p = am65_common_get_host(common);
446 int port_idx, i, ret, tx;
450 if (common->usage_count)
453 /* Control register */
454 writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
455 AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
456 common->cpsw_base + AM65_CPSW_REG_CTL);
457 /* Max length register */
458 writel(AM65_CPSW_MAX_PACKET_SIZE,
459 host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
460 /* set base flow_id */
461 writel(common->rx_flow_id_base,
462 host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
463 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN | AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN,
464 host_p->port_base + AM65_CPSW_P0_REG_CTL);
466 am65_cpsw_nuss_set_p0_ptype(common);
468 /* enable statistic */
469 val = BIT(HOST_PORT_NUM);
470 for (port_idx = 0; port_idx < common->port_num; port_idx++) {
471 struct am65_cpsw_port *port = &common->ports[port_idx];
474 val |= BIT(port->port_id);
476 writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
478 /* disable priority elevation */
479 writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
481 cpsw_ale_start(common->ale);
483 /* limit to one RX flow only */
484 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
485 ALE_DEFAULT_THREAD_ID, 0);
486 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
487 ALE_DEFAULT_THREAD_ENABLE, 1);
488 /* switch to vlan unaware mode */
489 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
490 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
491 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
493 /* default vlan cfg: create mask based on enabled ports */
494 port_mask = GENMASK(common->port_num, 0) &
495 ~common->disabled_ports_mask;
497 cpsw_ale_add_vlan(common->ale, 0, port_mask,
498 port_mask, port_mask,
499 port_mask & ~ALE_PORT_HOST);
501 if (common->is_emac_mode)
502 am65_cpsw_init_host_port_emac(common);
504 am65_cpsw_init_host_port_switch(common);
506 am65_cpsw_qos_tx_p0_rate_init(common);
508 for (i = 0; i < common->rx_chns.descs_num; i++) {
509 skb = __netdev_alloc_skb_ip_align(NULL,
510 AM65_CPSW_MAX_PACKET_SIZE,
514 dev_err(common->dev, "cannot allocate skb\n");
521 ret = am65_cpsw_nuss_rx_push(common, skb);
524 "cannot submit skb to channel rx, error %d\n",
534 ret = k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
536 dev_err(common->dev, "couldn't enable rx chn: %d\n", ret);
540 for (tx = 0; tx < common->tx_ch_num; tx++) {
541 ret = k3_udma_glue_enable_tx_chn(common->tx_chns[tx].tx_chn);
543 dev_err(common->dev, "couldn't enable tx chn %d: %d\n",
548 napi_enable(&common->tx_chns[tx].napi_tx);
551 napi_enable(&common->napi_rx);
552 if (common->rx_irq_disabled) {
553 common->rx_irq_disabled = false;
554 enable_irq(common->rx_chns.irq);
557 dev_dbg(common->dev, "cpsw_nuss started\n");
562 napi_disable(&common->tx_chns[tx].napi_tx);
563 k3_udma_glue_disable_tx_chn(common->tx_chns[tx].tx_chn);
567 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
570 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, 0,
572 am65_cpsw_nuss_rx_cleanup, 0);
576 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
580 if (common->usage_count != 1)
583 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
584 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
586 /* shutdown tx channels */
587 atomic_set(&common->tdown_cnt, common->tx_ch_num);
588 /* ensure new tdown_cnt value is visible */
589 smp_mb__after_atomic();
590 reinit_completion(&common->tdown_complete);
592 for (i = 0; i < common->tx_ch_num; i++)
593 k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
595 i = wait_for_completion_timeout(&common->tdown_complete,
596 msecs_to_jiffies(1000));
598 dev_err(common->dev, "tx timeout\n");
599 for (i = 0; i < common->tx_ch_num; i++) {
600 napi_disable(&common->tx_chns[i].napi_tx);
601 hrtimer_cancel(&common->tx_chns[i].tx_hrtimer);
604 for (i = 0; i < common->tx_ch_num; i++) {
605 k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
607 am65_cpsw_nuss_tx_cleanup);
608 k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
611 reinit_completion(&common->tdown_complete);
612 k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
614 if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) {
615 i = wait_for_completion_timeout(&common->tdown_complete, msecs_to_jiffies(1000));
617 dev_err(common->dev, "rx teardown timeout\n");
620 napi_disable(&common->napi_rx);
621 hrtimer_cancel(&common->rx_hrtimer);
623 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
624 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
626 am65_cpsw_nuss_rx_cleanup, !!i);
628 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
630 cpsw_ale_stop(common->ale);
632 writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
633 writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
635 dev_dbg(common->dev, "cpsw_nuss stopped\n");
639 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
641 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
642 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
645 phylink_stop(port->slave.phylink);
647 netif_tx_stop_all_queues(ndev);
649 phylink_disconnect_phy(port->slave.phylink);
651 ret = am65_cpsw_nuss_common_stop(common);
655 common->usage_count--;
656 pm_runtime_put(common->dev);
660 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
662 struct am65_cpsw_port *port = arg;
667 return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
670 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
672 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
673 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
677 ret = pm_runtime_resume_and_get(common->dev);
682 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
683 cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
684 cpsw_sl_ctl_reset(port->slave.mac_sl);
687 cpsw_sl_reg_write(port->slave.mac_sl, CPSW_SL_SOFT_RESET, 1);
689 reg = cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_SOFT_RESET);
691 dev_err(common->dev, "soft RESET didn't complete\n");
696 /* Notify the stack of the actual queue counts. */
697 ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
699 dev_err(common->dev, "cannot set real number of tx queues\n");
703 ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
705 dev_err(common->dev, "cannot set real number of rx queues\n");
709 for (i = 0; i < common->tx_ch_num; i++) {
710 struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
712 netdev_tx_reset_queue(txq);
713 txq->tx_maxrate = common->tx_chns[i].rate_mbps;
716 ret = am65_cpsw_nuss_common_open(common);
720 common->usage_count++;
722 am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
724 if (common->is_emac_mode)
725 am65_cpsw_init_port_emac_ale(port);
727 am65_cpsw_init_port_switch_ale(port);
729 /* mac_sl should be configured via phy-link interface */
730 am65_cpsw_sl_ctl_reset(port);
732 ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0);
736 /* restore vlan configurations */
737 vlan_for_each(ndev, cpsw_restore_vlans, port);
739 phylink_start(port->slave.phylink);
744 am65_cpsw_nuss_ndo_slave_stop(ndev);
748 pm_runtime_put(common->dev);
752 static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata)
754 struct skb_shared_hwtstamps *ssh;
757 ns = ((u64)psdata[1] << 32) | psdata[0];
759 ssh = skb_hwtstamps(skb);
760 memset(ssh, 0, sizeof(*ssh));
761 ssh->hwtstamp = ns_to_ktime(ns);
764 /* RX psdata[2] word format - checksum information */
765 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0)
766 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16)
767 #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17)
768 #define AM65_CPSW_RX_PSD_IS_TCP BIT(18)
769 #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19)
770 #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20)
772 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
774 /* HW can verify IPv4/IPv6 TCP/UDP packets checksum
775 * csum information provides in psdata[2] word:
776 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
777 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
778 * bits - indicates IPv4/IPv6 packet
779 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
780 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
781 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
783 skb_checksum_none_assert(skb);
785 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
788 if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
789 AM65_CPSW_RX_PSD_IPV4_VALID)) &&
790 !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
791 /* csum for fragmented packets is unsupported */
792 if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
793 skb->ip_summed = CHECKSUM_UNNECESSARY;
797 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
800 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
801 u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
802 struct am65_cpsw_ndev_priv *ndev_priv;
803 struct am65_cpsw_ndev_stats *stats;
804 struct cppi5_host_desc_t *desc_rx;
805 struct device *dev = common->dev;
806 struct sk_buff *skb, *new_skb;
807 dma_addr_t desc_dma, buf_dma;
808 struct am65_cpsw_port *port;
809 struct net_device *ndev;
814 ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
817 dev_err(dev, "RX: pop chn fail %d\n", ret);
821 if (cppi5_desc_is_tdcm(desc_dma)) {
822 dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
823 if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ)
824 complete(&common->tdown_complete);
828 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
829 dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
830 __func__, flow_idx, &desc_dma);
832 swdata = cppi5_hdesc_get_swdata(desc_rx);
834 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
835 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
836 pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
837 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
838 dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
839 port = am65_common_get_port(common, port_id);
843 psdata = cppi5_hdesc_get_psdata(desc_rx);
844 /* add RX timestamp */
845 if (port->rx_ts_enabled)
846 am65_cpsw_nuss_rx_ts(skb, psdata);
847 csum_info = psdata[2];
848 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
850 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
852 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
854 new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
856 ndev_priv = netdev_priv(ndev);
857 am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark);
858 skb_put(skb, pkt_len);
859 skb->protocol = eth_type_trans(skb, ndev);
860 am65_cpsw_nuss_rx_csum(skb, csum_info);
861 napi_gro_receive(&common->napi_rx, skb);
863 stats = this_cpu_ptr(ndev_priv->stats);
865 u64_stats_update_begin(&stats->syncp);
867 stats->rx_bytes += pkt_len;
868 u64_stats_update_end(&stats->syncp);
869 kmemleak_not_leak(new_skb);
871 ndev->stats.rx_dropped++;
875 if (netif_dormant(ndev)) {
876 dev_kfree_skb_any(new_skb);
877 ndev->stats.rx_dropped++;
881 ret = am65_cpsw_nuss_rx_push(common, new_skb);
882 if (WARN_ON(ret < 0)) {
883 dev_kfree_skb_any(new_skb);
884 ndev->stats.rx_errors++;
885 ndev->stats.rx_dropped++;
891 static enum hrtimer_restart am65_cpsw_nuss_rx_timer_callback(struct hrtimer *timer)
893 struct am65_cpsw_common *common =
894 container_of(timer, struct am65_cpsw_common, rx_hrtimer);
896 enable_irq(common->rx_chns.irq);
897 return HRTIMER_NORESTART;
900 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
902 struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
903 int flow = AM65_CPSW_MAX_RX_FLOWS;
907 /* process every flow */
909 cur_budget = budget - num_rx;
911 while (cur_budget--) {
912 ret = am65_cpsw_nuss_rx_packets(common, flow);
918 if (num_rx >= budget)
922 dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
924 if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) {
925 if (common->rx_irq_disabled) {
926 common->rx_irq_disabled = false;
927 if (unlikely(common->rx_pace_timeout)) {
928 hrtimer_start(&common->rx_hrtimer,
929 ns_to_ktime(common->rx_pace_timeout),
930 HRTIMER_MODE_REL_PINNED);
932 enable_irq(common->rx_chns.irq);
940 static struct sk_buff *
941 am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn,
944 struct am65_cpsw_ndev_priv *ndev_priv;
945 struct am65_cpsw_ndev_stats *stats;
946 struct cppi5_host_desc_t *desc_tx;
947 struct net_device *ndev;
951 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
953 swdata = cppi5_hdesc_get_swdata(desc_tx);
955 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
959 am65_cpts_tx_timestamp(tx_chn->common->cpts, skb);
961 ndev_priv = netdev_priv(ndev);
962 stats = this_cpu_ptr(ndev_priv->stats);
963 u64_stats_update_begin(&stats->syncp);
965 stats->tx_bytes += skb->len;
966 u64_stats_update_end(&stats->syncp);
971 static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev,
972 struct netdev_queue *netif_txq)
974 if (netif_tx_queue_stopped(netif_txq)) {
975 /* Check whether the queue is stopped due to stalled
976 * tx dma, if the queue is stopped then wake the queue
977 * as we have free desc for tx
979 __netif_tx_lock(netif_txq, smp_processor_id());
980 if (netif_running(ndev) &&
981 (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS))
982 netif_tx_wake_queue(netif_txq);
984 __netif_tx_unlock(netif_txq);
988 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
989 int chn, unsigned int budget, bool *tdown)
991 struct device *dev = common->dev;
992 struct am65_cpsw_tx_chn *tx_chn;
993 struct netdev_queue *netif_txq;
994 unsigned int total_bytes = 0;
995 struct net_device *ndev;
1000 tx_chn = &common->tx_chns[chn];
1003 spin_lock(&tx_chn->lock);
1004 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
1005 spin_unlock(&tx_chn->lock);
1006 if (res == -ENODATA)
1009 if (cppi5_desc_is_tdcm(desc_dma)) {
1010 if (atomic_dec_and_test(&common->tdown_cnt))
1011 complete(&common->tdown_complete);
1016 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
1017 total_bytes = skb->len;
1019 napi_consume_skb(skb, budget);
1022 netif_txq = netdev_get_tx_queue(ndev, chn);
1024 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
1026 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
1029 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
1034 static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common,
1035 int chn, unsigned int budget, bool *tdown)
1037 struct device *dev = common->dev;
1038 struct am65_cpsw_tx_chn *tx_chn;
1039 struct netdev_queue *netif_txq;
1040 unsigned int total_bytes = 0;
1041 struct net_device *ndev;
1042 struct sk_buff *skb;
1043 dma_addr_t desc_dma;
1044 int res, num_tx = 0;
1046 tx_chn = &common->tx_chns[chn];
1049 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
1050 if (res == -ENODATA)
1053 if (cppi5_desc_is_tdcm(desc_dma)) {
1054 if (atomic_dec_and_test(&common->tdown_cnt))
1055 complete(&common->tdown_complete);
1060 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
1063 total_bytes += skb->len;
1064 napi_consume_skb(skb, budget);
1071 netif_txq = netdev_get_tx_queue(ndev, chn);
1073 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
1075 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
1077 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
1082 static enum hrtimer_restart am65_cpsw_nuss_tx_timer_callback(struct hrtimer *timer)
1084 struct am65_cpsw_tx_chn *tx_chns =
1085 container_of(timer, struct am65_cpsw_tx_chn, tx_hrtimer);
1087 enable_irq(tx_chns->irq);
1088 return HRTIMER_NORESTART;
1091 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
1093 struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
1097 if (AM65_CPSW_IS_CPSW2G(tx_chn->common))
1098 num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id,
1101 num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common,
1102 tx_chn->id, budget, &tdown);
1104 if (num_tx >= budget)
1107 if (napi_complete_done(napi_tx, num_tx)) {
1108 if (unlikely(tx_chn->tx_pace_timeout && !tdown)) {
1109 hrtimer_start(&tx_chn->tx_hrtimer,
1110 ns_to_ktime(tx_chn->tx_pace_timeout),
1111 HRTIMER_MODE_REL_PINNED);
1113 enable_irq(tx_chn->irq);
1120 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
1122 struct am65_cpsw_common *common = dev_id;
1124 common->rx_irq_disabled = true;
1125 disable_irq_nosync(irq);
1126 napi_schedule(&common->napi_rx);
1131 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
1133 struct am65_cpsw_tx_chn *tx_chn = dev_id;
1135 disable_irq_nosync(irq);
1136 napi_schedule(&tx_chn->napi_tx);
1141 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
1142 struct net_device *ndev)
1144 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1145 struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
1146 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1147 struct device *dev = common->dev;
1148 struct am65_cpsw_tx_chn *tx_chn;
1149 struct netdev_queue *netif_txq;
1150 dma_addr_t desc_dma, buf_dma;
1156 /* padding enabled in hw */
1157 pkt_len = skb_headlen(skb);
1159 /* SKB TX timestamp */
1160 if (port->tx_ts_enabled)
1161 am65_cpts_prep_tx_timestamp(common->cpts, skb);
1163 q_idx = skb_get_queue_mapping(skb);
1164 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1166 tx_chn = &common->tx_chns[q_idx];
1167 netif_txq = netdev_get_tx_queue(ndev, q_idx);
1169 /* Map the linear buffer */
1170 buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len,
1172 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1173 dev_err(dev, "Failed to map tx skb buffer\n");
1174 ndev->stats.tx_errors++;
1178 first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1180 dev_dbg(dev, "Failed to allocate descriptor\n");
1181 dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len,
1186 cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1187 AM65_CPSW_NAV_PS_DATA_SIZE);
1188 cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
1189 cppi5_hdesc_set_pkttype(first_desc, 0x7);
1190 cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1192 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1193 cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1194 swdata = cppi5_hdesc_get_swdata(first_desc);
1196 psdata = cppi5_hdesc_get_psdata(first_desc);
1198 /* HW csum offload if enabled */
1200 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1201 unsigned int cs_start, cs_offset;
1203 cs_start = skb_transport_offset(skb);
1204 cs_offset = cs_start + skb->csum_offset;
1205 /* HW numerates bytes starting from 1 */
1206 psdata[2] = ((cs_offset + 1) << 24) |
1207 ((cs_start + 1) << 16) | (skb->len - cs_start);
1208 dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1211 if (!skb_is_nonlinear(skb))
1214 dev_dbg(dev, "fragmented SKB\n");
1216 /* Handle the case where skb is fragmented in pages */
1217 cur_desc = first_desc;
1218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1219 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1220 u32 frag_size = skb_frag_size(frag);
1222 next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1224 dev_err(dev, "Failed to allocate descriptor\n");
1225 goto busy_free_descs;
1228 buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size,
1230 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1231 dev_err(dev, "Failed to map tx skb page\n");
1232 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1233 ndev->stats.tx_errors++;
1234 goto err_free_descs;
1237 cppi5_hdesc_reset_hbdesc(next_desc);
1238 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1239 cppi5_hdesc_attach_buf(next_desc,
1240 buf_dma, frag_size, buf_dma, frag_size);
1242 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1244 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma);
1245 cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1247 pkt_len += frag_size;
1248 cur_desc = next_desc;
1250 WARN_ON(pkt_len != skb->len);
1253 skb_tx_timestamp(skb);
1255 /* report bql before sending packet */
1256 netdev_tx_sent_queue(netif_txq, pkt_len);
1258 cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1259 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1260 if (AM65_CPSW_IS_CPSW2G(common)) {
1261 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1263 spin_lock_bh(&tx_chn->lock);
1264 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1265 spin_unlock_bh(&tx_chn->lock);
1268 dev_err(dev, "can't push desc %d\n", ret);
1270 netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1271 ndev->stats.tx_errors++;
1272 goto err_free_descs;
1275 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1276 netif_tx_stop_queue(netif_txq);
1277 /* Barrier, so that stop_queue visible to other cpus */
1278 smp_mb__after_atomic();
1279 dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1281 /* re-check for smp */
1282 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1284 netif_tx_wake_queue(netif_txq);
1285 dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1289 return NETDEV_TX_OK;
1292 am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1294 ndev->stats.tx_dropped++;
1295 dev_kfree_skb_any(skb);
1296 return NETDEV_TX_OK;
1299 am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1301 netif_tx_stop_queue(netif_txq);
1302 return NETDEV_TX_BUSY;
1305 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1308 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1309 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1310 struct sockaddr *sockaddr = (struct sockaddr *)addr;
1313 ret = eth_prepare_mac_addr_change(ndev, addr);
1317 ret = pm_runtime_resume_and_get(common->dev);
1321 cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1322 HOST_PORT_NUM, 0, 0);
1323 cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1324 HOST_PORT_NUM, ALE_SECURE, 0);
1326 am65_cpsw_port_set_sl_mac(port, addr);
1327 eth_commit_mac_addr_change(ndev, sockaddr);
1329 pm_runtime_put(common->dev);
1334 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
1337 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1338 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1339 u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
1340 struct hwtstamp_config cfg;
1342 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1345 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1348 /* TX HW timestamp */
1349 switch (cfg.tx_type) {
1350 case HWTSTAMP_TX_OFF:
1351 case HWTSTAMP_TX_ON:
1357 switch (cfg.rx_filter) {
1358 case HWTSTAMP_FILTER_NONE:
1359 port->rx_ts_enabled = false;
1361 case HWTSTAMP_FILTER_ALL:
1362 case HWTSTAMP_FILTER_SOME:
1363 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1364 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1365 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1366 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1367 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1368 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1369 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1370 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1371 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1372 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1373 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1374 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1375 case HWTSTAMP_FILTER_NTP_ALL:
1376 port->rx_ts_enabled = true;
1377 cfg.rx_filter = HWTSTAMP_FILTER_ALL;
1383 port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
1385 /* cfg TX timestamp */
1386 seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
1387 AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
1389 ts_vlan_ltype = ETH_P_8021Q;
1391 ts_ctrl_ltype2 = ETH_P_1588 |
1392 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
1393 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
1394 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
1395 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
1396 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
1397 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
1398 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
1399 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
1401 ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
1402 AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
1404 if (port->tx_ts_enabled)
1405 ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
1406 AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
1408 writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
1409 writel(ts_vlan_ltype, port->port_base +
1410 AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
1411 writel(ts_ctrl_ltype2, port->port_base +
1412 AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
1413 writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
1415 /* en/dis RX timestamp */
1416 am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled);
1418 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1421 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
1424 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1425 struct hwtstamp_config cfg;
1427 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1431 cfg.tx_type = port->tx_ts_enabled ?
1432 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1433 cfg.rx_filter = port->rx_ts_enabled ?
1434 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
1436 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1439 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1440 struct ifreq *req, int cmd)
1442 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1444 if (!netif_running(ndev))
1449 return am65_cpsw_nuss_hwtstamp_set(ndev, req);
1451 return am65_cpsw_nuss_hwtstamp_get(ndev, req);
1454 return phylink_mii_ioctl(port->slave.phylink, req, cmd);
1457 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1458 struct rtnl_link_stats64 *stats)
1460 struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1464 for_each_possible_cpu(cpu) {
1465 struct am65_cpsw_ndev_stats *cpu_stats;
1471 cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1473 start = u64_stats_fetch_begin(&cpu_stats->syncp);
1474 rx_packets = cpu_stats->rx_packets;
1475 rx_bytes = cpu_stats->rx_bytes;
1476 tx_packets = cpu_stats->tx_packets;
1477 tx_bytes = cpu_stats->tx_bytes;
1478 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
1480 stats->rx_packets += rx_packets;
1481 stats->rx_bytes += rx_bytes;
1482 stats->tx_packets += tx_packets;
1483 stats->tx_bytes += tx_bytes;
1486 stats->rx_errors = dev->stats.rx_errors;
1487 stats->rx_dropped = dev->stats.rx_dropped;
1488 stats->tx_dropped = dev->stats.tx_dropped;
1491 static const struct net_device_ops am65_cpsw_nuss_netdev_ops = {
1492 .ndo_open = am65_cpsw_nuss_ndo_slave_open,
1493 .ndo_stop = am65_cpsw_nuss_ndo_slave_stop,
1494 .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit,
1495 .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode,
1496 .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats,
1497 .ndo_validate_addr = eth_validate_addr,
1498 .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address,
1499 .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout,
1500 .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid,
1501 .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid,
1502 .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl,
1503 .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc,
1504 .ndo_set_tx_maxrate = am65_cpsw_qos_ndo_tx_p0_set_maxrate,
1507 static void am65_cpsw_disable_phy(struct phy *phy)
1513 static int am65_cpsw_enable_phy(struct phy *phy)
1517 ret = phy_init(phy);
1521 ret = phy_power_on(phy);
1530 static void am65_cpsw_disable_serdes_phy(struct am65_cpsw_common *common)
1532 struct am65_cpsw_port *port;
1536 for (i = 0; i < common->port_num; i++) {
1537 port = &common->ports[i];
1538 phy = port->slave.serdes_phy;
1540 am65_cpsw_disable_phy(phy);
1544 static int am65_cpsw_init_serdes_phy(struct device *dev, struct device_node *port_np,
1545 struct am65_cpsw_port *port)
1547 const char *name = "serdes";
1551 phy = devm_of_phy_optional_get(dev, port_np, name);
1552 if (IS_ERR_OR_NULL(phy))
1553 return PTR_ERR_OR_ZERO(phy);
1555 /* Serdes PHY exists. Store it. */
1556 port->slave.serdes_phy = phy;
1558 ret = am65_cpsw_enable_phy(phy);
1565 devm_phy_put(dev, phy);
1569 static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode,
1570 const struct phylink_link_state *state)
1572 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1574 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1575 struct am65_cpsw_common *common = port->common;
1577 if (common->pdata.extra_modes & BIT(state->interface)) {
1578 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
1579 writel(ADVERTISE_SGMII,
1580 port->sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG);
1581 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN);
1583 cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN);
1586 if (state->interface == PHY_INTERFACE_MODE_USXGMII) {
1587 cpsw_sl_ctl_set(port->slave.mac_sl,
1588 CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN);
1590 cpsw_sl_ctl_clr(port->slave.mac_sl,
1591 CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN);
1594 writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE,
1595 port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG);
1599 static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode,
1600 phy_interface_t interface)
1602 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1604 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1605 struct am65_cpsw_common *common = port->common;
1606 struct net_device *ndev = port->ndev;
1610 /* disable forwarding */
1611 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1613 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
1615 tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
1616 dev_dbg(common->dev, "down msc_sl %08x tmo %d\n",
1617 cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo);
1619 /* All the bits that am65_cpsw_nuss_mac_link_up() can possibly set */
1620 mac_control = CPSW_SL_CTL_GMII_EN | CPSW_SL_CTL_GIG | CPSW_SL_CTL_IFCTL_A |
1621 CPSW_SL_CTL_FULLDUPLEX | CPSW_SL_CTL_RX_FLOW_EN | CPSW_SL_CTL_TX_FLOW_EN;
1622 /* If interface mode is RGMII, CPSW_SL_CTL_EXT_EN might have been set for 10 Mbps */
1623 if (phy_interface_mode_is_rgmii(interface))
1624 mac_control |= CPSW_SL_CTL_EXT_EN;
1625 /* Only clear those bits that can be set by am65_cpsw_nuss_mac_link_up() */
1626 cpsw_sl_ctl_clr(port->slave.mac_sl, mac_control);
1628 am65_cpsw_qos_link_down(ndev);
1629 netif_tx_stop_all_queues(ndev);
1632 static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy,
1633 unsigned int mode, phy_interface_t interface, int speed,
1634 int duplex, bool tx_pause, bool rx_pause)
1636 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1638 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1639 struct am65_cpsw_common *common = port->common;
1640 u32 mac_control = CPSW_SL_CTL_GMII_EN;
1641 struct net_device *ndev = port->ndev;
1643 /* Bring the port out of idle state */
1644 cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
1646 if (speed == SPEED_1000)
1647 mac_control |= CPSW_SL_CTL_GIG;
1648 /* TODO: Verify whether in-band is necessary for 10 Mbps RGMII */
1649 if (speed == SPEED_10 && phy_interface_mode_is_rgmii(interface))
1650 /* Can be used with in band mode only */
1651 mac_control |= CPSW_SL_CTL_EXT_EN;
1652 if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII)
1653 mac_control |= CPSW_SL_CTL_IFCTL_A;
1655 mac_control |= CPSW_SL_CTL_FULLDUPLEX;
1657 /* rx_pause/tx_pause */
1659 mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
1662 mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
1664 cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
1666 /* enable forwarding */
1667 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1669 am65_cpsw_qos_link_up(ndev, speed);
1670 netif_tx_wake_all_queues(ndev);
1673 static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = {
1674 .mac_config = am65_cpsw_nuss_mac_config,
1675 .mac_link_down = am65_cpsw_nuss_mac_link_down,
1676 .mac_link_up = am65_cpsw_nuss_mac_link_up,
1679 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
1681 struct am65_cpsw_common *common = port->common;
1683 if (!port->disabled)
1686 cpsw_ale_control_set(common->ale, port->port_id,
1687 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1689 cpsw_sl_reset(port->slave.mac_sl, 100);
1690 cpsw_sl_ctl_reset(port->slave.mac_sl);
1693 static void am65_cpsw_nuss_free_tx_chns(void *data)
1695 struct am65_cpsw_common *common = data;
1698 for (i = 0; i < common->tx_ch_num; i++) {
1699 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1701 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1702 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1704 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1705 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1707 memset(tx_chn, 0, sizeof(*tx_chn));
1711 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
1713 struct device *dev = common->dev;
1716 devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1718 common->tx_ch_rate_msk = 0;
1719 for (i = 0; i < common->tx_ch_num; i++) {
1720 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1723 devm_free_irq(dev, tx_chn->irq, tx_chn);
1725 netif_napi_del(&tx_chn->napi_tx);
1727 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1728 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1730 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1731 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1733 memset(tx_chn, 0, sizeof(*tx_chn));
1737 static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common)
1739 struct device *dev = common->dev;
1742 for (i = 0; i < common->tx_ch_num; i++) {
1743 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1745 netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx,
1746 am65_cpsw_nuss_tx_poll);
1747 hrtimer_init(&tx_chn->tx_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
1748 tx_chn->tx_hrtimer.function = &am65_cpsw_nuss_tx_timer_callback;
1750 ret = devm_request_irq(dev, tx_chn->irq,
1751 am65_cpsw_nuss_tx_irq,
1753 tx_chn->tx_chn_name, tx_chn);
1755 dev_err(dev, "failure requesting tx%u irq %u, %d\n",
1756 tx_chn->id, tx_chn->irq, ret);
1765 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
1767 u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
1768 struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
1769 struct device *dev = common->dev;
1770 struct k3_ring_cfg ring_cfg = {
1771 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1772 .mode = K3_RINGACC_RING_MODE_RING,
1778 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1779 AM65_CPSW_NAV_SW_DATA_SIZE);
1781 tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1782 tx_cfg.tx_cfg = ring_cfg;
1783 tx_cfg.txcq_cfg = ring_cfg;
1784 tx_cfg.tx_cfg.size = max_desc_num;
1785 tx_cfg.txcq_cfg.size = max_desc_num;
1787 for (i = 0; i < common->tx_ch_num; i++) {
1788 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1790 snprintf(tx_chn->tx_chn_name,
1791 sizeof(tx_chn->tx_chn_name), "tx%d", i);
1793 spin_lock_init(&tx_chn->lock);
1794 tx_chn->common = common;
1796 tx_chn->descs_num = max_desc_num;
1799 k3_udma_glue_request_tx_chn(dev,
1800 tx_chn->tx_chn_name,
1802 if (IS_ERR(tx_chn->tx_chn)) {
1803 ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn),
1804 "Failed to request tx dma channel\n");
1807 tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn);
1809 tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev,
1812 tx_chn->tx_chn_name);
1813 if (IS_ERR(tx_chn->desc_pool)) {
1814 ret = PTR_ERR(tx_chn->desc_pool);
1815 dev_err(dev, "Failed to create poll %d\n", ret);
1819 tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
1820 if (tx_chn->irq < 0) {
1821 dev_err(dev, "Failed to get tx dma irq %d\n",
1827 snprintf(tx_chn->tx_chn_name,
1828 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
1829 dev_name(dev), tx_chn->id);
1832 ret = am65_cpsw_nuss_ndev_add_tx_napi(common);
1834 dev_err(dev, "Failed to add tx NAPI %d\n", ret);
1839 i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1841 dev_err(dev, "Failed to add free_tx_chns action %d\n", i);
1848 static void am65_cpsw_nuss_free_rx_chns(void *data)
1850 struct am65_cpsw_common *common = data;
1851 struct am65_cpsw_rx_chn *rx_chn;
1853 rx_chn = &common->rx_chns;
1855 if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1856 k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1858 if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1859 k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1862 static void am65_cpsw_nuss_remove_rx_chns(void *data)
1864 struct am65_cpsw_common *common = data;
1865 struct am65_cpsw_rx_chn *rx_chn;
1866 struct device *dev = common->dev;
1868 rx_chn = &common->rx_chns;
1869 devm_remove_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1871 if (!(rx_chn->irq < 0))
1872 devm_free_irq(dev, rx_chn->irq, common);
1874 netif_napi_del(&common->napi_rx);
1876 if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1877 k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1879 if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1880 k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1882 common->rx_flow_id_base = -1;
1885 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
1887 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
1888 struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
1889 u32 max_desc_num = AM65_CPSW_MAX_RX_DESC;
1890 struct device *dev = common->dev;
1895 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1896 AM65_CPSW_NAV_SW_DATA_SIZE);
1898 rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1899 rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
1900 rx_cfg.flow_id_base = common->rx_flow_id_base;
1902 /* init all flows */
1904 rx_chn->descs_num = max_desc_num;
1906 rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
1907 if (IS_ERR(rx_chn->rx_chn)) {
1908 ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn),
1909 "Failed to request rx dma channel\n");
1912 rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn);
1914 rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev,
1917 if (IS_ERR(rx_chn->desc_pool)) {
1918 ret = PTR_ERR(rx_chn->desc_pool);
1919 dev_err(dev, "Failed to create rx poll %d\n", ret);
1923 common->rx_flow_id_base =
1924 k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
1925 dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
1927 fdqring_id = K3_RINGACC_RING_ID_ANY;
1928 for (i = 0; i < rx_cfg.flow_id_num; i++) {
1929 struct k3_ring_cfg rxring_cfg = {
1930 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1931 .mode = K3_RINGACC_RING_MODE_RING,
1934 struct k3_ring_cfg fdqring_cfg = {
1935 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1936 .flags = K3_RINGACC_RING_SHARED,
1938 struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
1939 .rx_cfg = rxring_cfg,
1940 .rxfdq_cfg = fdqring_cfg,
1941 .ring_rxq_id = K3_RINGACC_RING_ID_ANY,
1943 K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
1946 rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
1947 rx_flow_cfg.rx_cfg.size = max_desc_num;
1948 rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
1949 rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode;
1951 ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
1954 dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
1959 k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
1962 rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
1964 if (rx_chn->irq <= 0) {
1965 dev_err(dev, "Failed to get rx dma irq %d\n",
1972 netif_napi_add(common->dma_ndev, &common->napi_rx,
1973 am65_cpsw_nuss_rx_poll);
1974 hrtimer_init(&common->rx_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
1975 common->rx_hrtimer.function = &am65_cpsw_nuss_rx_timer_callback;
1977 ret = devm_request_irq(dev, rx_chn->irq,
1978 am65_cpsw_nuss_rx_irq,
1979 IRQF_TRIGGER_HIGH, dev_name(dev), common);
1981 dev_err(dev, "failure requesting rx irq %u, %d\n",
1987 i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1989 dev_err(dev, "Failed to add free_rx_chns action %d\n", i);
1996 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
1998 struct am65_cpsw_host *host_p = am65_common_get_host(common);
2000 host_p->common = common;
2001 host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
2002 host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
2007 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
2008 int slave, u8 *mac_addr)
2010 u32 mac_lo, mac_hi, offset;
2011 struct regmap *syscon;
2014 syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
2015 if (IS_ERR(syscon)) {
2016 if (PTR_ERR(syscon) == -ENODEV)
2018 return PTR_ERR(syscon);
2021 ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
2026 regmap_read(syscon, offset, &mac_lo);
2027 regmap_read(syscon, offset + 4, &mac_hi);
2029 mac_addr[0] = (mac_hi >> 8) & 0xff;
2030 mac_addr[1] = mac_hi & 0xff;
2031 mac_addr[2] = (mac_lo >> 24) & 0xff;
2032 mac_addr[3] = (mac_lo >> 16) & 0xff;
2033 mac_addr[4] = (mac_lo >> 8) & 0xff;
2034 mac_addr[5] = mac_lo & 0xff;
2039 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
2041 struct device *dev = common->dev;
2042 struct device_node *node;
2043 struct am65_cpts *cpts;
2044 void __iomem *reg_base;
2046 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
2049 node = of_get_child_by_name(dev->of_node, "cpts");
2051 dev_err(dev, "%s cpts not found\n", __func__);
2055 reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
2056 cpts = am65_cpts_create(dev, reg_base, node);
2058 int ret = PTR_ERR(cpts);
2061 dev_err(dev, "cpts create err %d\n", ret);
2064 common->cpts = cpts;
2065 /* Forbid PM runtime if CPTS is running.
2066 * K3 CPSWxG modules may completely lose context during ON->OFF
2067 * transitions depending on integration.
2068 * AM65x/J721E MCU CPSW2G: false
2069 * J721E MAIN_CPSW9G: true
2071 pm_runtime_forbid(dev);
2076 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
2078 struct device_node *node, *port_np;
2079 struct device *dev = common->dev;
2082 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2086 for_each_child_of_node(node, port_np) {
2087 struct am65_cpsw_port *port;
2090 /* it is not a slave port node, continue */
2091 if (strcmp(port_np->name, "port"))
2094 ret = of_property_read_u32(port_np, "reg", &port_id);
2096 dev_err(dev, "%pOF error reading port_id %d\n",
2101 if (!port_id || port_id > common->port_num) {
2102 dev_err(dev, "%pOF has invalid port_id %u %s\n",
2103 port_np, port_id, port_np->name);
2108 port = am65_common_get_port(common, port_id);
2109 port->port_id = port_id;
2110 port->common = common;
2111 port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
2112 AM65_CPSW_NU_PORTS_OFFSET * (port_id);
2113 if (common->pdata.extra_modes)
2114 port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id);
2115 port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
2116 (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
2117 port->name = of_get_property(port_np, "label", NULL);
2118 port->fetch_ram_base =
2119 common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
2120 (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
2122 port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
2123 if (IS_ERR(port->slave.mac_sl)) {
2124 ret = PTR_ERR(port->slave.mac_sl);
2128 port->disabled = !of_device_is_available(port_np);
2129 if (port->disabled) {
2130 common->disabled_ports_mask |= BIT(port->port_id);
2134 port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
2135 if (IS_ERR(port->slave.ifphy)) {
2136 ret = PTR_ERR(port->slave.ifphy);
2137 dev_err(dev, "%pOF error retrieving port phy: %d\n",
2142 /* Initialize the Serdes PHY for the port */
2143 ret = am65_cpsw_init_serdes_phy(dev, port_np, port);
2147 port->slave.mac_only =
2148 of_property_read_bool(port_np, "ti,mac-only");
2150 /* get phy/link info */
2151 port->slave.phy_node = port_np;
2152 ret = of_get_phy_mode(port_np, &port->slave.phy_if);
2154 dev_err(dev, "%pOF read phy-mode err %d\n",
2159 ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if);
2163 ret = of_get_mac_address(port_np, port->slave.mac_addr);
2165 am65_cpsw_am654_get_efuse_macid(port_np,
2167 port->slave.mac_addr);
2168 if (!is_valid_ether_addr(port->slave.mac_addr)) {
2169 eth_random_addr(port->slave.mac_addr);
2170 dev_err(dev, "Use random MAC address\n");
2174 /* Reset all Queue priorities to 0 */
2175 writel(0, port->port_base + AM65_CPSW_PN_REG_TX_PRI_MAP);
2179 /* is there at least one ext.port */
2180 if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) {
2181 dev_err(dev, "No Ext. port are available\n");
2188 of_node_put(port_np);
2193 static void am65_cpsw_pcpu_stats_free(void *data)
2195 struct am65_cpsw_ndev_stats __percpu *stats = data;
2200 static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common)
2202 struct am65_cpsw_port *port;
2205 for (i = 0; i < common->port_num; i++) {
2206 port = &common->ports[i];
2207 if (port->slave.phylink)
2208 phylink_destroy(port->slave.phylink);
2213 am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx)
2215 struct am65_cpsw_ndev_priv *ndev_priv;
2216 struct device *dev = common->dev;
2217 struct am65_cpsw_port *port;
2218 struct phylink *phylink;
2221 port = &common->ports[port_idx];
2227 port->ndev = devm_alloc_etherdev_mqs(common->dev,
2228 sizeof(struct am65_cpsw_ndev_priv),
2229 AM65_CPSW_MAX_TX_QUEUES,
2230 AM65_CPSW_MAX_RX_QUEUES);
2232 dev_err(dev, "error allocating slave net_device %u\n",
2237 ndev_priv = netdev_priv(port->ndev);
2238 ndev_priv->port = port;
2239 ndev_priv->msg_enable = AM65_CPSW_DEBUG;
2240 mutex_init(&ndev_priv->mm_lock);
2241 port->qos.link_speed = SPEED_UNKNOWN;
2242 SET_NETDEV_DEV(port->ndev, dev);
2244 eth_hw_addr_set(port->ndev, port->slave.mac_addr);
2246 port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
2247 port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE -
2248 (VLAN_ETH_HLEN + ETH_FCS_LEN);
2249 port->ndev->hw_features = NETIF_F_SG |
2253 port->ndev->features = port->ndev->hw_features |
2254 NETIF_F_HW_VLAN_CTAG_FILTER;
2255 port->ndev->vlan_features |= NETIF_F_SG;
2256 port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops;
2257 port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
2259 /* Configuring Phylink */
2260 port->slave.phylink_config.dev = &port->ndev->dev;
2261 port->slave.phylink_config.type = PHYLINK_NETDEV;
2262 port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 |
2263 MAC_1000FD | MAC_5000FD;
2264 port->slave.phylink_config.mac_managed_pm = true; /* MAC does PM */
2266 switch (port->slave.phy_if) {
2267 case PHY_INTERFACE_MODE_RGMII:
2268 case PHY_INTERFACE_MODE_RGMII_ID:
2269 case PHY_INTERFACE_MODE_RGMII_RXID:
2270 case PHY_INTERFACE_MODE_RGMII_TXID:
2271 phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces);
2274 case PHY_INTERFACE_MODE_RMII:
2275 __set_bit(PHY_INTERFACE_MODE_RMII,
2276 port->slave.phylink_config.supported_interfaces);
2279 case PHY_INTERFACE_MODE_QSGMII:
2280 case PHY_INTERFACE_MODE_SGMII:
2281 case PHY_INTERFACE_MODE_USXGMII:
2282 if (common->pdata.extra_modes & BIT(port->slave.phy_if)) {
2283 __set_bit(port->slave.phy_if,
2284 port->slave.phylink_config.supported_interfaces);
2286 dev_err(dev, "selected phy-mode is not supported\n");
2292 dev_err(dev, "selected phy-mode is not supported\n");
2296 phylink = phylink_create(&port->slave.phylink_config,
2297 of_node_to_fwnode(port->slave.phy_node),
2299 &am65_cpsw_phylink_mac_ops);
2300 if (IS_ERR(phylink))
2301 return PTR_ERR(phylink);
2303 port->slave.phylink = phylink;
2305 /* Disable TX checksum offload by default due to HW bug */
2306 if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
2307 port->ndev->features &= ~NETIF_F_HW_CSUM;
2309 ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
2310 if (!ndev_priv->stats)
2313 ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
2316 dev_err(dev, "failed to add percpu stat free action %d\n", ret);
2318 if (!common->dma_ndev)
2319 common->dma_ndev = port->ndev;
2324 static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common)
2329 for (i = 0; i < common->port_num; i++) {
2330 ret = am65_cpsw_nuss_init_port_ndev(common, i);
2338 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
2340 struct am65_cpsw_port *port;
2343 for (i = 0; i < common->port_num; i++) {
2344 port = &common->ports[i];
2345 if (port->ndev && port->ndev->reg_state == NETREG_REGISTERED)
2346 unregister_netdev(port->ndev);
2350 static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common)
2355 if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask))
2358 dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val);
2360 for (i = 1; i <= common->port_num; i++) {
2361 struct am65_cpsw_port *port = am65_common_get_port(common, i);
2362 struct am65_cpsw_ndev_priv *priv;
2367 priv = am65_ndev_to_priv(port->ndev);
2368 priv->offload_fwd_mark = set_val;
2372 bool am65_cpsw_port_dev_check(const struct net_device *ndev)
2374 if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) {
2375 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2377 return !common->is_emac_mode;
2383 static int am65_cpsw_netdevice_port_link(struct net_device *ndev,
2384 struct net_device *br_ndev,
2385 struct netlink_ext_ack *extack)
2387 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2388 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2391 if (!common->br_members) {
2392 common->hw_bridge_dev = br_ndev;
2394 /* This is adding the port to a second bridge, this is
2397 if (common->hw_bridge_dev != br_ndev)
2401 err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
2406 common->br_members |= BIT(priv->port->port_id);
2408 am65_cpsw_port_offload_fwd_mark_update(common);
2413 static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev)
2415 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2416 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2418 switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
2420 common->br_members &= ~BIT(priv->port->port_id);
2422 am65_cpsw_port_offload_fwd_mark_update(common);
2424 if (!common->br_members)
2425 common->hw_bridge_dev = NULL;
2428 /* netdev notifier */
2429 static int am65_cpsw_netdevice_event(struct notifier_block *unused,
2430 unsigned long event, void *ptr)
2432 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
2433 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2434 struct netdev_notifier_changeupper_info *info;
2435 int ret = NOTIFY_DONE;
2437 if (!am65_cpsw_port_dev_check(ndev))
2441 case NETDEV_CHANGEUPPER:
2444 if (netif_is_bridge_master(info->upper_dev)) {
2446 ret = am65_cpsw_netdevice_port_link(ndev,
2450 am65_cpsw_netdevice_port_unlink(ndev);
2457 return notifier_from_errno(ret);
2460 static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw)
2464 if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2465 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2468 cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event;
2469 ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2471 dev_err(cpsw->dev, "can't register netdevice notifier\n");
2475 ret = am65_cpsw_switchdev_register_notifiers(cpsw);
2477 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2482 static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw)
2484 if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2485 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2488 am65_cpsw_switchdev_unregister_notifiers(cpsw);
2489 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2492 static const struct devlink_ops am65_cpsw_devlink_ops = {};
2494 static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw)
2496 cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0,
2497 ALE_MCAST_BLOCK_LEARN_FWD);
2500 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common)
2502 struct am65_cpsw_host *host = am65_common_get_host(common);
2504 writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2506 am65_cpsw_init_stp_ale_entry(common);
2508 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
2509 dev_dbg(common->dev, "Set P0_UNI_FLOOD\n");
2510 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
2513 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common)
2515 struct am65_cpsw_host *host = am65_common_get_host(common);
2517 writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2519 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
2520 dev_dbg(common->dev, "unset P0_UNI_FLOOD\n");
2522 /* learning make no sense in multi-mac mode */
2523 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
2526 static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
2527 struct devlink_param_gset_ctx *ctx)
2529 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2530 struct am65_cpsw_common *common = dl_priv->common;
2532 dev_dbg(common->dev, "%s id:%u\n", __func__, id);
2534 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2537 ctx->val.vbool = !common->is_emac_mode;
2542 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port)
2544 struct am65_cpsw_slave_data *slave = &port->slave;
2545 struct am65_cpsw_common *common = port->common;
2548 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2550 if (slave->mac_only)
2551 /* enable mac-only mode on port */
2552 cpsw_ale_control_set(common->ale, port->port_id,
2553 ALE_PORT_MACONLY, 1);
2555 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1);
2557 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2559 cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr,
2560 HOST_PORT_NUM, ALE_SECURE, slave->port_vlan);
2561 cpsw_ale_add_mcast(common->ale, port->ndev->broadcast,
2562 port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2);
2565 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port)
2567 struct am65_cpsw_slave_data *slave = &port->slave;
2568 struct am65_cpsw_common *cpsw = port->common;
2571 cpsw_ale_control_set(cpsw->ale, port->port_id,
2572 ALE_PORT_NOLEARN, 0);
2574 cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr,
2575 HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN,
2578 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2580 cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast,
2581 port_mask, ALE_VLAN, slave->port_vlan,
2584 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2586 cpsw_ale_control_set(cpsw->ale, port->port_id,
2587 ALE_PORT_MACONLY, 0);
2590 static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
2591 struct devlink_param_gset_ctx *ctx)
2593 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2594 struct am65_cpsw_common *cpsw = dl_priv->common;
2595 bool switch_en = ctx->val.vbool;
2596 bool if_running = false;
2599 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
2601 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2604 if (switch_en == !cpsw->is_emac_mode)
2607 if (!switch_en && cpsw->br_members) {
2608 dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n");
2614 cpsw->is_emac_mode = !switch_en;
2616 for (i = 0; i < cpsw->port_num; i++) {
2617 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2619 if (!sl_ndev || !netif_running(sl_ndev))
2626 /* all ndevs are down */
2627 for (i = 0; i < cpsw->port_num; i++) {
2628 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2629 struct am65_cpsw_slave_data *slave;
2634 slave = am65_ndev_to_slave(sl_ndev);
2636 slave->port_vlan = cpsw->default_vlan;
2638 slave->port_vlan = 0;
2644 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
2645 /* clean up ALE table */
2646 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1);
2647 cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT);
2650 dev_info(cpsw->dev, "Enable switch mode\n");
2652 am65_cpsw_init_host_port_switch(cpsw);
2654 for (i = 0; i < cpsw->port_num; i++) {
2655 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2656 struct am65_cpsw_slave_data *slave;
2657 struct am65_cpsw_port *port;
2662 port = am65_ndev_to_port(sl_ndev);
2663 slave = am65_ndev_to_slave(sl_ndev);
2664 slave->port_vlan = cpsw->default_vlan;
2666 if (netif_running(sl_ndev))
2667 am65_cpsw_init_port_switch_ale(port);
2671 dev_info(cpsw->dev, "Disable switch mode\n");
2673 am65_cpsw_init_host_port_emac(cpsw);
2675 for (i = 0; i < cpsw->port_num; i++) {
2676 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2677 struct am65_cpsw_port *port;
2682 port = am65_ndev_to_port(sl_ndev);
2683 port->slave.port_vlan = 0;
2684 if (netif_running(sl_ndev))
2685 am65_cpsw_init_port_emac_ale(port);
2688 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0);
2695 static const struct devlink_param am65_cpsw_devlink_params[] = {
2696 DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode",
2697 DEVLINK_PARAM_TYPE_BOOL,
2698 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
2699 am65_cpsw_dl_switch_mode_get,
2700 am65_cpsw_dl_switch_mode_set, NULL),
2703 static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common)
2705 struct devlink_port_attrs attrs = {};
2706 struct am65_cpsw_devlink *dl_priv;
2707 struct device *dev = common->dev;
2708 struct devlink_port *dl_port;
2709 struct am65_cpsw_port *port;
2714 devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev);
2715 if (!common->devlink)
2718 dl_priv = devlink_priv(common->devlink);
2719 dl_priv->common = common;
2721 /* Provide devlink hook to switch mode when multiple external ports
2722 * are present NUSS switchdev driver is enabled.
2724 if (!AM65_CPSW_IS_CPSW2G(common) &&
2725 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) {
2726 ret = devlink_params_register(common->devlink,
2727 am65_cpsw_devlink_params,
2728 ARRAY_SIZE(am65_cpsw_devlink_params));
2730 dev_err(dev, "devlink params reg fail ret:%d\n", ret);
2735 for (i = 1; i <= common->port_num; i++) {
2736 port = am65_common_get_port(common, i);
2737 dl_port = &port->devlink_port;
2740 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
2742 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
2743 attrs.phys.port_number = port->port_id;
2744 attrs.switch_id.id_len = sizeof(resource_size_t);
2745 memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len);
2746 devlink_port_attrs_set(dl_port, &attrs);
2748 ret = devlink_port_register(common->devlink, dl_port, port->port_id);
2750 dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n",
2751 port->port_id, ret);
2755 devlink_register(common->devlink);
2759 for (i = i - 1; i >= 1; i--) {
2760 port = am65_common_get_port(common, i);
2761 dl_port = &port->devlink_port;
2763 devlink_port_unregister(dl_port);
2766 devlink_free(common->devlink);
2770 static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common)
2772 struct devlink_port *dl_port;
2773 struct am65_cpsw_port *port;
2776 devlink_unregister(common->devlink);
2778 for (i = 1; i <= common->port_num; i++) {
2779 port = am65_common_get_port(common, i);
2780 dl_port = &port->devlink_port;
2782 devlink_port_unregister(dl_port);
2785 if (!AM65_CPSW_IS_CPSW2G(common) &&
2786 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2787 devlink_params_unregister(common->devlink,
2788 am65_cpsw_devlink_params,
2789 ARRAY_SIZE(am65_cpsw_devlink_params));
2791 devlink_free(common->devlink);
2794 static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common)
2796 struct am65_cpsw_rx_chn *rx_chan = &common->rx_chns;
2797 struct am65_cpsw_tx_chn *tx_chan = common->tx_chns;
2798 struct device *dev = common->dev;
2799 struct am65_cpsw_port *port;
2802 /* init tx channels */
2803 ret = am65_cpsw_nuss_init_tx_chns(common);
2806 ret = am65_cpsw_nuss_init_rx_chns(common);
2810 /* The DMA Channels are not guaranteed to be in a clean state.
2811 * Reset and disable them to ensure that they are back to the
2812 * clean state and ready to be used.
2814 for (i = 0; i < common->tx_ch_num; i++) {
2815 k3_udma_glue_reset_tx_chn(tx_chan[i].tx_chn, &tx_chan[i],
2816 am65_cpsw_nuss_tx_cleanup);
2817 k3_udma_glue_disable_tx_chn(tx_chan[i].tx_chn);
2820 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
2821 k3_udma_glue_reset_rx_chn(rx_chan->rx_chn, i, rx_chan,
2822 am65_cpsw_nuss_rx_cleanup, !!i);
2824 k3_udma_glue_disable_rx_chn(rx_chan->rx_chn);
2826 ret = am65_cpsw_nuss_register_devlink(common);
2830 for (i = 0; i < common->port_num; i++) {
2831 port = &common->ports[i];
2836 SET_NETDEV_DEVLINK_PORT(port->ndev, &port->devlink_port);
2838 ret = register_netdev(port->ndev);
2840 dev_err(dev, "error registering slave net device%i %d\n",
2842 goto err_cleanup_ndev;
2846 ret = am65_cpsw_register_notifiers(common);
2848 goto err_cleanup_ndev;
2850 /* can't auto unregister ndev using devm_add_action() due to
2851 * devres release sequence in DD core for DMA
2857 am65_cpsw_nuss_cleanup_ndev(common);
2858 am65_cpsw_unregister_devlink(common);
2863 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
2867 common->tx_ch_num = num_tx;
2868 ret = am65_cpsw_nuss_init_tx_chns(common);
2873 struct am65_cpsw_soc_pdata {
2877 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
2878 .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2881 static const struct soc_device_attribute am65_cpsw_socinfo[] = {
2882 { .family = "AM65X",
2883 .revision = "SR2.0",
2884 .data = &am65x_soc_sr2_0
2889 static const struct am65_cpsw_pdata am65x_sr1_0 = {
2890 .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2891 .ale_dev_id = "am65x-cpsw2g",
2892 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2895 static const struct am65_cpsw_pdata j721e_pdata = {
2897 .ale_dev_id = "am65x-cpsw2g",
2898 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2901 static const struct am65_cpsw_pdata am64x_cpswxg_pdata = {
2902 .quirks = AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ,
2903 .ale_dev_id = "am64-cpswxg",
2904 .fdqring_mode = K3_RINGACC_RING_MODE_RING,
2907 static const struct am65_cpsw_pdata j7200_cpswxg_pdata = {
2909 .ale_dev_id = "am64-cpswxg",
2910 .fdqring_mode = K3_RINGACC_RING_MODE_RING,
2911 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII),
2914 static const struct am65_cpsw_pdata j721e_cpswxg_pdata = {
2916 .ale_dev_id = "am64-cpswxg",
2917 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2918 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII),
2921 static const struct am65_cpsw_pdata j784s4_cpswxg_pdata = {
2923 .ale_dev_id = "am64-cpswxg",
2924 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2925 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_USXGMII),
2928 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
2929 { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
2930 { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
2931 { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata},
2932 { .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata},
2933 { .compatible = "ti,j721e-cpswxg-nuss", .data = &j721e_cpswxg_pdata},
2934 { .compatible = "ti,j784s4-cpswxg-nuss", .data = &j784s4_cpswxg_pdata},
2937 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
2939 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
2941 const struct soc_device_attribute *soc;
2943 soc = soc_device_match(am65_cpsw_socinfo);
2944 if (soc && soc->data) {
2945 const struct am65_cpsw_soc_pdata *socdata = soc->data;
2947 /* disable quirks */
2948 common->pdata.quirks &= ~socdata->quirks_dis;
2952 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
2954 struct cpsw_ale_params ale_params = { 0 };
2955 const struct of_device_id *of_id;
2956 struct device *dev = &pdev->dev;
2957 struct am65_cpsw_common *common;
2958 struct device_node *node;
2959 struct resource *res;
2965 common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
2970 of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
2973 common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
2975 am65_cpsw_nuss_apply_socinfo(common);
2977 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
2978 common->ss_base = devm_ioremap_resource(&pdev->dev, res);
2979 if (IS_ERR(common->ss_base))
2980 return PTR_ERR(common->ss_base);
2981 common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
2982 /* Use device's physical base address as switch id */
2983 id_temp = cpu_to_be64(res->start);
2984 memcpy(common->switch_id, &id_temp, sizeof(res->start));
2986 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2989 common->port_num = of_get_child_count(node);
2991 if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
2994 common->rx_flow_id_base = -1;
2995 init_completion(&common->tdown_complete);
2996 common->tx_ch_num = AM65_CPSW_DEFAULT_TX_CHNS;
2997 common->pf_p0_rx_ptype_rrobin = false;
2998 common->default_vlan = 1;
3000 common->ports = devm_kcalloc(dev, common->port_num,
3001 sizeof(*common->ports),
3006 clk = devm_clk_get(dev, "fck");
3008 return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n");
3009 common->bus_freq = clk_get_rate(clk);
3011 pm_runtime_enable(dev);
3012 ret = pm_runtime_resume_and_get(dev);
3014 pm_runtime_disable(dev);
3018 node = of_get_child_by_name(dev->of_node, "mdio");
3020 dev_warn(dev, "MDIO node not found\n");
3021 } else if (of_device_is_available(node)) {
3022 struct platform_device *mdio_pdev;
3024 mdio_pdev = of_platform_device_create(node, NULL, dev);
3030 common->mdio_dev = &mdio_pdev->dev;
3034 am65_cpsw_nuss_get_ver(common);
3036 ret = am65_cpsw_nuss_init_host_p(common);
3040 ret = am65_cpsw_nuss_init_slave_ports(common);
3044 /* init common data */
3045 ale_params.dev = dev;
3046 ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
3047 ale_params.ale_ports = common->port_num + 1;
3048 ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
3049 ale_params.dev_id = common->pdata.ale_dev_id;
3050 ale_params.bus_freq = common->bus_freq;
3052 common->ale = cpsw_ale_create(&ale_params);
3053 if (IS_ERR(common->ale)) {
3054 dev_err(dev, "error initializing ale engine\n");
3055 ret = PTR_ERR(common->ale);
3059 ale_entries = common->ale->params.ale_entries;
3060 common->ale_context = devm_kzalloc(dev,
3061 ale_entries * ALE_ENTRY_WORDS * sizeof(u32),
3063 ret = am65_cpsw_init_cpts(common);
3068 for (i = 0; i < common->port_num; i++)
3069 am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
3071 dev_set_drvdata(dev, common);
3073 common->is_emac_mode = true;
3075 ret = am65_cpsw_nuss_init_ndevs(common);
3077 goto err_free_phylink;
3079 ret = am65_cpsw_nuss_register_ndevs(common);
3081 goto err_free_phylink;
3083 pm_runtime_put(dev);
3087 am65_cpsw_nuss_phylink_cleanup(common);
3088 am65_cpts_release(common->cpts);
3090 if (common->mdio_dev)
3091 of_platform_device_destroy(common->mdio_dev, NULL);
3093 pm_runtime_put_sync(dev);
3094 pm_runtime_disable(dev);
3098 static void am65_cpsw_nuss_remove(struct platform_device *pdev)
3100 struct device *dev = &pdev->dev;
3101 struct am65_cpsw_common *common;
3104 common = dev_get_drvdata(dev);
3106 ret = pm_runtime_resume_and_get(&pdev->dev);
3108 /* Note, if this error path is taken, we're leaking some
3111 dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
3116 am65_cpsw_unregister_devlink(common);
3117 am65_cpsw_unregister_notifiers(common);
3119 /* must unregister ndevs here because DD release_driver routine calls
3120 * dma_deconfigure(dev) before devres_release_all(dev)
3122 am65_cpsw_nuss_cleanup_ndev(common);
3123 am65_cpsw_nuss_phylink_cleanup(common);
3124 am65_cpts_release(common->cpts);
3125 am65_cpsw_disable_serdes_phy(common);
3127 if (common->mdio_dev)
3128 of_platform_device_destroy(common->mdio_dev, NULL);
3130 pm_runtime_put_sync(&pdev->dev);
3131 pm_runtime_disable(&pdev->dev);
3134 static int am65_cpsw_nuss_suspend(struct device *dev)
3136 struct am65_cpsw_common *common = dev_get_drvdata(dev);
3137 struct am65_cpsw_host *host_p = am65_common_get_host(common);
3138 struct am65_cpsw_port *port;
3139 struct net_device *ndev;
3142 cpsw_ale_dump(common->ale, common->ale_context);
3143 host_p->vid_context = readl(host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3144 for (i = 0; i < common->port_num; i++) {
3145 port = &common->ports[i];
3151 port->vid_context = readl(port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3152 netif_device_detach(ndev);
3153 if (netif_running(ndev)) {
3155 ret = am65_cpsw_nuss_ndo_slave_stop(ndev);
3158 netdev_err(ndev, "failed to stop: %d", ret);
3164 am65_cpts_suspend(common->cpts);
3166 am65_cpsw_nuss_remove_rx_chns(common);
3167 am65_cpsw_nuss_remove_tx_chns(common);
3172 static int am65_cpsw_nuss_resume(struct device *dev)
3174 struct am65_cpsw_common *common = dev_get_drvdata(dev);
3175 struct am65_cpsw_port *port;
3176 struct net_device *ndev;
3178 struct am65_cpsw_host *host_p = am65_common_get_host(common);
3180 ret = am65_cpsw_nuss_init_tx_chns(common);
3183 ret = am65_cpsw_nuss_init_rx_chns(common);
3187 /* If RX IRQ was disabled before suspend, keep it disabled */
3188 if (common->rx_irq_disabled)
3189 disable_irq(common->rx_chns.irq);
3191 am65_cpts_resume(common->cpts);
3193 for (i = 0; i < common->port_num; i++) {
3194 port = &common->ports[i];
3200 if (netif_running(ndev)) {
3202 ret = am65_cpsw_nuss_ndo_slave_open(ndev);
3205 netdev_err(ndev, "failed to start: %d", ret);
3210 netif_device_attach(ndev);
3211 writel(port->vid_context, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3214 writel(host_p->vid_context, host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3215 cpsw_ale_restore(common->ale, common->ale_context);
3220 static const struct dev_pm_ops am65_cpsw_nuss_dev_pm_ops = {
3221 SYSTEM_SLEEP_PM_OPS(am65_cpsw_nuss_suspend, am65_cpsw_nuss_resume)
3224 static struct platform_driver am65_cpsw_nuss_driver = {
3226 .name = AM65_CPSW_DRV_NAME,
3227 .of_match_table = am65_cpsw_nuss_of_mtable,
3228 .pm = &am65_cpsw_nuss_dev_pm_ops,
3230 .probe = am65_cpsw_nuss_probe,
3231 .remove_new = am65_cpsw_nuss_remove,
3234 module_platform_driver(am65_cpsw_nuss_driver);
3236 MODULE_LICENSE("GPL v2");
3237 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
3238 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");