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/
8 #include <linux/etherdevice.h>
9 #include <linux/if_vlan.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/kmemleak.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/net_tstamp.h>
17 #include <linux/of_mdio.h>
18 #include <linux/of_net.h>
19 #include <linux/of_device.h>
20 #include <linux/phy.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/dma/ti-cppi5.h>
27 #include <linux/dma/k3-udma-glue.h>
31 #include "am65-cpsw-nuss.h"
32 #include "k3-cppi-desc-pool.h"
34 #define AM65_CPSW_SS_BASE 0x0
35 #define AM65_CPSW_SGMII_BASE 0x100
36 #define AM65_CPSW_XGMII_BASE 0x2100
37 #define AM65_CPSW_CPSW_NU_BASE 0x20000
38 #define AM65_CPSW_NU_PORTS_BASE 0x1000
39 #define AM65_CPSW_NU_STATS_BASE 0x1a000
40 #define AM65_CPSW_NU_ALE_BASE 0x1e000
41 #define AM65_CPSW_NU_CPTS_BASE 0x1d000
43 #define AM65_CPSW_NU_PORTS_OFFSET 0x1000
44 #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200
46 #define AM65_CPSW_MAX_PORTS 8
48 #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN
49 #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
51 #define AM65_CPSW_REG_CTL 0x004
52 #define AM65_CPSW_REG_STAT_PORT_EN 0x014
53 #define AM65_CPSW_REG_PTYPE 0x018
55 #define AM65_CPSW_P0_REG_CTL 0x004
56 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008
58 #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c
59 #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020
60 #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024
62 #define AM65_CPSW_PORTN_REG_SA_L 0x308
63 #define AM65_CPSW_PORTN_REG_SA_H 0x30c
64 #define AM65_CPSW_PORTN_REG_TS_CTL 0x310
65 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314
66 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318
67 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C
69 #define AM65_CPSW_CTL_VLAN_AWARE BIT(1)
70 #define AM65_CPSW_CTL_P0_ENABLE BIT(2)
71 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13)
72 #define AM65_CPSW_CTL_P0_RX_PAD BIT(14)
74 /* AM65_CPSW_P0_REG_CTL */
75 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0)
77 /* AM65_CPSW_PORT_REG_PRI_CTL */
78 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8)
80 /* AM65_CPSW_PN_TS_CTL register fields */
81 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4)
82 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5)
83 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6)
84 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7)
85 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10)
86 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11)
87 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16
89 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
90 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16
92 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
93 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16)
94 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17)
95 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18)
96 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19)
97 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20)
98 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21)
99 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22)
100 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
102 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
103 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
105 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
107 #define AM65_CPSW_TS_TX_ANX_ALL_EN \
108 (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \
109 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \
110 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
112 #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30
113 /* Number of TX/RX descriptors */
114 #define AM65_CPSW_MAX_TX_DESC 500
115 #define AM65_CPSW_MAX_RX_DESC 500
117 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
118 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
120 #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
121 NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
122 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
124 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
127 u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
128 (dev_addr[2] << 16) | (dev_addr[3] << 24);
129 u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
131 writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
132 writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
135 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
137 cpsw_sl_reset(port->slave.mac_sl, 100);
138 /* Max length register has to be restored after MAC SL reset */
139 writel(AM65_CPSW_MAX_PACKET_SIZE,
140 port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
143 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
145 common->nuss_ver = readl(common->ss_base);
146 common->cpsw_ver = readl(common->cpsw_base);
147 dev_info(common->dev,
148 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u\n",
151 common->port_num + 1);
154 void am65_cpsw_nuss_adjust_link(struct net_device *ndev)
156 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
157 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
158 struct phy_device *phy = port->slave.phy;
165 mac_control = CPSW_SL_CTL_GMII_EN;
167 if (phy->speed == 1000)
168 mac_control |= CPSW_SL_CTL_GIG;
169 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
170 /* Can be used with in band mode only */
171 mac_control |= CPSW_SL_CTL_EXT_EN;
173 mac_control |= CPSW_SL_CTL_FULLDUPLEX;
175 /* RGMII speed is 100M if !CPSW_SL_CTL_GIG*/
177 /* rx_pause/tx_pause */
178 if (port->slave.rx_pause)
179 mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
181 if (port->slave.tx_pause)
182 mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
184 cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
186 /* enable forwarding */
187 cpsw_ale_control_set(common->ale, port->port_id,
188 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
190 netif_tx_wake_all_queues(ndev);
193 /* disable forwarding */
194 cpsw_ale_control_set(common->ale, port->port_id,
195 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
197 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
199 tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
200 dev_dbg(common->dev, "donw msc_sl %08x tmo %d\n",
201 cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS),
204 cpsw_sl_ctl_reset(port->slave.mac_sl);
206 netif_tx_stop_all_queues(ndev);
209 phy_print_status(phy);
212 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
213 __be16 proto, u16 vid)
215 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
216 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
217 u32 port_mask, unreg_mcast = 0;
220 ret = pm_runtime_get_sync(common->dev);
222 pm_runtime_put_noidle(common->dev);
226 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
228 unreg_mcast = port_mask;
229 dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
230 ret = cpsw_ale_add_vlan(common->ale, vid, port_mask,
231 unreg_mcast, port_mask, 0);
233 pm_runtime_put(common->dev);
237 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
238 __be16 proto, u16 vid)
240 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
243 ret = pm_runtime_get_sync(common->dev);
245 pm_runtime_put_noidle(common->dev);
249 dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
250 ret = cpsw_ale_del_vlan(common->ale, vid, 0);
252 pm_runtime_put(common->dev);
256 static void am65_cpsw_slave_set_promisc_2g(struct am65_cpsw_port *port,
259 struct am65_cpsw_common *common = port->common;
262 /* Enable promiscuous mode */
263 cpsw_ale_control_set(common->ale, port->port_id,
264 ALE_PORT_MACONLY_CAF, 1);
265 dev_dbg(common->dev, "promisc enabled\n");
267 /* Disable promiscuous mode */
268 cpsw_ale_control_set(common->ale, port->port_id,
269 ALE_PORT_MACONLY_CAF, 0);
270 dev_dbg(common->dev, "promisc disabled\n");
274 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
276 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
277 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
281 promisc = !!(ndev->flags & IFF_PROMISC);
282 am65_cpsw_slave_set_promisc_2g(port, promisc);
287 /* Restore allmulti on vlans if necessary */
288 cpsw_ale_set_allmulti(common->ale,
289 ndev->flags & IFF_ALLMULTI, port->port_id);
291 port_mask = ALE_PORT_HOST;
292 /* Clear all mcast from ALE */
293 cpsw_ale_flush_multicast(common->ale, port_mask, -1);
295 if (!netdev_mc_empty(ndev)) {
296 struct netdev_hw_addr *ha;
298 /* program multicast address list into ALE register */
299 netdev_for_each_mc_addr(ha, ndev) {
300 cpsw_ale_add_mcast(common->ale, ha->addr,
306 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
307 unsigned int txqueue)
309 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
310 struct am65_cpsw_tx_chn *tx_chn;
311 struct netdev_queue *netif_txq;
312 unsigned long trans_start;
314 netif_txq = netdev_get_tx_queue(ndev, txqueue);
315 tx_chn = &common->tx_chns[txqueue];
316 trans_start = netif_txq->trans_start;
318 netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
320 netif_tx_queue_stopped(netif_txq),
321 jiffies_to_msecs(jiffies - trans_start),
322 dql_avail(&netif_txq->dql),
323 k3_cppi_desc_pool_avail(tx_chn->desc_pool));
325 if (netif_tx_queue_stopped(netif_txq)) {
326 /* try recover if stopped by us */
327 txq_trans_update(netif_txq);
328 netif_tx_wake_queue(netif_txq);
332 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
335 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
336 struct cppi5_host_desc_t *desc_rx;
337 struct device *dev = common->dev;
338 u32 pkt_len = skb_tailroom(skb);
343 desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
345 dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
348 desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
350 buf_dma = dma_map_single(dev, skb->data, pkt_len, DMA_FROM_DEVICE);
351 if (unlikely(dma_mapping_error(dev, buf_dma))) {
352 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
353 dev_err(dev, "Failed to map rx skb buffer\n");
357 cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
358 AM65_CPSW_NAV_PS_DATA_SIZE);
359 cppi5_hdesc_attach_buf(desc_rx, 0, 0, buf_dma, skb_tailroom(skb));
360 swdata = cppi5_hdesc_get_swdata(desc_rx);
361 *((void **)swdata) = skb;
363 return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
366 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
368 struct am65_cpsw_host *host_p = am65_common_get_host(common);
371 /* P0 set Receive Priority Type */
372 val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
374 if (common->pf_p0_rx_ptype_rrobin) {
375 val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
376 /* Enet Ports fifos works in fixed priority mode only, so
377 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
381 val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
382 /* restore P0_Rx_Pri_Map */
383 pri_map = 0x76543210;
386 writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
387 writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
390 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common,
391 netdev_features_t features)
393 struct am65_cpsw_host *host_p = am65_common_get_host(common);
394 int port_idx, i, ret;
398 if (common->usage_count)
401 /* Control register */
402 writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
403 AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
404 common->cpsw_base + AM65_CPSW_REG_CTL);
405 /* Max length register */
406 writel(AM65_CPSW_MAX_PACKET_SIZE,
407 host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
408 /* set base flow_id */
409 writel(common->rx_flow_id_base,
410 host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
411 /* en tx crc offload */
412 if (features & NETIF_F_HW_CSUM)
413 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN,
414 host_p->port_base + AM65_CPSW_P0_REG_CTL);
416 am65_cpsw_nuss_set_p0_ptype(common);
418 /* enable statistic */
419 val = BIT(HOST_PORT_NUM);
420 for (port_idx = 0; port_idx < common->port_num; port_idx++) {
421 struct am65_cpsw_port *port = &common->ports[port_idx];
424 val |= BIT(port->port_id);
426 writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
428 /* disable priority elevation */
429 writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
431 cpsw_ale_start(common->ale);
433 /* limit to one RX flow only */
434 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
435 ALE_DEFAULT_THREAD_ID, 0);
436 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
437 ALE_DEFAULT_THREAD_ENABLE, 1);
438 if (AM65_CPSW_IS_CPSW2G(common))
439 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
440 ALE_PORT_NOLEARN, 1);
441 /* switch to vlan unaware mode */
442 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
443 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
444 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
446 /* default vlan cfg: create mask based on enabled ports */
447 port_mask = GENMASK(common->port_num, 0) &
448 ~common->disabled_ports_mask;
450 cpsw_ale_add_vlan(common->ale, 0, port_mask,
451 port_mask, port_mask,
452 port_mask & ~ALE_PORT_HOST);
454 for (i = 0; i < common->rx_chns.descs_num; i++) {
455 skb = __netdev_alloc_skb_ip_align(NULL,
456 AM65_CPSW_MAX_PACKET_SIZE,
459 dev_err(common->dev, "cannot allocate skb\n");
463 ret = am65_cpsw_nuss_rx_push(common, skb);
466 "cannot submit skb to channel rx, error %d\n",
471 kmemleak_not_leak(skb);
473 k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
475 for (i = 0; i < common->tx_ch_num; i++) {
476 ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn);
479 napi_enable(&common->tx_chns[i].napi_tx);
482 napi_enable(&common->napi_rx);
484 dev_dbg(common->dev, "cpsw_nuss started\n");
488 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma);
489 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma);
491 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
495 if (common->usage_count != 1)
498 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
499 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
501 /* shutdown tx channels */
502 atomic_set(&common->tdown_cnt, common->tx_ch_num);
503 /* ensure new tdown_cnt value is visible */
504 smp_mb__after_atomic();
505 reinit_completion(&common->tdown_complete);
507 for (i = 0; i < common->tx_ch_num; i++)
508 k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
510 i = wait_for_completion_timeout(&common->tdown_complete,
511 msecs_to_jiffies(1000));
513 dev_err(common->dev, "tx timeout\n");
514 for (i = 0; i < common->tx_ch_num; i++)
515 napi_disable(&common->tx_chns[i].napi_tx);
517 for (i = 0; i < common->tx_ch_num; i++) {
518 k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
520 am65_cpsw_nuss_tx_cleanup);
521 k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
524 k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
525 napi_disable(&common->napi_rx);
527 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
528 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
530 am65_cpsw_nuss_rx_cleanup, !!i);
532 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
534 cpsw_ale_stop(common->ale);
536 writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
537 writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
539 dev_dbg(common->dev, "cpsw_nuss stopped\n");
543 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
545 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
546 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
550 phy_stop(port->slave.phy);
552 netif_tx_stop_all_queues(ndev);
554 if (port->slave.phy) {
555 phy_disconnect(port->slave.phy);
556 port->slave.phy = NULL;
559 ret = am65_cpsw_nuss_common_stop(common);
563 common->usage_count--;
564 pm_runtime_put(common->dev);
568 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
570 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
571 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
575 ret = pm_runtime_get_sync(common->dev);
577 pm_runtime_put_noidle(common->dev);
581 /* Notify the stack of the actual queue counts. */
582 ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
584 dev_err(common->dev, "cannot set real number of tx queues\n");
588 ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
590 dev_err(common->dev, "cannot set real number of rx queues\n");
594 for (i = 0; i < common->tx_ch_num; i++)
595 netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i));
597 ret = am65_cpsw_nuss_common_open(common, ndev->features);
601 common->usage_count++;
603 am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
605 if (port->slave.mac_only)
606 /* enable mac-only mode on port */
607 cpsw_ale_control_set(common->ale, port->port_id,
608 ALE_PORT_MACONLY, 1);
609 if (AM65_CPSW_IS_CPSW2G(common))
610 cpsw_ale_control_set(common->ale, port->port_id,
611 ALE_PORT_NOLEARN, 1);
613 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
614 cpsw_ale_add_ucast(common->ale, ndev->dev_addr,
615 HOST_PORT_NUM, ALE_SECURE, 0);
616 cpsw_ale_add_mcast(common->ale, ndev->broadcast,
617 port_mask, 0, 0, ALE_MCAST_FWD_2);
619 /* mac_sl should be configured via phy-link interface */
620 am65_cpsw_sl_ctl_reset(port);
622 ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET,
627 if (port->slave.phy_node) {
628 port->slave.phy = of_phy_connect(ndev,
629 port->slave.phy_node,
630 &am65_cpsw_nuss_adjust_link,
631 0, port->slave.phy_if);
632 if (!port->slave.phy) {
633 dev_err(common->dev, "phy %pOF not found on slave %d\n",
634 port->slave.phy_node,
641 phy_attached_info(port->slave.phy);
642 phy_start(port->slave.phy);
647 am65_cpsw_nuss_ndo_slave_stop(ndev);
651 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
653 struct am65_cpsw_rx_chn *rx_chn = data;
654 struct cppi5_host_desc_t *desc_rx;
660 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
661 swdata = cppi5_hdesc_get_swdata(desc_rx);
663 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
665 dma_unmap_single(rx_chn->dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
666 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
668 dev_kfree_skb_any(skb);
671 /* RX psdata[2] word format - checksum information */
672 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0)
673 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16)
674 #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17)
675 #define AM65_CPSW_RX_PSD_IS_TCP BIT(18)
676 #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19)
677 #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20)
679 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
681 /* HW can verify IPv4/IPv6 TCP/UDP packets checksum
682 * csum information provides in psdata[2] word:
683 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
684 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
685 * bits - indicates IPv4/IPv6 packet
686 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
687 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
688 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
690 skb_checksum_none_assert(skb);
692 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
695 if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
696 AM65_CPSW_RX_PSD_IPV4_VALID)) &&
697 !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
698 /* csum for fragmented packets is unsupported */
699 if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
700 skb->ip_summed = CHECKSUM_UNNECESSARY;
704 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
707 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
708 u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
709 struct am65_cpsw_ndev_priv *ndev_priv;
710 struct am65_cpsw_ndev_stats *stats;
711 struct cppi5_host_desc_t *desc_rx;
712 struct device *dev = common->dev;
713 struct sk_buff *skb, *new_skb;
714 dma_addr_t desc_dma, buf_dma;
715 struct am65_cpsw_port *port;
716 struct net_device *ndev;
721 ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
724 dev_err(dev, "RX: pop chn fail %d\n", ret);
728 if (desc_dma & 0x1) {
729 dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
733 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
734 dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
735 __func__, flow_idx, &desc_dma);
737 swdata = cppi5_hdesc_get_swdata(desc_rx);
739 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
740 pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
741 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
742 dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
743 port = am65_common_get_port(common, port_id);
747 psdata = cppi5_hdesc_get_psdata(desc_rx);
748 csum_info = psdata[2];
749 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
751 dma_unmap_single(dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
753 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
755 new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
757 skb_put(skb, pkt_len);
758 skb->protocol = eth_type_trans(skb, ndev);
759 am65_cpsw_nuss_rx_csum(skb, csum_info);
760 napi_gro_receive(&common->napi_rx, skb);
762 ndev_priv = netdev_priv(ndev);
763 stats = this_cpu_ptr(ndev_priv->stats);
765 u64_stats_update_begin(&stats->syncp);
767 stats->rx_bytes += pkt_len;
768 u64_stats_update_end(&stats->syncp);
769 kmemleak_not_leak(new_skb);
771 ndev->stats.rx_dropped++;
775 if (netif_dormant(ndev)) {
776 dev_kfree_skb_any(new_skb);
777 ndev->stats.rx_dropped++;
781 ret = am65_cpsw_nuss_rx_push(common, new_skb);
782 if (WARN_ON(ret < 0)) {
783 dev_kfree_skb_any(new_skb);
784 ndev->stats.rx_errors++;
785 ndev->stats.rx_dropped++;
791 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
793 struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
794 int flow = AM65_CPSW_MAX_RX_FLOWS;
798 /* process every flow */
800 cur_budget = budget - num_rx;
802 while (cur_budget--) {
803 ret = am65_cpsw_nuss_rx_packets(common, flow);
809 if (num_rx >= budget)
813 dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
815 if (num_rx < budget && napi_complete_done(napi_rx, num_rx))
816 enable_irq(common->rx_chns.irq);
821 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
823 struct cppi5_host_desc_t *desc)
825 struct cppi5_host_desc_t *first_desc, *next_desc;
826 dma_addr_t buf_dma, next_desc_dma;
830 next_desc = first_desc;
832 cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
834 dma_unmap_single(dev, buf_dma, buf_dma_len,
837 next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
838 while (next_desc_dma) {
839 next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
841 cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
843 dma_unmap_page(dev, buf_dma, buf_dma_len,
846 next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
848 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
851 k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
854 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
856 struct am65_cpsw_tx_chn *tx_chn = data;
857 struct cppi5_host_desc_t *desc_tx;
861 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
862 swdata = cppi5_hdesc_get_swdata(desc_tx);
864 am65_cpsw_nuss_xmit_free(tx_chn, tx_chn->common->dev, desc_tx);
866 dev_kfree_skb_any(skb);
869 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
870 int chn, unsigned int budget)
872 struct cppi5_host_desc_t *desc_tx;
873 struct device *dev = common->dev;
874 struct am65_cpsw_tx_chn *tx_chn;
875 struct netdev_queue *netif_txq;
876 unsigned int total_bytes = 0;
877 struct net_device *ndev;
883 tx_chn = &common->tx_chns[chn];
886 struct am65_cpsw_ndev_priv *ndev_priv;
887 struct am65_cpsw_ndev_stats *stats;
889 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
893 if (desc_dma & 0x1) {
894 if (atomic_dec_and_test(&common->tdown_cnt))
895 complete(&common->tdown_complete);
899 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
901 swdata = cppi5_hdesc_get_swdata(desc_tx);
903 am65_cpsw_nuss_xmit_free(tx_chn, dev, desc_tx);
907 ndev_priv = netdev_priv(ndev);
908 stats = this_cpu_ptr(ndev_priv->stats);
909 u64_stats_update_begin(&stats->syncp);
911 stats->tx_bytes += skb->len;
912 u64_stats_update_end(&stats->syncp);
914 total_bytes += skb->len;
915 napi_consume_skb(skb, budget);
922 netif_txq = netdev_get_tx_queue(ndev, chn);
924 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
926 if (netif_tx_queue_stopped(netif_txq)) {
927 /* Check whether the queue is stopped due to stalled tx dma,
928 * if the queue is stopped then wake the queue as
929 * we have free desc for tx
931 __netif_tx_lock(netif_txq, smp_processor_id());
932 if (netif_running(ndev) &&
933 (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
935 netif_tx_wake_queue(netif_txq);
937 __netif_tx_unlock(netif_txq);
939 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
944 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
946 struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
949 num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id,
951 num_tx = min(num_tx, budget);
952 if (num_tx < budget) {
953 napi_complete(napi_tx);
954 enable_irq(tx_chn->irq);
960 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
962 struct am65_cpsw_common *common = dev_id;
964 disable_irq_nosync(irq);
965 napi_schedule(&common->napi_rx);
970 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
972 struct am65_cpsw_tx_chn *tx_chn = dev_id;
974 disable_irq_nosync(irq);
975 napi_schedule(&tx_chn->napi_tx);
980 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
981 struct net_device *ndev)
983 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
984 struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
985 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
986 struct device *dev = common->dev;
987 struct am65_cpsw_tx_chn *tx_chn;
988 struct netdev_queue *netif_txq;
989 dma_addr_t desc_dma, buf_dma;
995 /* padding enabled in hw */
996 pkt_len = skb_headlen(skb);
998 q_idx = skb_get_queue_mapping(skb);
999 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1001 tx_chn = &common->tx_chns[q_idx];
1002 netif_txq = netdev_get_tx_queue(ndev, q_idx);
1004 /* Map the linear buffer */
1005 buf_dma = dma_map_single(dev, skb->data, pkt_len,
1007 if (unlikely(dma_mapping_error(dev, buf_dma))) {
1008 dev_err(dev, "Failed to map tx skb buffer\n");
1009 ndev->stats.tx_errors++;
1013 first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1015 dev_dbg(dev, "Failed to allocate descriptor\n");
1016 dma_unmap_single(dev, buf_dma, pkt_len, DMA_TO_DEVICE);
1020 cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1021 AM65_CPSW_NAV_PS_DATA_SIZE);
1022 cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
1023 cppi5_hdesc_set_pkttype(first_desc, 0x7);
1024 cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1026 cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1027 swdata = cppi5_hdesc_get_swdata(first_desc);
1029 psdata = cppi5_hdesc_get_psdata(first_desc);
1031 /* HW csum offload if enabled */
1033 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1034 unsigned int cs_start, cs_offset;
1036 cs_start = skb_transport_offset(skb);
1037 cs_offset = cs_start + skb->csum_offset;
1038 /* HW numerates bytes starting from 1 */
1039 psdata[2] = ((cs_offset + 1) << 24) |
1040 ((cs_start + 1) << 16) | (skb->len - cs_start);
1041 dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1044 if (!skb_is_nonlinear(skb))
1047 dev_dbg(dev, "fragmented SKB\n");
1049 /* Handle the case where skb is fragmented in pages */
1050 cur_desc = first_desc;
1051 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1052 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1053 u32 frag_size = skb_frag_size(frag);
1055 next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1057 dev_err(dev, "Failed to allocate descriptor\n");
1058 goto busy_free_descs;
1061 buf_dma = skb_frag_dma_map(dev, frag, 0, frag_size,
1063 if (unlikely(dma_mapping_error(dev, buf_dma))) {
1064 dev_err(dev, "Failed to map tx skb page\n");
1065 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1066 ndev->stats.tx_errors++;
1067 goto err_free_descs;
1070 cppi5_hdesc_reset_hbdesc(next_desc);
1071 cppi5_hdesc_attach_buf(next_desc,
1072 buf_dma, frag_size, buf_dma, frag_size);
1074 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1076 cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1078 pkt_len += frag_size;
1079 cur_desc = next_desc;
1081 WARN_ON(pkt_len != skb->len);
1084 skb_tx_timestamp(skb);
1086 /* report bql before sending packet */
1087 netdev_tx_sent_queue(netif_txq, pkt_len);
1089 cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1090 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1091 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1093 dev_err(dev, "can't push desc %d\n", ret);
1095 netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1096 ndev->stats.tx_errors++;
1097 goto err_free_descs;
1100 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1101 netif_tx_stop_queue(netif_txq);
1102 /* Barrier, so that stop_queue visible to other cpus */
1103 smp_mb__after_atomic();
1104 dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1106 /* re-check for smp */
1107 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1109 netif_tx_wake_queue(netif_txq);
1110 dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1114 return NETDEV_TX_OK;
1117 am65_cpsw_nuss_xmit_free(tx_chn, dev, first_desc);
1119 ndev->stats.tx_dropped++;
1120 dev_kfree_skb_any(skb);
1121 return NETDEV_TX_OK;
1124 am65_cpsw_nuss_xmit_free(tx_chn, dev, first_desc);
1126 netif_tx_stop_queue(netif_txq);
1127 return NETDEV_TX_BUSY;
1130 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1133 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1134 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1135 struct sockaddr *sockaddr = (struct sockaddr *)addr;
1138 ret = eth_prepare_mac_addr_change(ndev, addr);
1142 ret = pm_runtime_get_sync(common->dev);
1144 pm_runtime_put_noidle(common->dev);
1148 cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1149 HOST_PORT_NUM, 0, 0);
1150 cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1151 HOST_PORT_NUM, ALE_SECURE, 0);
1153 am65_cpsw_port_set_sl_mac(port, addr);
1154 eth_commit_mac_addr_change(ndev, sockaddr);
1156 pm_runtime_put(common->dev);
1161 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1162 struct ifreq *req, int cmd)
1164 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1166 if (!netif_running(ndev))
1169 if (!port->slave.phy)
1172 return phy_mii_ioctl(port->slave.phy, req, cmd);
1175 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1176 struct rtnl_link_stats64 *stats)
1178 struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1182 for_each_possible_cpu(cpu) {
1183 struct am65_cpsw_ndev_stats *cpu_stats;
1189 cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1191 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1192 rx_packets = cpu_stats->rx_packets;
1193 rx_bytes = cpu_stats->rx_bytes;
1194 tx_packets = cpu_stats->tx_packets;
1195 tx_bytes = cpu_stats->tx_bytes;
1196 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1198 stats->rx_packets += rx_packets;
1199 stats->rx_bytes += rx_bytes;
1200 stats->tx_packets += tx_packets;
1201 stats->tx_bytes += tx_bytes;
1204 stats->rx_errors = dev->stats.rx_errors;
1205 stats->rx_dropped = dev->stats.rx_dropped;
1206 stats->tx_dropped = dev->stats.tx_dropped;
1209 static int am65_cpsw_nuss_ndo_slave_set_features(struct net_device *ndev,
1210 netdev_features_t features)
1212 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1213 netdev_features_t changes = features ^ ndev->features;
1214 struct am65_cpsw_host *host_p;
1216 host_p = am65_common_get_host(common);
1218 if (changes & NETIF_F_HW_CSUM) {
1219 bool enable = !!(features & NETIF_F_HW_CSUM);
1221 dev_info(common->dev, "Turn %s tx-checksum-ip-generic\n",
1222 enable ? "ON" : "OFF");
1224 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN,
1225 host_p->port_base + AM65_CPSW_P0_REG_CTL);
1228 host_p->port_base + AM65_CPSW_P0_REG_CTL);
1234 static const struct net_device_ops am65_cpsw_nuss_netdev_ops_2g = {
1235 .ndo_open = am65_cpsw_nuss_ndo_slave_open,
1236 .ndo_stop = am65_cpsw_nuss_ndo_slave_stop,
1237 .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit,
1238 .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode,
1239 .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats,
1240 .ndo_validate_addr = eth_validate_addr,
1241 .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address,
1242 .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout,
1243 .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid,
1244 .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid,
1245 .ndo_do_ioctl = am65_cpsw_nuss_ndo_slave_ioctl,
1246 .ndo_set_features = am65_cpsw_nuss_ndo_slave_set_features,
1249 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
1251 struct am65_cpsw_common *common = port->common;
1253 if (!port->disabled)
1256 common->disabled_ports_mask |= BIT(port->port_id);
1257 cpsw_ale_control_set(common->ale, port->port_id,
1258 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1260 cpsw_sl_reset(port->slave.mac_sl, 100);
1261 cpsw_sl_ctl_reset(port->slave.mac_sl);
1264 static void am65_cpsw_nuss_free_tx_chns(void *data)
1266 struct am65_cpsw_common *common = data;
1269 for (i = 0; i < common->tx_ch_num; i++) {
1270 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1272 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1273 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1275 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1276 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1278 memset(tx_chn, 0, sizeof(*tx_chn));
1282 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
1284 struct device *dev = common->dev;
1287 devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1289 for (i = 0; i < common->tx_ch_num; i++) {
1290 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1293 devm_free_irq(dev, tx_chn->irq, tx_chn);
1295 netif_napi_del(&tx_chn->napi_tx);
1297 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1298 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1300 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1301 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1303 memset(tx_chn, 0, sizeof(*tx_chn));
1307 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
1309 u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
1310 struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
1311 struct device *dev = common->dev;
1312 struct k3_ring_cfg ring_cfg = {
1313 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1314 .mode = K3_RINGACC_RING_MODE_RING,
1320 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1321 AM65_CPSW_NAV_SW_DATA_SIZE);
1323 tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1324 tx_cfg.tx_cfg = ring_cfg;
1325 tx_cfg.txcq_cfg = ring_cfg;
1326 tx_cfg.tx_cfg.size = max_desc_num;
1327 tx_cfg.txcq_cfg.size = max_desc_num;
1329 for (i = 0; i < common->tx_ch_num; i++) {
1330 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1332 snprintf(tx_chn->tx_chn_name,
1333 sizeof(tx_chn->tx_chn_name), "tx%d", i);
1335 tx_chn->common = common;
1337 tx_chn->descs_num = max_desc_num;
1339 k3_cppi_desc_pool_create_name(dev,
1342 tx_chn->tx_chn_name);
1343 if (IS_ERR(tx_chn->desc_pool)) {
1344 ret = PTR_ERR(tx_chn->desc_pool);
1345 dev_err(dev, "Failed to create poll %d\n", ret);
1350 k3_udma_glue_request_tx_chn(dev,
1351 tx_chn->tx_chn_name,
1353 if (IS_ERR(tx_chn->tx_chn)) {
1354 ret = PTR_ERR(tx_chn->tx_chn);
1355 dev_err(dev, "Failed to request tx dma channel %d\n",
1360 tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
1361 if (tx_chn->irq <= 0) {
1362 dev_err(dev, "Failed to get tx dma irq %d\n",
1367 snprintf(tx_chn->tx_chn_name,
1368 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
1369 dev_name(dev), tx_chn->id);
1373 i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1375 dev_err(dev, "failed to add free_tx_chns action %d", i);
1382 static void am65_cpsw_nuss_free_rx_chns(void *data)
1384 struct am65_cpsw_common *common = data;
1385 struct am65_cpsw_rx_chn *rx_chn;
1387 rx_chn = &common->rx_chns;
1389 if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1390 k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1392 if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1393 k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1396 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
1398 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
1399 struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
1400 u32 max_desc_num = AM65_CPSW_MAX_RX_DESC;
1401 struct device *dev = common->dev;
1406 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1407 AM65_CPSW_NAV_SW_DATA_SIZE);
1409 rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1410 rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
1411 rx_cfg.flow_id_base = common->rx_flow_id_base;
1413 /* init all flows */
1415 rx_chn->descs_num = max_desc_num;
1416 rx_chn->desc_pool = k3_cppi_desc_pool_create_name(dev,
1419 if (IS_ERR(rx_chn->desc_pool)) {
1420 ret = PTR_ERR(rx_chn->desc_pool);
1421 dev_err(dev, "Failed to create rx poll %d\n", ret);
1425 rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
1426 if (IS_ERR(rx_chn->rx_chn)) {
1427 ret = PTR_ERR(rx_chn->rx_chn);
1428 dev_err(dev, "Failed to request rx dma channel %d\n", ret);
1432 common->rx_flow_id_base =
1433 k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
1434 dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
1436 fdqring_id = K3_RINGACC_RING_ID_ANY;
1437 for (i = 0; i < rx_cfg.flow_id_num; i++) {
1438 struct k3_ring_cfg rxring_cfg = {
1439 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1440 .mode = K3_RINGACC_RING_MODE_RING,
1443 struct k3_ring_cfg fdqring_cfg = {
1444 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1445 .mode = K3_RINGACC_RING_MODE_MESSAGE,
1446 .flags = K3_RINGACC_RING_SHARED,
1448 struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
1449 .rx_cfg = rxring_cfg,
1450 .rxfdq_cfg = fdqring_cfg,
1451 .ring_rxq_id = K3_RINGACC_RING_ID_ANY,
1453 K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
1456 rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
1457 rx_flow_cfg.rx_cfg.size = max_desc_num;
1458 rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
1460 ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
1463 dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
1468 k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
1471 rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
1473 if (rx_chn->irq <= 0) {
1474 dev_err(dev, "Failed to get rx dma irq %d\n",
1482 i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1484 dev_err(dev, "failed to add free_rx_chns action %d", i);
1491 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
1493 struct am65_cpsw_host *host_p = am65_common_get_host(common);
1495 host_p->common = common;
1496 host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
1497 host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
1502 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
1503 int slave, u8 *mac_addr)
1505 u32 mac_lo, mac_hi, offset;
1506 struct regmap *syscon;
1509 syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
1510 if (IS_ERR(syscon)) {
1511 if (PTR_ERR(syscon) == -ENODEV)
1513 return PTR_ERR(syscon);
1516 ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
1521 regmap_read(syscon, offset, &mac_lo);
1522 regmap_read(syscon, offset + 4, &mac_hi);
1524 mac_addr[0] = (mac_hi >> 8) & 0xff;
1525 mac_addr[1] = mac_hi & 0xff;
1526 mac_addr[2] = (mac_lo >> 24) & 0xff;
1527 mac_addr[3] = (mac_lo >> 16) & 0xff;
1528 mac_addr[4] = (mac_lo >> 8) & 0xff;
1529 mac_addr[5] = mac_lo & 0xff;
1534 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
1536 struct device_node *node, *port_np;
1537 struct device *dev = common->dev;
1540 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
1544 for_each_child_of_node(node, port_np) {
1545 struct am65_cpsw_port *port;
1546 const void *mac_addr;
1549 /* it is not a slave port node, continue */
1550 if (strcmp(port_np->name, "port"))
1553 ret = of_property_read_u32(port_np, "reg", &port_id);
1555 dev_err(dev, "%pOF error reading port_id %d\n",
1560 if (!port_id || port_id > common->port_num) {
1561 dev_err(dev, "%pOF has invalid port_id %u %s\n",
1562 port_np, port_id, port_np->name);
1566 port = am65_common_get_port(common, port_id);
1567 port->port_id = port_id;
1568 port->common = common;
1569 port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
1570 AM65_CPSW_NU_PORTS_OFFSET * (port_id);
1571 port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
1572 (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
1573 port->name = of_get_property(port_np, "label", NULL);
1575 port->disabled = !of_device_is_available(port_np);
1579 port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
1580 if (IS_ERR(port->slave.ifphy)) {
1581 ret = PTR_ERR(port->slave.ifphy);
1582 dev_err(dev, "%pOF error retrieving port phy: %d\n",
1587 port->slave.mac_only =
1588 of_property_read_bool(port_np, "ti,mac-only");
1590 /* get phy/link info */
1591 if (of_phy_is_fixed_link(port_np)) {
1592 ret = of_phy_register_fixed_link(port_np);
1594 if (ret != -EPROBE_DEFER)
1595 dev_err(dev, "%pOF failed to register fixed-link phy: %d\n",
1599 port->slave.phy_node = of_node_get(port_np);
1601 port->slave.phy_node =
1602 of_parse_phandle(port_np, "phy-handle", 0);
1605 if (!port->slave.phy_node) {
1607 "slave[%d] no phy found\n", port_id);
1611 ret = of_get_phy_mode(port_np, &port->slave.phy_if);
1613 dev_err(dev, "%pOF read phy-mode err %d\n",
1618 port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
1619 if (IS_ERR(port->slave.mac_sl))
1620 return PTR_ERR(port->slave.mac_sl);
1622 mac_addr = of_get_mac_address(port_np);
1623 if (!IS_ERR(mac_addr)) {
1624 ether_addr_copy(port->slave.mac_addr, mac_addr);
1625 } else if (am65_cpsw_am654_get_efuse_macid(port_np,
1627 port->slave.mac_addr) ||
1628 !is_valid_ether_addr(port->slave.mac_addr)) {
1629 random_ether_addr(port->slave.mac_addr);
1630 dev_err(dev, "Use rundom MAC address\n");
1638 static void am65_cpsw_pcpu_stats_free(void *data)
1640 struct am65_cpsw_ndev_stats __percpu *stats = data;
1645 static int am65_cpsw_nuss_init_ndev_2g(struct am65_cpsw_common *common)
1647 struct am65_cpsw_ndev_priv *ndev_priv;
1648 struct device *dev = common->dev;
1649 struct am65_cpsw_port *port;
1652 port = am65_common_get_port(common, 1);
1655 port->ndev = devm_alloc_etherdev_mqs(common->dev,
1656 sizeof(struct am65_cpsw_ndev_priv),
1657 AM65_CPSW_MAX_TX_QUEUES,
1658 AM65_CPSW_MAX_RX_QUEUES);
1660 dev_err(dev, "error allocating slave net_device %u\n",
1665 ndev_priv = netdev_priv(port->ndev);
1666 ndev_priv->port = port;
1667 ndev_priv->msg_enable = AM65_CPSW_DEBUG;
1668 SET_NETDEV_DEV(port->ndev, dev);
1670 ether_addr_copy(port->ndev->dev_addr, port->slave.mac_addr);
1672 port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
1673 port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE;
1674 port->ndev->hw_features = NETIF_F_SG |
1677 port->ndev->features = port->ndev->hw_features |
1678 NETIF_F_HW_VLAN_CTAG_FILTER;
1679 port->ndev->vlan_features |= NETIF_F_SG;
1680 port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops_2g;
1681 port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
1683 /* Disable TX checksum offload by default due to HW bug */
1684 if (common->pdata->quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
1685 port->ndev->features &= ~NETIF_F_HW_CSUM;
1687 ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
1688 if (!ndev_priv->stats)
1691 ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
1694 dev_err(dev, "failed to add percpu stat free action %d", ret);
1698 netif_napi_add(port->ndev, &common->napi_rx,
1699 am65_cpsw_nuss_rx_poll, NAPI_POLL_WEIGHT);
1701 common->pf_p0_rx_ptype_rrobin = false;
1706 static int am65_cpsw_nuss_ndev_add_napi_2g(struct am65_cpsw_common *common)
1708 struct device *dev = common->dev;
1709 struct am65_cpsw_port *port;
1712 port = am65_common_get_port(common, 1);
1714 for (i = 0; i < common->tx_ch_num; i++) {
1715 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1717 netif_tx_napi_add(port->ndev, &tx_chn->napi_tx,
1718 am65_cpsw_nuss_tx_poll, NAPI_POLL_WEIGHT);
1720 ret = devm_request_irq(dev, tx_chn->irq,
1721 am65_cpsw_nuss_tx_irq,
1722 0, tx_chn->tx_chn_name, tx_chn);
1724 dev_err(dev, "failure requesting tx%u irq %u, %d\n",
1725 tx_chn->id, tx_chn->irq, ret);
1734 static int am65_cpsw_nuss_ndev_reg_2g(struct am65_cpsw_common *common)
1736 struct device *dev = common->dev;
1737 struct am65_cpsw_port *port;
1740 port = am65_common_get_port(common, 1);
1741 ret = am65_cpsw_nuss_ndev_add_napi_2g(common);
1745 ret = devm_request_irq(dev, common->rx_chns.irq,
1746 am65_cpsw_nuss_rx_irq,
1747 0, dev_name(dev), common);
1749 dev_err(dev, "failure requesting rx irq %u, %d\n",
1750 common->rx_chns.irq, ret);
1754 ret = register_netdev(port->ndev);
1756 dev_err(dev, "error registering slave net device %d\n", ret);
1758 /* can't auto unregister ndev using devm_add_action() due to
1759 * devres release sequence in DD core for DMA
1765 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
1769 common->tx_ch_num = num_tx;
1770 ret = am65_cpsw_nuss_init_tx_chns(common);
1774 return am65_cpsw_nuss_ndev_add_napi_2g(common);
1777 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
1779 struct am65_cpsw_port *port;
1782 for (i = 0; i < common->port_num; i++) {
1783 port = &common->ports[i];
1785 unregister_netdev(port->ndev);
1789 static const struct am65_cpsw_pdata am65x_sr1_0 = {
1790 .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
1793 static const struct am65_cpsw_pdata j721e_sr1_0 = {
1797 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
1798 { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0 },
1799 { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_sr1_0 },
1802 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
1804 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
1806 struct cpsw_ale_params ale_params;
1807 const struct of_device_id *of_id;
1808 struct device *dev = &pdev->dev;
1809 struct am65_cpsw_common *common;
1810 struct device_node *node;
1811 struct resource *res;
1814 common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
1819 of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
1822 common->pdata = of_id->data;
1824 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
1825 common->ss_base = devm_ioremap_resource(&pdev->dev, res);
1826 if (IS_ERR(common->ss_base))
1827 return PTR_ERR(common->ss_base);
1828 common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
1830 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
1833 common->port_num = of_get_child_count(node);
1834 if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
1838 if (common->port_num != 1)
1841 common->rx_flow_id_base = -1;
1842 init_completion(&common->tdown_complete);
1843 common->tx_ch_num = 1;
1845 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(48));
1847 dev_err(dev, "error setting dma mask: %d\n", ret);
1851 common->ports = devm_kcalloc(dev, common->port_num,
1852 sizeof(*common->ports),
1857 pm_runtime_enable(dev);
1858 ret = pm_runtime_get_sync(dev);
1860 pm_runtime_put_noidle(dev);
1861 pm_runtime_disable(dev);
1865 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1866 /* We do not want to force this, as in some cases may not have child */
1868 dev_warn(dev, "populating child nodes err:%d\n", ret);
1870 am65_cpsw_nuss_get_ver(common);
1872 /* init tx channels */
1873 ret = am65_cpsw_nuss_init_tx_chns(common);
1876 ret = am65_cpsw_nuss_init_rx_chns(common);
1880 ret = am65_cpsw_nuss_init_host_p(common);
1884 ret = am65_cpsw_nuss_init_slave_ports(common);
1888 /* init common data */
1889 ale_params.dev = dev;
1890 ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
1891 ale_params.ale_entries = 0;
1892 ale_params.ale_ports = common->port_num + 1;
1893 ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
1894 ale_params.nu_switch_ale = true;
1896 common->ale = cpsw_ale_create(&ale_params);
1898 dev_err(dev, "error initializing ale engine\n");
1903 for (i = 0; i < common->port_num; i++)
1904 am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
1906 dev_set_drvdata(dev, common);
1908 ret = am65_cpsw_nuss_init_ndev_2g(common);
1912 ret = am65_cpsw_nuss_ndev_reg_2g(common);
1916 pm_runtime_put(dev);
1920 of_platform_depopulate(dev);
1921 pm_runtime_put_sync(dev);
1922 pm_runtime_disable(dev);
1926 static int am65_cpsw_nuss_remove(struct platform_device *pdev)
1928 struct device *dev = &pdev->dev;
1929 struct am65_cpsw_common *common;
1932 common = dev_get_drvdata(dev);
1934 ret = pm_runtime_get_sync(&pdev->dev);
1936 pm_runtime_put_noidle(&pdev->dev);
1940 /* must unregister ndevs here because DD release_driver routine calls
1941 * dma_deconfigure(dev) before devres_release_all(dev)
1943 am65_cpsw_nuss_cleanup_ndev(common);
1945 of_platform_depopulate(dev);
1947 pm_runtime_put_sync(&pdev->dev);
1948 pm_runtime_disable(&pdev->dev);
1952 static struct platform_driver am65_cpsw_nuss_driver = {
1954 .name = AM65_CPSW_DRV_NAME,
1955 .of_match_table = am65_cpsw_nuss_of_mtable,
1957 .probe = am65_cpsw_nuss_probe,
1958 .remove = am65_cpsw_nuss_remove,
1961 module_platform_driver(am65_cpsw_nuss_driver);
1963 MODULE_LICENSE("GPL v2");
1964 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
1965 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");