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/phylink.h>
23 #include <linux/phy/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/sys_soc.h>
29 #include <linux/dma/ti-cppi5.h>
30 #include <linux/dma/k3-udma-glue.h>
31 #include <net/switchdev.h>
35 #include "am65-cpsw-nuss.h"
36 #include "am65-cpsw-switchdev.h"
37 #include "k3-cppi-desc-pool.h"
38 #include "am65-cpts.h"
40 #define AM65_CPSW_SS_BASE 0x0
41 #define AM65_CPSW_SGMII_BASE 0x100
42 #define AM65_CPSW_XGMII_BASE 0x2100
43 #define AM65_CPSW_CPSW_NU_BASE 0x20000
44 #define AM65_CPSW_NU_PORTS_BASE 0x1000
45 #define AM65_CPSW_NU_FRAM_BASE 0x12000
46 #define AM65_CPSW_NU_STATS_BASE 0x1a000
47 #define AM65_CPSW_NU_ALE_BASE 0x1e000
48 #define AM65_CPSW_NU_CPTS_BASE 0x1d000
50 #define AM65_CPSW_NU_PORTS_OFFSET 0x1000
51 #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200
52 #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200
54 #define AM65_CPSW_MAX_PORTS 8
56 #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN
57 #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
59 #define AM65_CPSW_REG_CTL 0x004
60 #define AM65_CPSW_REG_STAT_PORT_EN 0x014
61 #define AM65_CPSW_REG_PTYPE 0x018
63 #define AM65_CPSW_P0_REG_CTL 0x004
64 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008
66 #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c
67 #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020
68 #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024
70 #define AM65_CPSW_PORTN_REG_SA_L 0x308
71 #define AM65_CPSW_PORTN_REG_SA_H 0x30c
72 #define AM65_CPSW_PORTN_REG_TS_CTL 0x310
73 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314
74 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318
75 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C
77 #define AM65_CPSW_CTL_VLAN_AWARE BIT(1)
78 #define AM65_CPSW_CTL_P0_ENABLE BIT(2)
79 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13)
80 #define AM65_CPSW_CTL_P0_RX_PAD BIT(14)
82 /* AM65_CPSW_P0_REG_CTL */
83 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0)
85 /* AM65_CPSW_PORT_REG_PRI_CTL */
86 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8)
88 /* AM65_CPSW_PN_TS_CTL register fields */
89 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4)
90 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5)
91 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6)
92 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7)
93 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10)
94 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11)
95 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16
97 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
98 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16
100 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
101 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16)
102 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17)
103 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18)
104 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19)
105 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20)
106 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21)
107 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22)
108 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
110 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
111 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
113 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
115 #define AM65_CPSW_TS_TX_ANX_ALL_EN \
116 (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \
117 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \
118 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
120 #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30
121 /* Number of TX/RX descriptors */
122 #define AM65_CPSW_MAX_TX_DESC 500
123 #define AM65_CPSW_MAX_RX_DESC 500
125 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
126 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
128 #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
129 NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
130 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
132 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
135 u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
136 (dev_addr[2] << 16) | (dev_addr[3] << 24);
137 u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
139 writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
140 writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
143 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
145 cpsw_sl_reset(port->slave.mac_sl, 100);
146 /* Max length register has to be restored after MAC SL reset */
147 writel(AM65_CPSW_MAX_PACKET_SIZE,
148 port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
151 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
153 common->nuss_ver = readl(common->ss_base);
154 common->cpsw_ver = readl(common->cpsw_base);
155 dev_info(common->dev,
156 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
159 common->port_num + 1,
160 common->pdata.quirks);
163 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
164 __be16 proto, u16 vid)
166 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
167 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
168 u32 port_mask, unreg_mcast = 0;
171 if (!common->is_emac_mode)
174 if (!netif_running(ndev) || !vid)
177 ret = pm_runtime_resume_and_get(common->dev);
181 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
183 unreg_mcast = port_mask;
184 dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
185 ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask,
186 unreg_mcast, port_mask, 0);
188 pm_runtime_put(common->dev);
192 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
193 __be16 proto, u16 vid)
195 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
196 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
199 if (!common->is_emac_mode)
202 if (!netif_running(ndev) || !vid)
205 ret = pm_runtime_resume_and_get(common->dev);
209 dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
210 ret = cpsw_ale_del_vlan(common->ale, vid,
211 BIT(port->port_id) | ALE_PORT_HOST);
213 pm_runtime_put(common->dev);
217 static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port,
220 struct am65_cpsw_common *common = port->common;
222 if (promisc && !common->is_emac_mode) {
223 dev_dbg(common->dev, "promisc mode requested in switch mode");
228 /* Enable promiscuous mode */
229 cpsw_ale_control_set(common->ale, port->port_id,
230 ALE_PORT_MACONLY_CAF, 1);
231 dev_dbg(common->dev, "promisc enabled\n");
233 /* Disable promiscuous mode */
234 cpsw_ale_control_set(common->ale, port->port_id,
235 ALE_PORT_MACONLY_CAF, 0);
236 dev_dbg(common->dev, "promisc disabled\n");
240 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
242 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
243 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
247 promisc = !!(ndev->flags & IFF_PROMISC);
248 am65_cpsw_slave_set_promisc(port, promisc);
253 /* Restore allmulti on vlans if necessary */
254 cpsw_ale_set_allmulti(common->ale,
255 ndev->flags & IFF_ALLMULTI, port->port_id);
257 port_mask = ALE_PORT_HOST;
258 /* Clear all mcast from ALE */
259 cpsw_ale_flush_multicast(common->ale, port_mask, -1);
261 if (!netdev_mc_empty(ndev)) {
262 struct netdev_hw_addr *ha;
264 /* program multicast address list into ALE register */
265 netdev_for_each_mc_addr(ha, ndev) {
266 cpsw_ale_add_mcast(common->ale, ha->addr,
272 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
273 unsigned int txqueue)
275 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
276 struct am65_cpsw_tx_chn *tx_chn;
277 struct netdev_queue *netif_txq;
278 unsigned long trans_start;
280 netif_txq = netdev_get_tx_queue(ndev, txqueue);
281 tx_chn = &common->tx_chns[txqueue];
282 trans_start = READ_ONCE(netif_txq->trans_start);
284 netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
286 netif_tx_queue_stopped(netif_txq),
287 jiffies_to_msecs(jiffies - trans_start),
288 dql_avail(&netif_txq->dql),
289 k3_cppi_desc_pool_avail(tx_chn->desc_pool));
291 if (netif_tx_queue_stopped(netif_txq)) {
292 /* try recover if stopped by us */
293 txq_trans_update(netif_txq);
294 netif_tx_wake_queue(netif_txq);
298 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
301 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
302 struct cppi5_host_desc_t *desc_rx;
303 struct device *dev = common->dev;
304 u32 pkt_len = skb_tailroom(skb);
309 desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
311 dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
314 desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
316 buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len,
318 if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) {
319 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
320 dev_err(dev, "Failed to map rx skb buffer\n");
324 cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
325 AM65_CPSW_NAV_PS_DATA_SIZE);
326 k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma);
327 cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb));
328 swdata = cppi5_hdesc_get_swdata(desc_rx);
329 *((void **)swdata) = skb;
331 return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
334 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
336 struct am65_cpsw_host *host_p = am65_common_get_host(common);
339 /* P0 set Receive Priority Type */
340 val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
342 if (common->pf_p0_rx_ptype_rrobin) {
343 val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
344 /* Enet Ports fifos works in fixed priority mode only, so
345 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
349 val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
350 /* restore P0_Rx_Pri_Map */
351 pri_map = 0x76543210;
354 writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
355 writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
358 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common);
359 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common);
360 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port);
361 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port);
363 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common,
364 netdev_features_t features)
366 struct am65_cpsw_host *host_p = am65_common_get_host(common);
367 int port_idx, i, ret;
371 if (common->usage_count)
374 /* Control register */
375 writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
376 AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
377 common->cpsw_base + AM65_CPSW_REG_CTL);
378 /* Max length register */
379 writel(AM65_CPSW_MAX_PACKET_SIZE,
380 host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
381 /* set base flow_id */
382 writel(common->rx_flow_id_base,
383 host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
384 /* en tx crc offload */
385 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN, host_p->port_base + AM65_CPSW_P0_REG_CTL);
387 am65_cpsw_nuss_set_p0_ptype(common);
389 /* enable statistic */
390 val = BIT(HOST_PORT_NUM);
391 for (port_idx = 0; port_idx < common->port_num; port_idx++) {
392 struct am65_cpsw_port *port = &common->ports[port_idx];
395 val |= BIT(port->port_id);
397 writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
399 /* disable priority elevation */
400 writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
402 cpsw_ale_start(common->ale);
404 /* limit to one RX flow only */
405 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
406 ALE_DEFAULT_THREAD_ID, 0);
407 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
408 ALE_DEFAULT_THREAD_ENABLE, 1);
409 /* switch to vlan unaware mode */
410 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
411 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
412 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
414 /* default vlan cfg: create mask based on enabled ports */
415 port_mask = GENMASK(common->port_num, 0) &
416 ~common->disabled_ports_mask;
418 cpsw_ale_add_vlan(common->ale, 0, port_mask,
419 port_mask, port_mask,
420 port_mask & ~ALE_PORT_HOST);
422 if (common->is_emac_mode)
423 am65_cpsw_init_host_port_emac(common);
425 am65_cpsw_init_host_port_switch(common);
427 for (i = 0; i < common->rx_chns.descs_num; i++) {
428 skb = __netdev_alloc_skb_ip_align(NULL,
429 AM65_CPSW_MAX_PACKET_SIZE,
432 dev_err(common->dev, "cannot allocate skb\n");
436 ret = am65_cpsw_nuss_rx_push(common, skb);
439 "cannot submit skb to channel rx, error %d\n",
444 kmemleak_not_leak(skb);
446 k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
448 for (i = 0; i < common->tx_ch_num; i++) {
449 ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn);
452 napi_enable(&common->tx_chns[i].napi_tx);
455 napi_enable(&common->napi_rx);
456 if (common->rx_irq_disabled) {
457 common->rx_irq_disabled = false;
458 enable_irq(common->rx_chns.irq);
461 dev_dbg(common->dev, "cpsw_nuss started\n");
465 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma);
466 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma);
468 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
472 if (common->usage_count != 1)
475 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
476 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
478 /* shutdown tx channels */
479 atomic_set(&common->tdown_cnt, common->tx_ch_num);
480 /* ensure new tdown_cnt value is visible */
481 smp_mb__after_atomic();
482 reinit_completion(&common->tdown_complete);
484 for (i = 0; i < common->tx_ch_num; i++)
485 k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
487 i = wait_for_completion_timeout(&common->tdown_complete,
488 msecs_to_jiffies(1000));
490 dev_err(common->dev, "tx timeout\n");
491 for (i = 0; i < common->tx_ch_num; i++)
492 napi_disable(&common->tx_chns[i].napi_tx);
494 for (i = 0; i < common->tx_ch_num; i++) {
495 k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
497 am65_cpsw_nuss_tx_cleanup);
498 k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
501 k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
502 napi_disable(&common->napi_rx);
504 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
505 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
507 am65_cpsw_nuss_rx_cleanup, !!i);
509 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
511 cpsw_ale_stop(common->ale);
513 writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
514 writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
516 dev_dbg(common->dev, "cpsw_nuss stopped\n");
520 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
522 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
523 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
526 phylink_stop(port->slave.phylink);
528 netif_tx_stop_all_queues(ndev);
530 phylink_disconnect_phy(port->slave.phylink);
532 ret = am65_cpsw_nuss_common_stop(common);
536 common->usage_count--;
537 pm_runtime_put(common->dev);
541 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
543 struct am65_cpsw_port *port = arg;
548 return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
551 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
553 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
554 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
557 ret = pm_runtime_resume_and_get(common->dev);
561 /* Notify the stack of the actual queue counts. */
562 ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
564 dev_err(common->dev, "cannot set real number of tx queues\n");
568 ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
570 dev_err(common->dev, "cannot set real number of rx queues\n");
574 for (i = 0; i < common->tx_ch_num; i++)
575 netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i));
577 ret = am65_cpsw_nuss_common_open(common, ndev->features);
581 common->usage_count++;
583 am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
585 if (common->is_emac_mode)
586 am65_cpsw_init_port_emac_ale(port);
588 am65_cpsw_init_port_switch_ale(port);
590 /* mac_sl should be configured via phy-link interface */
591 am65_cpsw_sl_ctl_reset(port);
593 ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET,
598 ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0);
602 /* restore vlan configurations */
603 vlan_for_each(ndev, cpsw_restore_vlans, port);
605 phylink_start(port->slave.phylink);
610 am65_cpsw_nuss_ndo_slave_stop(ndev);
614 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
616 struct am65_cpsw_rx_chn *rx_chn = data;
617 struct cppi5_host_desc_t *desc_rx;
623 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
624 swdata = cppi5_hdesc_get_swdata(desc_rx);
626 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
627 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
629 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
630 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
632 dev_kfree_skb_any(skb);
635 static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata)
637 struct skb_shared_hwtstamps *ssh;
640 ns = ((u64)psdata[1] << 32) | psdata[0];
642 ssh = skb_hwtstamps(skb);
643 memset(ssh, 0, sizeof(*ssh));
644 ssh->hwtstamp = ns_to_ktime(ns);
647 /* RX psdata[2] word format - checksum information */
648 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0)
649 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16)
650 #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17)
651 #define AM65_CPSW_RX_PSD_IS_TCP BIT(18)
652 #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19)
653 #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20)
655 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
657 /* HW can verify IPv4/IPv6 TCP/UDP packets checksum
658 * csum information provides in psdata[2] word:
659 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
660 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
661 * bits - indicates IPv4/IPv6 packet
662 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
663 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
664 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
666 skb_checksum_none_assert(skb);
668 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
671 if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
672 AM65_CPSW_RX_PSD_IPV4_VALID)) &&
673 !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
674 /* csum for fragmented packets is unsupported */
675 if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
676 skb->ip_summed = CHECKSUM_UNNECESSARY;
680 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
683 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
684 u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
685 struct am65_cpsw_ndev_priv *ndev_priv;
686 struct am65_cpsw_ndev_stats *stats;
687 struct cppi5_host_desc_t *desc_rx;
688 struct device *dev = common->dev;
689 struct sk_buff *skb, *new_skb;
690 dma_addr_t desc_dma, buf_dma;
691 struct am65_cpsw_port *port;
692 struct net_device *ndev;
697 ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
700 dev_err(dev, "RX: pop chn fail %d\n", ret);
704 if (cppi5_desc_is_tdcm(desc_dma)) {
705 dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
709 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
710 dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
711 __func__, flow_idx, &desc_dma);
713 swdata = cppi5_hdesc_get_swdata(desc_rx);
715 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
716 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
717 pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
718 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
719 dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
720 port = am65_common_get_port(common, port_id);
724 psdata = cppi5_hdesc_get_psdata(desc_rx);
725 /* add RX timestamp */
726 if (port->rx_ts_enabled)
727 am65_cpsw_nuss_rx_ts(skb, psdata);
728 csum_info = psdata[2];
729 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
731 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
733 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
735 new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
737 ndev_priv = netdev_priv(ndev);
738 am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark);
739 skb_put(skb, pkt_len);
740 skb->protocol = eth_type_trans(skb, ndev);
741 am65_cpsw_nuss_rx_csum(skb, csum_info);
742 napi_gro_receive(&common->napi_rx, skb);
744 stats = this_cpu_ptr(ndev_priv->stats);
746 u64_stats_update_begin(&stats->syncp);
748 stats->rx_bytes += pkt_len;
749 u64_stats_update_end(&stats->syncp);
750 kmemleak_not_leak(new_skb);
752 ndev->stats.rx_dropped++;
756 if (netif_dormant(ndev)) {
757 dev_kfree_skb_any(new_skb);
758 ndev->stats.rx_dropped++;
762 ret = am65_cpsw_nuss_rx_push(common, new_skb);
763 if (WARN_ON(ret < 0)) {
764 dev_kfree_skb_any(new_skb);
765 ndev->stats.rx_errors++;
766 ndev->stats.rx_dropped++;
772 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
774 struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
775 int flow = AM65_CPSW_MAX_RX_FLOWS;
779 /* process every flow */
781 cur_budget = budget - num_rx;
783 while (cur_budget--) {
784 ret = am65_cpsw_nuss_rx_packets(common, flow);
790 if (num_rx >= budget)
794 dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
796 if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) {
797 if (common->rx_irq_disabled) {
798 common->rx_irq_disabled = false;
799 enable_irq(common->rx_chns.irq);
806 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
807 struct cppi5_host_desc_t *desc)
809 struct cppi5_host_desc_t *first_desc, *next_desc;
810 dma_addr_t buf_dma, next_desc_dma;
814 next_desc = first_desc;
816 cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
817 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
819 dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
821 next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
822 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
823 while (next_desc_dma) {
824 next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
826 cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
827 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
829 dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len,
832 next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
833 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
835 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
838 k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
841 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
843 struct am65_cpsw_tx_chn *tx_chn = data;
844 struct cppi5_host_desc_t *desc_tx;
848 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
849 swdata = cppi5_hdesc_get_swdata(desc_tx);
851 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
853 dev_kfree_skb_any(skb);
856 static struct sk_buff *
857 am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn,
860 struct am65_cpsw_ndev_priv *ndev_priv;
861 struct am65_cpsw_ndev_stats *stats;
862 struct cppi5_host_desc_t *desc_tx;
863 struct net_device *ndev;
867 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
869 swdata = cppi5_hdesc_get_swdata(desc_tx);
871 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
875 am65_cpts_tx_timestamp(tx_chn->common->cpts, skb);
877 ndev_priv = netdev_priv(ndev);
878 stats = this_cpu_ptr(ndev_priv->stats);
879 u64_stats_update_begin(&stats->syncp);
881 stats->tx_bytes += skb->len;
882 u64_stats_update_end(&stats->syncp);
887 static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev,
888 struct netdev_queue *netif_txq)
890 if (netif_tx_queue_stopped(netif_txq)) {
891 /* Check whether the queue is stopped due to stalled
892 * tx dma, if the queue is stopped then wake the queue
893 * as we have free desc for tx
895 __netif_tx_lock(netif_txq, smp_processor_id());
896 if (netif_running(ndev) &&
897 (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS))
898 netif_tx_wake_queue(netif_txq);
900 __netif_tx_unlock(netif_txq);
904 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
905 int chn, unsigned int budget)
907 struct device *dev = common->dev;
908 struct am65_cpsw_tx_chn *tx_chn;
909 struct netdev_queue *netif_txq;
910 unsigned int total_bytes = 0;
911 struct net_device *ndev;
916 tx_chn = &common->tx_chns[chn];
919 spin_lock(&tx_chn->lock);
920 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
921 spin_unlock(&tx_chn->lock);
925 if (cppi5_desc_is_tdcm(desc_dma)) {
926 if (atomic_dec_and_test(&common->tdown_cnt))
927 complete(&common->tdown_complete);
931 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
932 total_bytes = skb->len;
934 napi_consume_skb(skb, budget);
937 netif_txq = netdev_get_tx_queue(ndev, chn);
939 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
941 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
944 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
949 static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common,
950 int chn, unsigned int budget)
952 struct device *dev = common->dev;
953 struct am65_cpsw_tx_chn *tx_chn;
954 struct netdev_queue *netif_txq;
955 unsigned int total_bytes = 0;
956 struct net_device *ndev;
961 tx_chn = &common->tx_chns[chn];
964 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
968 if (cppi5_desc_is_tdcm(desc_dma)) {
969 if (atomic_dec_and_test(&common->tdown_cnt))
970 complete(&common->tdown_complete);
974 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
977 total_bytes += skb->len;
978 napi_consume_skb(skb, budget);
985 netif_txq = netdev_get_tx_queue(ndev, chn);
987 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
989 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
991 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
996 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
998 struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
1001 if (AM65_CPSW_IS_CPSW2G(tx_chn->common))
1002 num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget);
1004 num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget);
1006 if (num_tx >= budget)
1009 if (napi_complete_done(napi_tx, num_tx))
1010 enable_irq(tx_chn->irq);
1015 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
1017 struct am65_cpsw_common *common = dev_id;
1019 common->rx_irq_disabled = true;
1020 disable_irq_nosync(irq);
1021 napi_schedule(&common->napi_rx);
1026 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
1028 struct am65_cpsw_tx_chn *tx_chn = dev_id;
1030 disable_irq_nosync(irq);
1031 napi_schedule(&tx_chn->napi_tx);
1036 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
1037 struct net_device *ndev)
1039 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1040 struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
1041 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1042 struct device *dev = common->dev;
1043 struct am65_cpsw_tx_chn *tx_chn;
1044 struct netdev_queue *netif_txq;
1045 dma_addr_t desc_dma, buf_dma;
1051 /* padding enabled in hw */
1052 pkt_len = skb_headlen(skb);
1054 /* SKB TX timestamp */
1055 if (port->tx_ts_enabled)
1056 am65_cpts_prep_tx_timestamp(common->cpts, skb);
1058 q_idx = skb_get_queue_mapping(skb);
1059 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1061 tx_chn = &common->tx_chns[q_idx];
1062 netif_txq = netdev_get_tx_queue(ndev, q_idx);
1064 /* Map the linear buffer */
1065 buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len,
1067 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1068 dev_err(dev, "Failed to map tx skb buffer\n");
1069 ndev->stats.tx_errors++;
1073 first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1075 dev_dbg(dev, "Failed to allocate descriptor\n");
1076 dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len,
1081 cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1082 AM65_CPSW_NAV_PS_DATA_SIZE);
1083 cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
1084 cppi5_hdesc_set_pkttype(first_desc, 0x7);
1085 cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1087 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1088 cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1089 swdata = cppi5_hdesc_get_swdata(first_desc);
1091 psdata = cppi5_hdesc_get_psdata(first_desc);
1093 /* HW csum offload if enabled */
1095 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1096 unsigned int cs_start, cs_offset;
1098 cs_start = skb_transport_offset(skb);
1099 cs_offset = cs_start + skb->csum_offset;
1100 /* HW numerates bytes starting from 1 */
1101 psdata[2] = ((cs_offset + 1) << 24) |
1102 ((cs_start + 1) << 16) | (skb->len - cs_start);
1103 dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1106 if (!skb_is_nonlinear(skb))
1109 dev_dbg(dev, "fragmented SKB\n");
1111 /* Handle the case where skb is fragmented in pages */
1112 cur_desc = first_desc;
1113 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1114 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1115 u32 frag_size = skb_frag_size(frag);
1117 next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1119 dev_err(dev, "Failed to allocate descriptor\n");
1120 goto busy_free_descs;
1123 buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size,
1125 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1126 dev_err(dev, "Failed to map tx skb page\n");
1127 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1128 ndev->stats.tx_errors++;
1129 goto err_free_descs;
1132 cppi5_hdesc_reset_hbdesc(next_desc);
1133 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1134 cppi5_hdesc_attach_buf(next_desc,
1135 buf_dma, frag_size, buf_dma, frag_size);
1137 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1139 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma);
1140 cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1142 pkt_len += frag_size;
1143 cur_desc = next_desc;
1145 WARN_ON(pkt_len != skb->len);
1148 skb_tx_timestamp(skb);
1150 /* report bql before sending packet */
1151 netdev_tx_sent_queue(netif_txq, pkt_len);
1153 cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1154 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1155 if (AM65_CPSW_IS_CPSW2G(common)) {
1156 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1158 spin_lock_bh(&tx_chn->lock);
1159 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1160 spin_unlock_bh(&tx_chn->lock);
1163 dev_err(dev, "can't push desc %d\n", ret);
1165 netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1166 ndev->stats.tx_errors++;
1167 goto err_free_descs;
1170 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1171 netif_tx_stop_queue(netif_txq);
1172 /* Barrier, so that stop_queue visible to other cpus */
1173 smp_mb__after_atomic();
1174 dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1176 /* re-check for smp */
1177 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1179 netif_tx_wake_queue(netif_txq);
1180 dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1184 return NETDEV_TX_OK;
1187 am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1189 ndev->stats.tx_dropped++;
1190 dev_kfree_skb_any(skb);
1191 return NETDEV_TX_OK;
1194 am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1196 netif_tx_stop_queue(netif_txq);
1197 return NETDEV_TX_BUSY;
1200 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1203 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1204 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1205 struct sockaddr *sockaddr = (struct sockaddr *)addr;
1208 ret = eth_prepare_mac_addr_change(ndev, addr);
1212 ret = pm_runtime_resume_and_get(common->dev);
1216 cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1217 HOST_PORT_NUM, 0, 0);
1218 cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1219 HOST_PORT_NUM, ALE_SECURE, 0);
1221 am65_cpsw_port_set_sl_mac(port, addr);
1222 eth_commit_mac_addr_change(ndev, sockaddr);
1224 pm_runtime_put(common->dev);
1229 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
1232 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1233 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1234 u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
1235 struct hwtstamp_config cfg;
1237 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1240 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1243 /* TX HW timestamp */
1244 switch (cfg.tx_type) {
1245 case HWTSTAMP_TX_OFF:
1246 case HWTSTAMP_TX_ON:
1252 switch (cfg.rx_filter) {
1253 case HWTSTAMP_FILTER_NONE:
1254 port->rx_ts_enabled = false;
1256 case HWTSTAMP_FILTER_ALL:
1257 case HWTSTAMP_FILTER_SOME:
1258 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1259 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1260 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1261 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1262 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1263 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1264 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1265 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1266 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1267 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1268 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1269 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1270 case HWTSTAMP_FILTER_NTP_ALL:
1271 port->rx_ts_enabled = true;
1272 cfg.rx_filter = HWTSTAMP_FILTER_ALL;
1278 port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
1280 /* cfg TX timestamp */
1281 seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
1282 AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
1284 ts_vlan_ltype = ETH_P_8021Q;
1286 ts_ctrl_ltype2 = ETH_P_1588 |
1287 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
1288 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
1289 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
1290 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
1291 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
1292 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
1293 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
1294 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
1296 ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
1297 AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
1299 if (port->tx_ts_enabled)
1300 ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
1301 AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
1303 writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
1304 writel(ts_vlan_ltype, port->port_base +
1305 AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
1306 writel(ts_ctrl_ltype2, port->port_base +
1307 AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
1308 writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
1310 /* en/dis RX timestamp */
1311 am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled);
1313 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1316 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
1319 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1320 struct hwtstamp_config cfg;
1322 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1326 cfg.tx_type = port->tx_ts_enabled ?
1327 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1328 cfg.rx_filter = port->rx_ts_enabled ?
1329 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
1331 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1334 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1335 struct ifreq *req, int cmd)
1337 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1339 if (!netif_running(ndev))
1344 return am65_cpsw_nuss_hwtstamp_set(ndev, req);
1346 return am65_cpsw_nuss_hwtstamp_get(ndev, req);
1349 return phylink_mii_ioctl(port->slave.phylink, req, cmd);
1352 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1353 struct rtnl_link_stats64 *stats)
1355 struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1359 for_each_possible_cpu(cpu) {
1360 struct am65_cpsw_ndev_stats *cpu_stats;
1366 cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1368 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1369 rx_packets = cpu_stats->rx_packets;
1370 rx_bytes = cpu_stats->rx_bytes;
1371 tx_packets = cpu_stats->tx_packets;
1372 tx_bytes = cpu_stats->tx_bytes;
1373 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1375 stats->rx_packets += rx_packets;
1376 stats->rx_bytes += rx_bytes;
1377 stats->tx_packets += tx_packets;
1378 stats->tx_bytes += tx_bytes;
1381 stats->rx_errors = dev->stats.rx_errors;
1382 stats->rx_dropped = dev->stats.rx_dropped;
1383 stats->tx_dropped = dev->stats.tx_dropped;
1386 static struct devlink_port *am65_cpsw_ndo_get_devlink_port(struct net_device *ndev)
1388 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1390 return &port->devlink_port;
1393 static const struct net_device_ops am65_cpsw_nuss_netdev_ops = {
1394 .ndo_open = am65_cpsw_nuss_ndo_slave_open,
1395 .ndo_stop = am65_cpsw_nuss_ndo_slave_stop,
1396 .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit,
1397 .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode,
1398 .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats,
1399 .ndo_validate_addr = eth_validate_addr,
1400 .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address,
1401 .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout,
1402 .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid,
1403 .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid,
1404 .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl,
1405 .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc,
1406 .ndo_get_devlink_port = am65_cpsw_ndo_get_devlink_port,
1409 static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode,
1410 const struct phylink_link_state *state)
1412 /* Currently not used */
1415 static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode,
1416 phy_interface_t interface)
1418 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1420 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1421 struct am65_cpsw_common *common = port->common;
1422 struct net_device *ndev = port->ndev;
1425 /* disable forwarding */
1426 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1428 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
1430 tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
1431 dev_dbg(common->dev, "down msc_sl %08x tmo %d\n",
1432 cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo);
1434 cpsw_sl_ctl_reset(port->slave.mac_sl);
1436 am65_cpsw_qos_link_down(ndev);
1437 netif_tx_stop_all_queues(ndev);
1440 static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy,
1441 unsigned int mode, phy_interface_t interface, int speed,
1442 int duplex, bool tx_pause, bool rx_pause)
1444 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1446 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1447 struct am65_cpsw_common *common = port->common;
1448 u32 mac_control = CPSW_SL_CTL_GMII_EN;
1449 struct net_device *ndev = port->ndev;
1451 if (speed == SPEED_1000)
1452 mac_control |= CPSW_SL_CTL_GIG;
1453 if (speed == SPEED_10 && interface == PHY_INTERFACE_MODE_RGMII)
1454 /* Can be used with in band mode only */
1455 mac_control |= CPSW_SL_CTL_EXT_EN;
1456 if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII)
1457 mac_control |= CPSW_SL_CTL_IFCTL_A;
1459 mac_control |= CPSW_SL_CTL_FULLDUPLEX;
1461 /* rx_pause/tx_pause */
1463 mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
1466 mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
1468 cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
1470 /* enable forwarding */
1471 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1473 am65_cpsw_qos_link_up(ndev, speed);
1474 netif_tx_wake_all_queues(ndev);
1477 static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = {
1478 .validate = phylink_generic_validate,
1479 .mac_config = am65_cpsw_nuss_mac_config,
1480 .mac_link_down = am65_cpsw_nuss_mac_link_down,
1481 .mac_link_up = am65_cpsw_nuss_mac_link_up,
1484 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
1486 struct am65_cpsw_common *common = port->common;
1488 if (!port->disabled)
1491 cpsw_ale_control_set(common->ale, port->port_id,
1492 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1494 cpsw_sl_reset(port->slave.mac_sl, 100);
1495 cpsw_sl_ctl_reset(port->slave.mac_sl);
1498 static void am65_cpsw_nuss_free_tx_chns(void *data)
1500 struct am65_cpsw_common *common = data;
1503 for (i = 0; i < common->tx_ch_num; i++) {
1504 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1506 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1507 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1509 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1510 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1512 memset(tx_chn, 0, sizeof(*tx_chn));
1516 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
1518 struct device *dev = common->dev;
1521 devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1523 for (i = 0; i < common->tx_ch_num; i++) {
1524 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1527 devm_free_irq(dev, tx_chn->irq, tx_chn);
1529 netif_napi_del(&tx_chn->napi_tx);
1531 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1532 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1534 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1535 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1537 memset(tx_chn, 0, sizeof(*tx_chn));
1541 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
1543 u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
1544 struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
1545 struct device *dev = common->dev;
1546 struct k3_ring_cfg ring_cfg = {
1547 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1548 .mode = K3_RINGACC_RING_MODE_RING,
1554 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1555 AM65_CPSW_NAV_SW_DATA_SIZE);
1557 tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1558 tx_cfg.tx_cfg = ring_cfg;
1559 tx_cfg.txcq_cfg = ring_cfg;
1560 tx_cfg.tx_cfg.size = max_desc_num;
1561 tx_cfg.txcq_cfg.size = max_desc_num;
1563 for (i = 0; i < common->tx_ch_num; i++) {
1564 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1566 snprintf(tx_chn->tx_chn_name,
1567 sizeof(tx_chn->tx_chn_name), "tx%d", i);
1569 spin_lock_init(&tx_chn->lock);
1570 tx_chn->common = common;
1572 tx_chn->descs_num = max_desc_num;
1575 k3_udma_glue_request_tx_chn(dev,
1576 tx_chn->tx_chn_name,
1578 if (IS_ERR(tx_chn->tx_chn)) {
1579 ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn),
1580 "Failed to request tx dma channel\n");
1583 tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn);
1585 tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev,
1588 tx_chn->tx_chn_name);
1589 if (IS_ERR(tx_chn->desc_pool)) {
1590 ret = PTR_ERR(tx_chn->desc_pool);
1591 dev_err(dev, "Failed to create poll %d\n", ret);
1595 tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
1596 if (tx_chn->irq <= 0) {
1597 dev_err(dev, "Failed to get tx dma irq %d\n",
1602 snprintf(tx_chn->tx_chn_name,
1603 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
1604 dev_name(dev), tx_chn->id);
1608 i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1610 dev_err(dev, "Failed to add free_tx_chns action %d\n", i);
1617 static void am65_cpsw_nuss_free_rx_chns(void *data)
1619 struct am65_cpsw_common *common = data;
1620 struct am65_cpsw_rx_chn *rx_chn;
1622 rx_chn = &common->rx_chns;
1624 if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1625 k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1627 if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1628 k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1631 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
1633 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
1634 struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
1635 u32 max_desc_num = AM65_CPSW_MAX_RX_DESC;
1636 struct device *dev = common->dev;
1641 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1642 AM65_CPSW_NAV_SW_DATA_SIZE);
1644 rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1645 rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
1646 rx_cfg.flow_id_base = common->rx_flow_id_base;
1648 /* init all flows */
1650 rx_chn->descs_num = max_desc_num;
1652 rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
1653 if (IS_ERR(rx_chn->rx_chn)) {
1654 ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn),
1655 "Failed to request rx dma channel\n");
1658 rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn);
1660 rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev,
1663 if (IS_ERR(rx_chn->desc_pool)) {
1664 ret = PTR_ERR(rx_chn->desc_pool);
1665 dev_err(dev, "Failed to create rx poll %d\n", ret);
1669 common->rx_flow_id_base =
1670 k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
1671 dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
1673 fdqring_id = K3_RINGACC_RING_ID_ANY;
1674 for (i = 0; i < rx_cfg.flow_id_num; i++) {
1675 struct k3_ring_cfg rxring_cfg = {
1676 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1677 .mode = K3_RINGACC_RING_MODE_RING,
1680 struct k3_ring_cfg fdqring_cfg = {
1681 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1682 .flags = K3_RINGACC_RING_SHARED,
1684 struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
1685 .rx_cfg = rxring_cfg,
1686 .rxfdq_cfg = fdqring_cfg,
1687 .ring_rxq_id = K3_RINGACC_RING_ID_ANY,
1689 K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
1692 rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
1693 rx_flow_cfg.rx_cfg.size = max_desc_num;
1694 rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
1695 rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode;
1697 ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
1700 dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
1705 k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
1708 rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
1710 if (rx_chn->irq <= 0) {
1711 dev_err(dev, "Failed to get rx dma irq %d\n",
1719 i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1721 dev_err(dev, "Failed to add free_rx_chns action %d\n", i);
1728 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
1730 struct am65_cpsw_host *host_p = am65_common_get_host(common);
1732 host_p->common = common;
1733 host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
1734 host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
1739 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
1740 int slave, u8 *mac_addr)
1742 u32 mac_lo, mac_hi, offset;
1743 struct regmap *syscon;
1746 syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
1747 if (IS_ERR(syscon)) {
1748 if (PTR_ERR(syscon) == -ENODEV)
1750 return PTR_ERR(syscon);
1753 ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
1758 regmap_read(syscon, offset, &mac_lo);
1759 regmap_read(syscon, offset + 4, &mac_hi);
1761 mac_addr[0] = (mac_hi >> 8) & 0xff;
1762 mac_addr[1] = mac_hi & 0xff;
1763 mac_addr[2] = (mac_lo >> 24) & 0xff;
1764 mac_addr[3] = (mac_lo >> 16) & 0xff;
1765 mac_addr[4] = (mac_lo >> 8) & 0xff;
1766 mac_addr[5] = mac_lo & 0xff;
1771 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
1773 struct device *dev = common->dev;
1774 struct device_node *node;
1775 struct am65_cpts *cpts;
1776 void __iomem *reg_base;
1778 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1781 node = of_get_child_by_name(dev->of_node, "cpts");
1783 dev_err(dev, "%s cpts not found\n", __func__);
1787 reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
1788 cpts = am65_cpts_create(dev, reg_base, node);
1790 int ret = PTR_ERR(cpts);
1793 if (ret == -EOPNOTSUPP) {
1794 dev_info(dev, "cpts disabled\n");
1798 dev_err(dev, "cpts create err %d\n", ret);
1801 common->cpts = cpts;
1802 /* Forbid PM runtime if CPTS is running.
1803 * K3 CPSWxG modules may completely lose context during ON->OFF
1804 * transitions depending on integration.
1805 * AM65x/J721E MCU CPSW2G: false
1806 * J721E MAIN_CPSW9G: true
1808 pm_runtime_forbid(dev);
1813 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
1815 struct device_node *node, *port_np;
1816 struct device *dev = common->dev;
1819 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
1823 for_each_child_of_node(node, port_np) {
1824 struct am65_cpsw_port *port;
1827 /* it is not a slave port node, continue */
1828 if (strcmp(port_np->name, "port"))
1831 ret = of_property_read_u32(port_np, "reg", &port_id);
1833 dev_err(dev, "%pOF error reading port_id %d\n",
1838 if (!port_id || port_id > common->port_num) {
1839 dev_err(dev, "%pOF has invalid port_id %u %s\n",
1840 port_np, port_id, port_np->name);
1845 port = am65_common_get_port(common, port_id);
1846 port->port_id = port_id;
1847 port->common = common;
1848 port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
1849 AM65_CPSW_NU_PORTS_OFFSET * (port_id);
1850 port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
1851 (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
1852 port->name = of_get_property(port_np, "label", NULL);
1853 port->fetch_ram_base =
1854 common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
1855 (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
1857 port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
1858 if (IS_ERR(port->slave.mac_sl)) {
1859 ret = PTR_ERR(port->slave.mac_sl);
1863 port->disabled = !of_device_is_available(port_np);
1864 if (port->disabled) {
1865 common->disabled_ports_mask |= BIT(port->port_id);
1869 port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
1870 if (IS_ERR(port->slave.ifphy)) {
1871 ret = PTR_ERR(port->slave.ifphy);
1872 dev_err(dev, "%pOF error retrieving port phy: %d\n",
1877 port->slave.mac_only =
1878 of_property_read_bool(port_np, "ti,mac-only");
1880 /* get phy/link info */
1881 port->slave.phy_node = port_np;
1882 ret = of_get_phy_mode(port_np, &port->slave.phy_if);
1884 dev_err(dev, "%pOF read phy-mode err %d\n",
1889 ret = of_get_mac_address(port_np, port->slave.mac_addr);
1891 am65_cpsw_am654_get_efuse_macid(port_np,
1893 port->slave.mac_addr);
1894 if (!is_valid_ether_addr(port->slave.mac_addr)) {
1895 eth_random_addr(port->slave.mac_addr);
1896 dev_err(dev, "Use random MAC address\n");
1902 /* is there at least one ext.port */
1903 if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) {
1904 dev_err(dev, "No Ext. port are available\n");
1911 of_node_put(port_np);
1916 static void am65_cpsw_pcpu_stats_free(void *data)
1918 struct am65_cpsw_ndev_stats __percpu *stats = data;
1923 static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common)
1925 struct am65_cpsw_port *port;
1928 for (i = 0; i < common->port_num; i++) {
1929 port = &common->ports[i];
1930 if (port->slave.phylink)
1931 phylink_destroy(port->slave.phylink);
1936 am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx)
1938 struct am65_cpsw_ndev_priv *ndev_priv;
1939 struct device *dev = common->dev;
1940 struct am65_cpsw_port *port;
1941 struct phylink *phylink;
1944 port = &common->ports[port_idx];
1950 port->ndev = devm_alloc_etherdev_mqs(common->dev,
1951 sizeof(struct am65_cpsw_ndev_priv),
1952 AM65_CPSW_MAX_TX_QUEUES,
1953 AM65_CPSW_MAX_RX_QUEUES);
1955 dev_err(dev, "error allocating slave net_device %u\n",
1960 ndev_priv = netdev_priv(port->ndev);
1961 ndev_priv->port = port;
1962 ndev_priv->msg_enable = AM65_CPSW_DEBUG;
1963 SET_NETDEV_DEV(port->ndev, dev);
1965 eth_hw_addr_set(port->ndev, port->slave.mac_addr);
1967 port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
1968 port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE;
1969 port->ndev->hw_features = NETIF_F_SG |
1973 port->ndev->features = port->ndev->hw_features |
1974 NETIF_F_HW_VLAN_CTAG_FILTER;
1975 port->ndev->vlan_features |= NETIF_F_SG;
1976 port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops;
1977 port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
1979 /* Configuring Phylink */
1980 port->slave.phylink_config.dev = &port->ndev->dev;
1981 port->slave.phylink_config.type = PHYLINK_NETDEV;
1982 port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000FD;
1984 phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces);
1986 phylink = phylink_create(&port->slave.phylink_config,
1987 of_node_to_fwnode(port->slave.phy_node),
1989 &am65_cpsw_phylink_mac_ops);
1990 if (IS_ERR(phylink))
1991 return PTR_ERR(phylink);
1993 port->slave.phylink = phylink;
1995 /* Disable TX checksum offload by default due to HW bug */
1996 if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
1997 port->ndev->features &= ~NETIF_F_HW_CSUM;
1999 ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
2000 if (!ndev_priv->stats)
2003 ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
2006 dev_err(dev, "failed to add percpu stat free action %d\n", ret);
2008 if (!common->dma_ndev)
2009 common->dma_ndev = port->ndev;
2014 static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common)
2019 for (i = 0; i < common->port_num; i++) {
2020 ret = am65_cpsw_nuss_init_port_ndev(common, i);
2025 netif_napi_add(common->dma_ndev, &common->napi_rx,
2026 am65_cpsw_nuss_rx_poll, NAPI_POLL_WEIGHT);
2031 static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common)
2033 struct device *dev = common->dev;
2036 for (i = 0; i < common->tx_ch_num; i++) {
2037 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
2039 netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx,
2040 am65_cpsw_nuss_tx_poll);
2042 ret = devm_request_irq(dev, tx_chn->irq,
2043 am65_cpsw_nuss_tx_irq,
2045 tx_chn->tx_chn_name, tx_chn);
2047 dev_err(dev, "failure requesting tx%u irq %u, %d\n",
2048 tx_chn->id, tx_chn->irq, ret);
2057 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
2059 struct am65_cpsw_port *port;
2062 for (i = 0; i < common->port_num; i++) {
2063 port = &common->ports[i];
2065 unregister_netdev(port->ndev);
2069 static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common)
2074 if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask))
2077 dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val);
2079 for (i = 1; i <= common->port_num; i++) {
2080 struct am65_cpsw_port *port = am65_common_get_port(common, i);
2081 struct am65_cpsw_ndev_priv *priv;
2086 priv = am65_ndev_to_priv(port->ndev);
2087 priv->offload_fwd_mark = set_val;
2091 bool am65_cpsw_port_dev_check(const struct net_device *ndev)
2093 if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) {
2094 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2096 return !common->is_emac_mode;
2102 static int am65_cpsw_netdevice_port_link(struct net_device *ndev,
2103 struct net_device *br_ndev,
2104 struct netlink_ext_ack *extack)
2106 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2107 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2110 if (!common->br_members) {
2111 common->hw_bridge_dev = br_ndev;
2113 /* This is adding the port to a second bridge, this is
2116 if (common->hw_bridge_dev != br_ndev)
2120 err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
2125 common->br_members |= BIT(priv->port->port_id);
2127 am65_cpsw_port_offload_fwd_mark_update(common);
2132 static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev)
2134 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2135 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2137 switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
2139 common->br_members &= ~BIT(priv->port->port_id);
2141 am65_cpsw_port_offload_fwd_mark_update(common);
2143 if (!common->br_members)
2144 common->hw_bridge_dev = NULL;
2147 /* netdev notifier */
2148 static int am65_cpsw_netdevice_event(struct notifier_block *unused,
2149 unsigned long event, void *ptr)
2151 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
2152 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2153 struct netdev_notifier_changeupper_info *info;
2154 int ret = NOTIFY_DONE;
2156 if (!am65_cpsw_port_dev_check(ndev))
2160 case NETDEV_CHANGEUPPER:
2163 if (netif_is_bridge_master(info->upper_dev)) {
2165 ret = am65_cpsw_netdevice_port_link(ndev,
2169 am65_cpsw_netdevice_port_unlink(ndev);
2176 return notifier_from_errno(ret);
2179 static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw)
2183 if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2184 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2187 cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event;
2188 ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2190 dev_err(cpsw->dev, "can't register netdevice notifier\n");
2194 ret = am65_cpsw_switchdev_register_notifiers(cpsw);
2196 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2201 static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw)
2203 if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2204 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2207 am65_cpsw_switchdev_unregister_notifiers(cpsw);
2208 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2211 static const struct devlink_ops am65_cpsw_devlink_ops = {};
2213 static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw)
2215 cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0,
2216 ALE_MCAST_BLOCK_LEARN_FWD);
2219 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common)
2221 struct am65_cpsw_host *host = am65_common_get_host(common);
2223 writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2225 am65_cpsw_init_stp_ale_entry(common);
2227 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
2228 dev_dbg(common->dev, "Set P0_UNI_FLOOD\n");
2229 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
2232 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common)
2234 struct am65_cpsw_host *host = am65_common_get_host(common);
2236 writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2238 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
2239 dev_dbg(common->dev, "unset P0_UNI_FLOOD\n");
2241 /* learning make no sense in multi-mac mode */
2242 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
2245 static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
2246 struct devlink_param_gset_ctx *ctx)
2248 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2249 struct am65_cpsw_common *common = dl_priv->common;
2251 dev_dbg(common->dev, "%s id:%u\n", __func__, id);
2253 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2256 ctx->val.vbool = !common->is_emac_mode;
2261 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port)
2263 struct am65_cpsw_slave_data *slave = &port->slave;
2264 struct am65_cpsw_common *common = port->common;
2267 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2269 if (slave->mac_only)
2270 /* enable mac-only mode on port */
2271 cpsw_ale_control_set(common->ale, port->port_id,
2272 ALE_PORT_MACONLY, 1);
2274 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1);
2276 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2278 cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr,
2279 HOST_PORT_NUM, ALE_SECURE, slave->port_vlan);
2280 cpsw_ale_add_mcast(common->ale, port->ndev->broadcast,
2281 port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2);
2284 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port)
2286 struct am65_cpsw_slave_data *slave = &port->slave;
2287 struct am65_cpsw_common *cpsw = port->common;
2290 cpsw_ale_control_set(cpsw->ale, port->port_id,
2291 ALE_PORT_NOLEARN, 0);
2293 cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr,
2294 HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN,
2297 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2299 cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast,
2300 port_mask, ALE_VLAN, slave->port_vlan,
2303 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2305 cpsw_ale_control_set(cpsw->ale, port->port_id,
2306 ALE_PORT_MACONLY, 0);
2309 static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
2310 struct devlink_param_gset_ctx *ctx)
2312 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2313 struct am65_cpsw_common *cpsw = dl_priv->common;
2314 bool switch_en = ctx->val.vbool;
2315 bool if_running = false;
2318 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
2320 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2323 if (switch_en == !cpsw->is_emac_mode)
2326 if (!switch_en && cpsw->br_members) {
2327 dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n");
2333 cpsw->is_emac_mode = !switch_en;
2335 for (i = 0; i < cpsw->port_num; i++) {
2336 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2338 if (!sl_ndev || !netif_running(sl_ndev))
2345 /* all ndevs are down */
2346 for (i = 0; i < cpsw->port_num; i++) {
2347 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2348 struct am65_cpsw_slave_data *slave;
2353 slave = am65_ndev_to_slave(sl_ndev);
2355 slave->port_vlan = cpsw->default_vlan;
2357 slave->port_vlan = 0;
2363 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
2364 /* clean up ALE table */
2365 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1);
2366 cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT);
2369 dev_info(cpsw->dev, "Enable switch mode\n");
2371 am65_cpsw_init_host_port_switch(cpsw);
2373 for (i = 0; i < cpsw->port_num; i++) {
2374 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2375 struct am65_cpsw_slave_data *slave;
2376 struct am65_cpsw_port *port;
2381 port = am65_ndev_to_port(sl_ndev);
2382 slave = am65_ndev_to_slave(sl_ndev);
2383 slave->port_vlan = cpsw->default_vlan;
2385 if (netif_running(sl_ndev))
2386 am65_cpsw_init_port_switch_ale(port);
2390 dev_info(cpsw->dev, "Disable switch mode\n");
2392 am65_cpsw_init_host_port_emac(cpsw);
2394 for (i = 0; i < cpsw->port_num; i++) {
2395 struct net_device *sl_ndev = cpsw->ports[i].ndev;
2396 struct am65_cpsw_port *port;
2401 port = am65_ndev_to_port(sl_ndev);
2402 port->slave.port_vlan = 0;
2403 if (netif_running(sl_ndev))
2404 am65_cpsw_init_port_emac_ale(port);
2407 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0);
2414 static const struct devlink_param am65_cpsw_devlink_params[] = {
2415 DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode",
2416 DEVLINK_PARAM_TYPE_BOOL,
2417 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
2418 am65_cpsw_dl_switch_mode_get,
2419 am65_cpsw_dl_switch_mode_set, NULL),
2422 static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common)
2424 struct devlink_port_attrs attrs = {};
2425 struct am65_cpsw_devlink *dl_priv;
2426 struct device *dev = common->dev;
2427 struct devlink_port *dl_port;
2428 struct am65_cpsw_port *port;
2433 devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev);
2434 if (!common->devlink)
2437 dl_priv = devlink_priv(common->devlink);
2438 dl_priv->common = common;
2440 /* Provide devlink hook to switch mode when multiple external ports
2441 * are present NUSS switchdev driver is enabled.
2443 if (!AM65_CPSW_IS_CPSW2G(common) &&
2444 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) {
2445 ret = devlink_params_register(common->devlink,
2446 am65_cpsw_devlink_params,
2447 ARRAY_SIZE(am65_cpsw_devlink_params));
2449 dev_err(dev, "devlink params reg fail ret:%d\n", ret);
2454 for (i = 1; i <= common->port_num; i++) {
2455 port = am65_common_get_port(common, i);
2456 dl_port = &port->devlink_port;
2458 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
2459 attrs.phys.port_number = port->port_id;
2460 attrs.switch_id.id_len = sizeof(resource_size_t);
2461 memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len);
2462 devlink_port_attrs_set(dl_port, &attrs);
2464 ret = devlink_port_register(common->devlink, dl_port, port->port_id);
2466 dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n",
2467 port->port_id, ret);
2471 devlink_register(common->devlink);
2475 for (i = i - 1; i >= 1; i--) {
2476 port = am65_common_get_port(common, i);
2477 dl_port = &port->devlink_port;
2479 devlink_port_unregister(dl_port);
2482 devlink_free(common->devlink);
2486 static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common)
2488 struct devlink_port *dl_port;
2489 struct am65_cpsw_port *port;
2492 devlink_unregister(common->devlink);
2494 for (i = 1; i <= common->port_num; i++) {
2495 port = am65_common_get_port(common, i);
2496 dl_port = &port->devlink_port;
2498 devlink_port_unregister(dl_port);
2501 if (!AM65_CPSW_IS_CPSW2G(common) &&
2502 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2503 devlink_params_unregister(common->devlink,
2504 am65_cpsw_devlink_params,
2505 ARRAY_SIZE(am65_cpsw_devlink_params));
2507 devlink_free(common->devlink);
2510 static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common)
2512 struct device *dev = common->dev;
2513 struct devlink_port *dl_port;
2514 struct am65_cpsw_port *port;
2517 ret = am65_cpsw_nuss_ndev_add_tx_napi(common);
2521 ret = devm_request_irq(dev, common->rx_chns.irq,
2522 am65_cpsw_nuss_rx_irq,
2523 IRQF_TRIGGER_HIGH, dev_name(dev), common);
2525 dev_err(dev, "failure requesting rx irq %u, %d\n",
2526 common->rx_chns.irq, ret);
2530 ret = am65_cpsw_nuss_register_devlink(common);
2534 for (i = 0; i < common->port_num; i++) {
2535 port = &common->ports[i];
2540 ret = register_netdev(port->ndev);
2542 dev_err(dev, "error registering slave net device%i %d\n",
2544 goto err_cleanup_ndev;
2547 dl_port = &port->devlink_port;
2548 devlink_port_type_eth_set(dl_port, port->ndev);
2551 ret = am65_cpsw_register_notifiers(common);
2553 goto err_cleanup_ndev;
2555 /* can't auto unregister ndev using devm_add_action() due to
2556 * devres release sequence in DD core for DMA
2562 am65_cpsw_nuss_cleanup_ndev(common);
2563 am65_cpsw_unregister_devlink(common);
2568 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
2572 common->tx_ch_num = num_tx;
2573 ret = am65_cpsw_nuss_init_tx_chns(common);
2577 return am65_cpsw_nuss_ndev_add_tx_napi(common);
2580 struct am65_cpsw_soc_pdata {
2584 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
2585 .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2588 static const struct soc_device_attribute am65_cpsw_socinfo[] = {
2589 { .family = "AM65X",
2590 .revision = "SR2.0",
2591 .data = &am65x_soc_sr2_0
2596 static const struct am65_cpsw_pdata am65x_sr1_0 = {
2597 .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2598 .ale_dev_id = "am65x-cpsw2g",
2599 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2602 static const struct am65_cpsw_pdata j721e_pdata = {
2604 .ale_dev_id = "am65x-cpsw2g",
2605 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2608 static const struct am65_cpsw_pdata am64x_cpswxg_pdata = {
2610 .ale_dev_id = "am64-cpswxg",
2611 .fdqring_mode = K3_RINGACC_RING_MODE_RING,
2614 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
2615 { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
2616 { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
2617 { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata},
2620 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
2622 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
2624 const struct soc_device_attribute *soc;
2626 soc = soc_device_match(am65_cpsw_socinfo);
2627 if (soc && soc->data) {
2628 const struct am65_cpsw_soc_pdata *socdata = soc->data;
2630 /* disable quirks */
2631 common->pdata.quirks &= ~socdata->quirks_dis;
2635 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
2637 struct cpsw_ale_params ale_params = { 0 };
2638 const struct of_device_id *of_id;
2639 struct device *dev = &pdev->dev;
2640 struct am65_cpsw_common *common;
2641 struct device_node *node;
2642 struct resource *res;
2647 common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
2652 of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
2655 common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
2657 am65_cpsw_nuss_apply_socinfo(common);
2659 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
2660 common->ss_base = devm_ioremap_resource(&pdev->dev, res);
2661 if (IS_ERR(common->ss_base))
2662 return PTR_ERR(common->ss_base);
2663 common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
2664 /* Use device's physical base address as switch id */
2665 id_temp = cpu_to_be64(res->start);
2666 memcpy(common->switch_id, &id_temp, sizeof(res->start));
2668 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2671 common->port_num = of_get_child_count(node);
2673 if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
2676 common->rx_flow_id_base = -1;
2677 init_completion(&common->tdown_complete);
2678 common->tx_ch_num = 1;
2679 common->pf_p0_rx_ptype_rrobin = false;
2680 common->default_vlan = 1;
2682 common->ports = devm_kcalloc(dev, common->port_num,
2683 sizeof(*common->ports),
2688 clk = devm_clk_get(dev, "fck");
2690 return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n");
2691 common->bus_freq = clk_get_rate(clk);
2693 pm_runtime_enable(dev);
2694 ret = pm_runtime_resume_and_get(dev);
2696 pm_runtime_disable(dev);
2700 node = of_get_child_by_name(dev->of_node, "mdio");
2702 dev_warn(dev, "MDIO node not found\n");
2703 } else if (of_device_is_available(node)) {
2704 struct platform_device *mdio_pdev;
2706 mdio_pdev = of_platform_device_create(node, NULL, dev);
2712 common->mdio_dev = &mdio_pdev->dev;
2716 am65_cpsw_nuss_get_ver(common);
2718 /* init tx channels */
2719 ret = am65_cpsw_nuss_init_tx_chns(common);
2722 ret = am65_cpsw_nuss_init_rx_chns(common);
2726 ret = am65_cpsw_nuss_init_host_p(common);
2730 ret = am65_cpsw_nuss_init_slave_ports(common);
2734 /* init common data */
2735 ale_params.dev = dev;
2736 ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
2737 ale_params.ale_ports = common->port_num + 1;
2738 ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
2739 ale_params.dev_id = common->pdata.ale_dev_id;
2740 ale_params.bus_freq = common->bus_freq;
2742 common->ale = cpsw_ale_create(&ale_params);
2743 if (IS_ERR(common->ale)) {
2744 dev_err(dev, "error initializing ale engine\n");
2745 ret = PTR_ERR(common->ale);
2749 ret = am65_cpsw_init_cpts(common);
2754 for (i = 0; i < common->port_num; i++)
2755 am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
2757 dev_set_drvdata(dev, common);
2759 common->is_emac_mode = true;
2761 ret = am65_cpsw_nuss_init_ndevs(common);
2763 goto err_free_phylink;
2765 ret = am65_cpsw_nuss_register_ndevs(common);
2767 goto err_free_phylink;
2769 pm_runtime_put(dev);
2773 am65_cpsw_nuss_phylink_cleanup(common);
2775 of_platform_device_destroy(common->mdio_dev, NULL);
2777 pm_runtime_put_sync(dev);
2778 pm_runtime_disable(dev);
2782 static int am65_cpsw_nuss_remove(struct platform_device *pdev)
2784 struct device *dev = &pdev->dev;
2785 struct am65_cpsw_common *common;
2788 common = dev_get_drvdata(dev);
2790 ret = pm_runtime_resume_and_get(&pdev->dev);
2794 am65_cpsw_nuss_phylink_cleanup(common);
2795 am65_cpsw_unregister_devlink(common);
2796 am65_cpsw_unregister_notifiers(common);
2798 /* must unregister ndevs here because DD release_driver routine calls
2799 * dma_deconfigure(dev) before devres_release_all(dev)
2801 am65_cpsw_nuss_cleanup_ndev(common);
2803 of_platform_device_destroy(common->mdio_dev, NULL);
2805 pm_runtime_put_sync(&pdev->dev);
2806 pm_runtime_disable(&pdev->dev);
2810 static struct platform_driver am65_cpsw_nuss_driver = {
2812 .name = AM65_CPSW_DRV_NAME,
2813 .of_match_table = am65_cpsw_nuss_of_mtable,
2815 .probe = am65_cpsw_nuss_probe,
2816 .remove = am65_cpsw_nuss_remove,
2819 module_platform_driver(am65_cpsw_nuss_driver);
2821 MODULE_LICENSE("GPL v2");
2822 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
2823 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");