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/sys_soc.h>
27 #include <linux/dma/ti-cppi5.h>
28 #include <linux/dma/k3-udma-glue.h>
32 #include "am65-cpsw-nuss.h"
33 #include "k3-cppi-desc-pool.h"
34 #include "am65-cpts.h"
36 #define AM65_CPSW_SS_BASE 0x0
37 #define AM65_CPSW_SGMII_BASE 0x100
38 #define AM65_CPSW_XGMII_BASE 0x2100
39 #define AM65_CPSW_CPSW_NU_BASE 0x20000
40 #define AM65_CPSW_NU_PORTS_BASE 0x1000
41 #define AM65_CPSW_NU_FRAM_BASE 0x12000
42 #define AM65_CPSW_NU_STATS_BASE 0x1a000
43 #define AM65_CPSW_NU_ALE_BASE 0x1e000
44 #define AM65_CPSW_NU_CPTS_BASE 0x1d000
46 #define AM65_CPSW_NU_PORTS_OFFSET 0x1000
47 #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200
48 #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200
50 #define AM65_CPSW_MAX_PORTS 8
52 #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN
53 #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
55 #define AM65_CPSW_REG_CTL 0x004
56 #define AM65_CPSW_REG_STAT_PORT_EN 0x014
57 #define AM65_CPSW_REG_PTYPE 0x018
59 #define AM65_CPSW_P0_REG_CTL 0x004
60 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008
62 #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c
63 #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020
64 #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024
66 #define AM65_CPSW_PORTN_REG_SA_L 0x308
67 #define AM65_CPSW_PORTN_REG_SA_H 0x30c
68 #define AM65_CPSW_PORTN_REG_TS_CTL 0x310
69 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314
70 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318
71 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C
73 #define AM65_CPSW_CTL_VLAN_AWARE BIT(1)
74 #define AM65_CPSW_CTL_P0_ENABLE BIT(2)
75 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13)
76 #define AM65_CPSW_CTL_P0_RX_PAD BIT(14)
78 /* AM65_CPSW_P0_REG_CTL */
79 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0)
81 /* AM65_CPSW_PORT_REG_PRI_CTL */
82 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8)
84 /* AM65_CPSW_PN_TS_CTL register fields */
85 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4)
86 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5)
87 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6)
88 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7)
89 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10)
90 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11)
91 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16
93 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
94 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16
96 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
97 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16)
98 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17)
99 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18)
100 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19)
101 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20)
102 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21)
103 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22)
104 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
106 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
107 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
109 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
111 #define AM65_CPSW_TS_TX_ANX_ALL_EN \
112 (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \
113 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \
114 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
116 #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30
117 /* Number of TX/RX descriptors */
118 #define AM65_CPSW_MAX_TX_DESC 500
119 #define AM65_CPSW_MAX_RX_DESC 500
121 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
122 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
124 #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
125 NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
126 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
128 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
131 u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
132 (dev_addr[2] << 16) | (dev_addr[3] << 24);
133 u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
135 writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
136 writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
139 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
141 cpsw_sl_reset(port->slave.mac_sl, 100);
142 /* Max length register has to be restored after MAC SL reset */
143 writel(AM65_CPSW_MAX_PACKET_SIZE,
144 port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
147 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
149 common->nuss_ver = readl(common->ss_base);
150 common->cpsw_ver = readl(common->cpsw_base);
151 dev_info(common->dev,
152 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
155 common->port_num + 1,
156 common->pdata.quirks);
159 void am65_cpsw_nuss_adjust_link(struct net_device *ndev)
161 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
162 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
163 struct phy_device *phy = port->slave.phy;
170 mac_control = CPSW_SL_CTL_GMII_EN;
172 if (phy->speed == 1000)
173 mac_control |= CPSW_SL_CTL_GIG;
174 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
175 /* Can be used with in band mode only */
176 mac_control |= CPSW_SL_CTL_EXT_EN;
178 mac_control |= CPSW_SL_CTL_FULLDUPLEX;
180 /* RGMII speed is 100M if !CPSW_SL_CTL_GIG*/
182 /* rx_pause/tx_pause */
183 if (port->slave.rx_pause)
184 mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
186 if (port->slave.tx_pause)
187 mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
189 cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
191 /* enable forwarding */
192 cpsw_ale_control_set(common->ale, port->port_id,
193 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
195 am65_cpsw_qos_link_up(ndev, phy->speed);
196 netif_tx_wake_all_queues(ndev);
200 /* disable forwarding */
201 cpsw_ale_control_set(common->ale, port->port_id,
202 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
204 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
206 tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
207 dev_dbg(common->dev, "donw msc_sl %08x tmo %d\n",
208 cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS),
211 cpsw_sl_ctl_reset(port->slave.mac_sl);
213 am65_cpsw_qos_link_down(ndev);
214 netif_tx_stop_all_queues(ndev);
217 phy_print_status(phy);
220 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
221 __be16 proto, u16 vid)
223 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
224 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
225 u32 port_mask, unreg_mcast = 0;
228 if (!netif_running(ndev) || !vid)
231 ret = pm_runtime_get_sync(common->dev);
233 pm_runtime_put_noidle(common->dev);
237 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
239 unreg_mcast = port_mask;
240 dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
241 ret = cpsw_ale_add_vlan(common->ale, vid, port_mask,
242 unreg_mcast, port_mask, 0);
244 pm_runtime_put(common->dev);
248 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
249 __be16 proto, u16 vid)
251 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
254 if (!netif_running(ndev) || !vid)
257 ret = pm_runtime_get_sync(common->dev);
259 pm_runtime_put_noidle(common->dev);
263 dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
264 ret = cpsw_ale_del_vlan(common->ale, vid, 0);
266 pm_runtime_put(common->dev);
270 static void am65_cpsw_slave_set_promisc_2g(struct am65_cpsw_port *port,
273 struct am65_cpsw_common *common = port->common;
276 /* Enable promiscuous mode */
277 cpsw_ale_control_set(common->ale, port->port_id,
278 ALE_PORT_MACONLY_CAF, 1);
279 dev_dbg(common->dev, "promisc enabled\n");
281 /* Disable promiscuous mode */
282 cpsw_ale_control_set(common->ale, port->port_id,
283 ALE_PORT_MACONLY_CAF, 0);
284 dev_dbg(common->dev, "promisc disabled\n");
288 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
290 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
291 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
295 promisc = !!(ndev->flags & IFF_PROMISC);
296 am65_cpsw_slave_set_promisc_2g(port, promisc);
301 /* Restore allmulti on vlans if necessary */
302 cpsw_ale_set_allmulti(common->ale,
303 ndev->flags & IFF_ALLMULTI, port->port_id);
305 port_mask = ALE_PORT_HOST;
306 /* Clear all mcast from ALE */
307 cpsw_ale_flush_multicast(common->ale, port_mask, -1);
309 if (!netdev_mc_empty(ndev)) {
310 struct netdev_hw_addr *ha;
312 /* program multicast address list into ALE register */
313 netdev_for_each_mc_addr(ha, ndev) {
314 cpsw_ale_add_mcast(common->ale, ha->addr,
320 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
321 unsigned int txqueue)
323 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
324 struct am65_cpsw_tx_chn *tx_chn;
325 struct netdev_queue *netif_txq;
326 unsigned long trans_start;
328 netif_txq = netdev_get_tx_queue(ndev, txqueue);
329 tx_chn = &common->tx_chns[txqueue];
330 trans_start = netif_txq->trans_start;
332 netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
334 netif_tx_queue_stopped(netif_txq),
335 jiffies_to_msecs(jiffies - trans_start),
336 dql_avail(&netif_txq->dql),
337 k3_cppi_desc_pool_avail(tx_chn->desc_pool));
339 if (netif_tx_queue_stopped(netif_txq)) {
340 /* try recover if stopped by us */
341 txq_trans_update(netif_txq);
342 netif_tx_wake_queue(netif_txq);
346 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
349 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
350 struct cppi5_host_desc_t *desc_rx;
351 struct device *dev = common->dev;
352 u32 pkt_len = skb_tailroom(skb);
357 desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
359 dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
362 desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
364 buf_dma = dma_map_single(dev, skb->data, pkt_len, DMA_FROM_DEVICE);
365 if (unlikely(dma_mapping_error(dev, buf_dma))) {
366 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
367 dev_err(dev, "Failed to map rx skb buffer\n");
371 cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
372 AM65_CPSW_NAV_PS_DATA_SIZE);
373 cppi5_hdesc_attach_buf(desc_rx, 0, 0, buf_dma, skb_tailroom(skb));
374 swdata = cppi5_hdesc_get_swdata(desc_rx);
375 *((void **)swdata) = skb;
377 return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
380 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
382 struct am65_cpsw_host *host_p = am65_common_get_host(common);
385 /* P0 set Receive Priority Type */
386 val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
388 if (common->pf_p0_rx_ptype_rrobin) {
389 val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
390 /* Enet Ports fifos works in fixed priority mode only, so
391 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
395 val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
396 /* restore P0_Rx_Pri_Map */
397 pri_map = 0x76543210;
400 writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
401 writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
404 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common,
405 netdev_features_t features)
407 struct am65_cpsw_host *host_p = am65_common_get_host(common);
408 int port_idx, i, ret;
412 if (common->usage_count)
415 /* Control register */
416 writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
417 AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
418 common->cpsw_base + AM65_CPSW_REG_CTL);
419 /* Max length register */
420 writel(AM65_CPSW_MAX_PACKET_SIZE,
421 host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
422 /* set base flow_id */
423 writel(common->rx_flow_id_base,
424 host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
425 /* en tx crc offload */
426 if (features & NETIF_F_HW_CSUM)
427 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN,
428 host_p->port_base + AM65_CPSW_P0_REG_CTL);
430 am65_cpsw_nuss_set_p0_ptype(common);
432 /* enable statistic */
433 val = BIT(HOST_PORT_NUM);
434 for (port_idx = 0; port_idx < common->port_num; port_idx++) {
435 struct am65_cpsw_port *port = &common->ports[port_idx];
438 val |= BIT(port->port_id);
440 writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
442 /* disable priority elevation */
443 writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
445 cpsw_ale_start(common->ale);
447 /* limit to one RX flow only */
448 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
449 ALE_DEFAULT_THREAD_ID, 0);
450 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
451 ALE_DEFAULT_THREAD_ENABLE, 1);
452 if (AM65_CPSW_IS_CPSW2G(common))
453 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
454 ALE_PORT_NOLEARN, 1);
455 /* switch to vlan unaware mode */
456 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
457 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
458 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
460 /* default vlan cfg: create mask based on enabled ports */
461 port_mask = GENMASK(common->port_num, 0) &
462 ~common->disabled_ports_mask;
464 cpsw_ale_add_vlan(common->ale, 0, port_mask,
465 port_mask, port_mask,
466 port_mask & ~ALE_PORT_HOST);
468 for (i = 0; i < common->rx_chns.descs_num; i++) {
469 skb = __netdev_alloc_skb_ip_align(NULL,
470 AM65_CPSW_MAX_PACKET_SIZE,
473 dev_err(common->dev, "cannot allocate skb\n");
477 ret = am65_cpsw_nuss_rx_push(common, skb);
480 "cannot submit skb to channel rx, error %d\n",
485 kmemleak_not_leak(skb);
487 k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
489 for (i = 0; i < common->tx_ch_num; i++) {
490 ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn);
493 napi_enable(&common->tx_chns[i].napi_tx);
496 napi_enable(&common->napi_rx);
498 dev_dbg(common->dev, "cpsw_nuss started\n");
502 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma);
503 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma);
505 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
509 if (common->usage_count != 1)
512 cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
513 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
515 /* shutdown tx channels */
516 atomic_set(&common->tdown_cnt, common->tx_ch_num);
517 /* ensure new tdown_cnt value is visible */
518 smp_mb__after_atomic();
519 reinit_completion(&common->tdown_complete);
521 for (i = 0; i < common->tx_ch_num; i++)
522 k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
524 i = wait_for_completion_timeout(&common->tdown_complete,
525 msecs_to_jiffies(1000));
527 dev_err(common->dev, "tx timeout\n");
528 for (i = 0; i < common->tx_ch_num; i++)
529 napi_disable(&common->tx_chns[i].napi_tx);
531 for (i = 0; i < common->tx_ch_num; i++) {
532 k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
534 am65_cpsw_nuss_tx_cleanup);
535 k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
538 k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
539 napi_disable(&common->napi_rx);
541 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
542 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
544 am65_cpsw_nuss_rx_cleanup, !!i);
546 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
548 cpsw_ale_stop(common->ale);
550 writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
551 writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
553 dev_dbg(common->dev, "cpsw_nuss stopped\n");
557 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
559 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
560 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
564 phy_stop(port->slave.phy);
566 netif_tx_stop_all_queues(ndev);
568 if (port->slave.phy) {
569 phy_disconnect(port->slave.phy);
570 port->slave.phy = NULL;
573 ret = am65_cpsw_nuss_common_stop(common);
577 common->usage_count--;
578 pm_runtime_put(common->dev);
582 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
584 struct am65_cpsw_port *port = arg;
589 return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
592 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
594 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
595 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
599 ret = pm_runtime_get_sync(common->dev);
601 pm_runtime_put_noidle(common->dev);
605 /* Notify the stack of the actual queue counts. */
606 ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
608 dev_err(common->dev, "cannot set real number of tx queues\n");
612 ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
614 dev_err(common->dev, "cannot set real number of rx queues\n");
618 for (i = 0; i < common->tx_ch_num; i++)
619 netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i));
621 ret = am65_cpsw_nuss_common_open(common, ndev->features);
625 common->usage_count++;
627 am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
629 if (port->slave.mac_only)
630 /* enable mac-only mode on port */
631 cpsw_ale_control_set(common->ale, port->port_id,
632 ALE_PORT_MACONLY, 1);
633 if (AM65_CPSW_IS_CPSW2G(common))
634 cpsw_ale_control_set(common->ale, port->port_id,
635 ALE_PORT_NOLEARN, 1);
637 port_mask = BIT(port->port_id) | ALE_PORT_HOST;
638 cpsw_ale_add_ucast(common->ale, ndev->dev_addr,
639 HOST_PORT_NUM, ALE_SECURE, 0);
640 cpsw_ale_add_mcast(common->ale, ndev->broadcast,
641 port_mask, 0, 0, ALE_MCAST_FWD_2);
643 /* mac_sl should be configured via phy-link interface */
644 am65_cpsw_sl_ctl_reset(port);
646 ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET,
651 if (port->slave.phy_node) {
652 port->slave.phy = of_phy_connect(ndev,
653 port->slave.phy_node,
654 &am65_cpsw_nuss_adjust_link,
655 0, port->slave.phy_if);
656 if (!port->slave.phy) {
657 dev_err(common->dev, "phy %pOF not found on slave %d\n",
658 port->slave.phy_node,
665 /* restore vlan configurations */
666 vlan_for_each(ndev, cpsw_restore_vlans, port);
668 phy_attached_info(port->slave.phy);
669 phy_start(port->slave.phy);
674 am65_cpsw_nuss_ndo_slave_stop(ndev);
678 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
680 struct am65_cpsw_rx_chn *rx_chn = data;
681 struct cppi5_host_desc_t *desc_rx;
687 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
688 swdata = cppi5_hdesc_get_swdata(desc_rx);
690 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
692 dma_unmap_single(rx_chn->dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
693 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
695 dev_kfree_skb_any(skb);
698 static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata)
700 struct skb_shared_hwtstamps *ssh;
703 ns = ((u64)psdata[1] << 32) | psdata[0];
705 ssh = skb_hwtstamps(skb);
706 memset(ssh, 0, sizeof(*ssh));
707 ssh->hwtstamp = ns_to_ktime(ns);
710 /* RX psdata[2] word format - checksum information */
711 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0)
712 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16)
713 #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17)
714 #define AM65_CPSW_RX_PSD_IS_TCP BIT(18)
715 #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19)
716 #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20)
718 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
720 /* HW can verify IPv4/IPv6 TCP/UDP packets checksum
721 * csum information provides in psdata[2] word:
722 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
723 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
724 * bits - indicates IPv4/IPv6 packet
725 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
726 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
727 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
729 skb_checksum_none_assert(skb);
731 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
734 if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
735 AM65_CPSW_RX_PSD_IPV4_VALID)) &&
736 !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
737 /* csum for fragmented packets is unsupported */
738 if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
739 skb->ip_summed = CHECKSUM_UNNECESSARY;
743 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
746 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
747 u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
748 struct am65_cpsw_ndev_priv *ndev_priv;
749 struct am65_cpsw_ndev_stats *stats;
750 struct cppi5_host_desc_t *desc_rx;
751 struct device *dev = common->dev;
752 struct sk_buff *skb, *new_skb;
753 dma_addr_t desc_dma, buf_dma;
754 struct am65_cpsw_port *port;
755 struct net_device *ndev;
760 ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
763 dev_err(dev, "RX: pop chn fail %d\n", ret);
767 if (desc_dma & 0x1) {
768 dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
772 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
773 dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
774 __func__, flow_idx, &desc_dma);
776 swdata = cppi5_hdesc_get_swdata(desc_rx);
778 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
779 pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
780 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
781 dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
782 port = am65_common_get_port(common, port_id);
786 psdata = cppi5_hdesc_get_psdata(desc_rx);
787 /* add RX timestamp */
788 if (port->rx_ts_enabled)
789 am65_cpsw_nuss_rx_ts(skb, psdata);
790 csum_info = psdata[2];
791 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
793 dma_unmap_single(dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
795 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
797 new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
799 skb_put(skb, pkt_len);
800 skb->protocol = eth_type_trans(skb, ndev);
801 am65_cpsw_nuss_rx_csum(skb, csum_info);
802 napi_gro_receive(&common->napi_rx, skb);
804 ndev_priv = netdev_priv(ndev);
805 stats = this_cpu_ptr(ndev_priv->stats);
807 u64_stats_update_begin(&stats->syncp);
809 stats->rx_bytes += pkt_len;
810 u64_stats_update_end(&stats->syncp);
811 kmemleak_not_leak(new_skb);
813 ndev->stats.rx_dropped++;
817 if (netif_dormant(ndev)) {
818 dev_kfree_skb_any(new_skb);
819 ndev->stats.rx_dropped++;
823 ret = am65_cpsw_nuss_rx_push(common, new_skb);
824 if (WARN_ON(ret < 0)) {
825 dev_kfree_skb_any(new_skb);
826 ndev->stats.rx_errors++;
827 ndev->stats.rx_dropped++;
833 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
835 struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
836 int flow = AM65_CPSW_MAX_RX_FLOWS;
840 /* process every flow */
842 cur_budget = budget - num_rx;
844 while (cur_budget--) {
845 ret = am65_cpsw_nuss_rx_packets(common, flow);
851 if (num_rx >= budget)
855 dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
857 if (num_rx < budget && napi_complete_done(napi_rx, num_rx))
858 enable_irq(common->rx_chns.irq);
863 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
865 struct cppi5_host_desc_t *desc)
867 struct cppi5_host_desc_t *first_desc, *next_desc;
868 dma_addr_t buf_dma, next_desc_dma;
872 next_desc = first_desc;
874 cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
876 dma_unmap_single(dev, buf_dma, buf_dma_len,
879 next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
880 while (next_desc_dma) {
881 next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
883 cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
885 dma_unmap_page(dev, buf_dma, buf_dma_len,
888 next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
890 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
893 k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
896 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
898 struct am65_cpsw_tx_chn *tx_chn = data;
899 struct cppi5_host_desc_t *desc_tx;
903 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
904 swdata = cppi5_hdesc_get_swdata(desc_tx);
906 am65_cpsw_nuss_xmit_free(tx_chn, tx_chn->common->dev, desc_tx);
908 dev_kfree_skb_any(skb);
911 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
912 int chn, unsigned int budget)
914 struct cppi5_host_desc_t *desc_tx;
915 struct device *dev = common->dev;
916 struct am65_cpsw_tx_chn *tx_chn;
917 struct netdev_queue *netif_txq;
918 unsigned int total_bytes = 0;
919 struct net_device *ndev;
925 tx_chn = &common->tx_chns[chn];
928 struct am65_cpsw_ndev_priv *ndev_priv;
929 struct am65_cpsw_ndev_stats *stats;
931 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
935 if (desc_dma & 0x1) {
936 if (atomic_dec_and_test(&common->tdown_cnt))
937 complete(&common->tdown_complete);
941 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
943 swdata = cppi5_hdesc_get_swdata(desc_tx);
945 am65_cpsw_nuss_xmit_free(tx_chn, dev, desc_tx);
949 am65_cpts_tx_timestamp(common->cpts, skb);
951 ndev_priv = netdev_priv(ndev);
952 stats = this_cpu_ptr(ndev_priv->stats);
953 u64_stats_update_begin(&stats->syncp);
955 stats->tx_bytes += skb->len;
956 u64_stats_update_end(&stats->syncp);
958 total_bytes += skb->len;
959 napi_consume_skb(skb, budget);
966 netif_txq = netdev_get_tx_queue(ndev, chn);
968 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
970 if (netif_tx_queue_stopped(netif_txq)) {
971 /* Check whether the queue is stopped due to stalled tx dma,
972 * if the queue is stopped then wake the queue as
973 * we have free desc for tx
975 __netif_tx_lock(netif_txq, smp_processor_id());
976 if (netif_running(ndev) &&
977 (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
979 netif_tx_wake_queue(netif_txq);
981 __netif_tx_unlock(netif_txq);
983 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
988 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
990 struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
993 num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id,
995 num_tx = min(num_tx, budget);
996 if (num_tx < budget) {
997 napi_complete(napi_tx);
998 enable_irq(tx_chn->irq);
1004 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
1006 struct am65_cpsw_common *common = dev_id;
1008 disable_irq_nosync(irq);
1009 napi_schedule(&common->napi_rx);
1014 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
1016 struct am65_cpsw_tx_chn *tx_chn = dev_id;
1018 disable_irq_nosync(irq);
1019 napi_schedule(&tx_chn->napi_tx);
1024 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
1025 struct net_device *ndev)
1027 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1028 struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
1029 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1030 struct device *dev = common->dev;
1031 struct am65_cpsw_tx_chn *tx_chn;
1032 struct netdev_queue *netif_txq;
1033 dma_addr_t desc_dma, buf_dma;
1039 /* padding enabled in hw */
1040 pkt_len = skb_headlen(skb);
1042 /* SKB TX timestamp */
1043 if (port->tx_ts_enabled)
1044 am65_cpts_prep_tx_timestamp(common->cpts, skb);
1046 q_idx = skb_get_queue_mapping(skb);
1047 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1049 tx_chn = &common->tx_chns[q_idx];
1050 netif_txq = netdev_get_tx_queue(ndev, q_idx);
1052 /* Map the linear buffer */
1053 buf_dma = dma_map_single(dev, skb->data, pkt_len,
1055 if (unlikely(dma_mapping_error(dev, buf_dma))) {
1056 dev_err(dev, "Failed to map tx skb buffer\n");
1057 ndev->stats.tx_errors++;
1061 first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1063 dev_dbg(dev, "Failed to allocate descriptor\n");
1064 dma_unmap_single(dev, buf_dma, pkt_len, DMA_TO_DEVICE);
1068 cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1069 AM65_CPSW_NAV_PS_DATA_SIZE);
1070 cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
1071 cppi5_hdesc_set_pkttype(first_desc, 0x7);
1072 cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1074 cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1075 swdata = cppi5_hdesc_get_swdata(first_desc);
1077 psdata = cppi5_hdesc_get_psdata(first_desc);
1079 /* HW csum offload if enabled */
1081 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1082 unsigned int cs_start, cs_offset;
1084 cs_start = skb_transport_offset(skb);
1085 cs_offset = cs_start + skb->csum_offset;
1086 /* HW numerates bytes starting from 1 */
1087 psdata[2] = ((cs_offset + 1) << 24) |
1088 ((cs_start + 1) << 16) | (skb->len - cs_start);
1089 dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1092 if (!skb_is_nonlinear(skb))
1095 dev_dbg(dev, "fragmented SKB\n");
1097 /* Handle the case where skb is fragmented in pages */
1098 cur_desc = first_desc;
1099 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1100 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1101 u32 frag_size = skb_frag_size(frag);
1103 next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1105 dev_err(dev, "Failed to allocate descriptor\n");
1106 goto busy_free_descs;
1109 buf_dma = skb_frag_dma_map(dev, frag, 0, frag_size,
1111 if (unlikely(dma_mapping_error(dev, buf_dma))) {
1112 dev_err(dev, "Failed to map tx skb page\n");
1113 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1114 ndev->stats.tx_errors++;
1115 goto err_free_descs;
1118 cppi5_hdesc_reset_hbdesc(next_desc);
1119 cppi5_hdesc_attach_buf(next_desc,
1120 buf_dma, frag_size, buf_dma, frag_size);
1122 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1124 cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1126 pkt_len += frag_size;
1127 cur_desc = next_desc;
1129 WARN_ON(pkt_len != skb->len);
1132 skb_tx_timestamp(skb);
1134 /* report bql before sending packet */
1135 netdev_tx_sent_queue(netif_txq, pkt_len);
1137 cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1138 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1139 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1141 dev_err(dev, "can't push desc %d\n", ret);
1143 netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1144 ndev->stats.tx_errors++;
1145 goto err_free_descs;
1148 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1149 netif_tx_stop_queue(netif_txq);
1150 /* Barrier, so that stop_queue visible to other cpus */
1151 smp_mb__after_atomic();
1152 dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1154 /* re-check for smp */
1155 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1157 netif_tx_wake_queue(netif_txq);
1158 dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1162 return NETDEV_TX_OK;
1165 am65_cpsw_nuss_xmit_free(tx_chn, dev, first_desc);
1167 ndev->stats.tx_dropped++;
1168 dev_kfree_skb_any(skb);
1169 return NETDEV_TX_OK;
1172 am65_cpsw_nuss_xmit_free(tx_chn, dev, first_desc);
1174 netif_tx_stop_queue(netif_txq);
1175 return NETDEV_TX_BUSY;
1178 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1181 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1182 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1183 struct sockaddr *sockaddr = (struct sockaddr *)addr;
1186 ret = eth_prepare_mac_addr_change(ndev, addr);
1190 ret = pm_runtime_get_sync(common->dev);
1192 pm_runtime_put_noidle(common->dev);
1196 cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1197 HOST_PORT_NUM, 0, 0);
1198 cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1199 HOST_PORT_NUM, ALE_SECURE, 0);
1201 am65_cpsw_port_set_sl_mac(port, addr);
1202 eth_commit_mac_addr_change(ndev, sockaddr);
1204 pm_runtime_put(common->dev);
1209 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
1212 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1213 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1214 u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
1215 struct hwtstamp_config cfg;
1217 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1220 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1223 /* TX HW timestamp */
1224 switch (cfg.tx_type) {
1225 case HWTSTAMP_TX_OFF:
1226 case HWTSTAMP_TX_ON:
1232 switch (cfg.rx_filter) {
1233 case HWTSTAMP_FILTER_NONE:
1234 port->rx_ts_enabled = false;
1236 case HWTSTAMP_FILTER_ALL:
1237 case HWTSTAMP_FILTER_SOME:
1238 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1239 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1240 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1241 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1242 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1243 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1244 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1245 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1246 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1247 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1248 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1249 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1250 case HWTSTAMP_FILTER_NTP_ALL:
1251 port->rx_ts_enabled = true;
1252 cfg.rx_filter = HWTSTAMP_FILTER_ALL;
1258 port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
1260 /* cfg TX timestamp */
1261 seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
1262 AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
1264 ts_vlan_ltype = ETH_P_8021Q;
1266 ts_ctrl_ltype2 = ETH_P_1588 |
1267 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
1268 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
1269 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
1270 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
1271 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
1272 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
1273 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
1274 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
1276 ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
1277 AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
1279 if (port->tx_ts_enabled)
1280 ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
1281 AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
1283 writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
1284 writel(ts_vlan_ltype, port->port_base +
1285 AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
1286 writel(ts_ctrl_ltype2, port->port_base +
1287 AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
1288 writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
1290 /* en/dis RX timestamp */
1291 am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled);
1293 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1296 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
1299 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1300 struct hwtstamp_config cfg;
1302 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1306 cfg.tx_type = port->tx_ts_enabled ?
1307 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1308 cfg.rx_filter = port->rx_ts_enabled ?
1309 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
1311 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1314 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1315 struct ifreq *req, int cmd)
1317 struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1319 if (!netif_running(ndev))
1324 return am65_cpsw_nuss_hwtstamp_set(ndev, req);
1326 return am65_cpsw_nuss_hwtstamp_get(ndev, req);
1329 if (!port->slave.phy)
1332 return phy_mii_ioctl(port->slave.phy, req, cmd);
1335 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1336 struct rtnl_link_stats64 *stats)
1338 struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1342 for_each_possible_cpu(cpu) {
1343 struct am65_cpsw_ndev_stats *cpu_stats;
1349 cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1351 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1352 rx_packets = cpu_stats->rx_packets;
1353 rx_bytes = cpu_stats->rx_bytes;
1354 tx_packets = cpu_stats->tx_packets;
1355 tx_bytes = cpu_stats->tx_bytes;
1356 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1358 stats->rx_packets += rx_packets;
1359 stats->rx_bytes += rx_bytes;
1360 stats->tx_packets += tx_packets;
1361 stats->tx_bytes += tx_bytes;
1364 stats->rx_errors = dev->stats.rx_errors;
1365 stats->rx_dropped = dev->stats.rx_dropped;
1366 stats->tx_dropped = dev->stats.tx_dropped;
1369 static int am65_cpsw_nuss_ndo_slave_set_features(struct net_device *ndev,
1370 netdev_features_t features)
1372 struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1373 netdev_features_t changes = features ^ ndev->features;
1374 struct am65_cpsw_host *host_p;
1376 host_p = am65_common_get_host(common);
1378 if (changes & NETIF_F_HW_CSUM) {
1379 bool enable = !!(features & NETIF_F_HW_CSUM);
1381 dev_info(common->dev, "Turn %s tx-checksum-ip-generic\n",
1382 enable ? "ON" : "OFF");
1384 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN,
1385 host_p->port_base + AM65_CPSW_P0_REG_CTL);
1388 host_p->port_base + AM65_CPSW_P0_REG_CTL);
1394 static const struct net_device_ops am65_cpsw_nuss_netdev_ops_2g = {
1395 .ndo_open = am65_cpsw_nuss_ndo_slave_open,
1396 .ndo_stop = am65_cpsw_nuss_ndo_slave_stop,
1397 .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit,
1398 .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode,
1399 .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats,
1400 .ndo_validate_addr = eth_validate_addr,
1401 .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address,
1402 .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout,
1403 .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid,
1404 .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid,
1405 .ndo_do_ioctl = am65_cpsw_nuss_ndo_slave_ioctl,
1406 .ndo_set_features = am65_cpsw_nuss_ndo_slave_set_features,
1407 .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc,
1410 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
1412 struct am65_cpsw_common *common = port->common;
1414 if (!port->disabled)
1417 common->disabled_ports_mask |= BIT(port->port_id);
1418 cpsw_ale_control_set(common->ale, port->port_id,
1419 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1421 cpsw_sl_reset(port->slave.mac_sl, 100);
1422 cpsw_sl_ctl_reset(port->slave.mac_sl);
1425 static void am65_cpsw_nuss_free_tx_chns(void *data)
1427 struct am65_cpsw_common *common = data;
1430 for (i = 0; i < common->tx_ch_num; i++) {
1431 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1433 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1434 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1436 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1437 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1439 memset(tx_chn, 0, sizeof(*tx_chn));
1443 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
1445 struct device *dev = common->dev;
1448 devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1450 for (i = 0; i < common->tx_ch_num; i++) {
1451 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1454 devm_free_irq(dev, tx_chn->irq, tx_chn);
1456 netif_napi_del(&tx_chn->napi_tx);
1458 if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1459 k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1461 if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1462 k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1464 memset(tx_chn, 0, sizeof(*tx_chn));
1468 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
1470 u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
1471 struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
1472 struct device *dev = common->dev;
1473 struct k3_ring_cfg ring_cfg = {
1474 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1475 .mode = K3_RINGACC_RING_MODE_RING,
1481 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1482 AM65_CPSW_NAV_SW_DATA_SIZE);
1484 tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1485 tx_cfg.tx_cfg = ring_cfg;
1486 tx_cfg.txcq_cfg = ring_cfg;
1487 tx_cfg.tx_cfg.size = max_desc_num;
1488 tx_cfg.txcq_cfg.size = max_desc_num;
1490 for (i = 0; i < common->tx_ch_num; i++) {
1491 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1493 snprintf(tx_chn->tx_chn_name,
1494 sizeof(tx_chn->tx_chn_name), "tx%d", i);
1496 tx_chn->common = common;
1498 tx_chn->descs_num = max_desc_num;
1500 k3_cppi_desc_pool_create_name(dev,
1503 tx_chn->tx_chn_name);
1504 if (IS_ERR(tx_chn->desc_pool)) {
1505 ret = PTR_ERR(tx_chn->desc_pool);
1506 dev_err(dev, "Failed to create poll %d\n", ret);
1511 k3_udma_glue_request_tx_chn(dev,
1512 tx_chn->tx_chn_name,
1514 if (IS_ERR(tx_chn->tx_chn)) {
1515 ret = PTR_ERR(tx_chn->tx_chn);
1516 dev_err(dev, "Failed to request tx dma channel %d\n",
1521 tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
1522 if (tx_chn->irq <= 0) {
1523 dev_err(dev, "Failed to get tx dma irq %d\n",
1528 snprintf(tx_chn->tx_chn_name,
1529 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
1530 dev_name(dev), tx_chn->id);
1534 i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1536 dev_err(dev, "Failed to add free_tx_chns action %d\n", i);
1543 static void am65_cpsw_nuss_free_rx_chns(void *data)
1545 struct am65_cpsw_common *common = data;
1546 struct am65_cpsw_rx_chn *rx_chn;
1548 rx_chn = &common->rx_chns;
1550 if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1551 k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1553 if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1554 k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1557 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
1559 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
1560 struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
1561 u32 max_desc_num = AM65_CPSW_MAX_RX_DESC;
1562 struct device *dev = common->dev;
1567 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1568 AM65_CPSW_NAV_SW_DATA_SIZE);
1570 rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1571 rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
1572 rx_cfg.flow_id_base = common->rx_flow_id_base;
1574 /* init all flows */
1576 rx_chn->descs_num = max_desc_num;
1577 rx_chn->desc_pool = k3_cppi_desc_pool_create_name(dev,
1580 if (IS_ERR(rx_chn->desc_pool)) {
1581 ret = PTR_ERR(rx_chn->desc_pool);
1582 dev_err(dev, "Failed to create rx poll %d\n", ret);
1586 rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
1587 if (IS_ERR(rx_chn->rx_chn)) {
1588 ret = PTR_ERR(rx_chn->rx_chn);
1589 dev_err(dev, "Failed to request rx dma channel %d\n", ret);
1593 common->rx_flow_id_base =
1594 k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
1595 dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
1597 fdqring_id = K3_RINGACC_RING_ID_ANY;
1598 for (i = 0; i < rx_cfg.flow_id_num; i++) {
1599 struct k3_ring_cfg rxring_cfg = {
1600 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1601 .mode = K3_RINGACC_RING_MODE_RING,
1604 struct k3_ring_cfg fdqring_cfg = {
1605 .elm_size = K3_RINGACC_RING_ELSIZE_8,
1606 .mode = K3_RINGACC_RING_MODE_MESSAGE,
1607 .flags = K3_RINGACC_RING_SHARED,
1609 struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
1610 .rx_cfg = rxring_cfg,
1611 .rxfdq_cfg = fdqring_cfg,
1612 .ring_rxq_id = K3_RINGACC_RING_ID_ANY,
1614 K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
1617 rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
1618 rx_flow_cfg.rx_cfg.size = max_desc_num;
1619 rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
1621 ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
1624 dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
1629 k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
1632 rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
1634 if (rx_chn->irq <= 0) {
1635 dev_err(dev, "Failed to get rx dma irq %d\n",
1643 i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1645 dev_err(dev, "Failed to add free_rx_chns action %d\n", i);
1652 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
1654 struct am65_cpsw_host *host_p = am65_common_get_host(common);
1656 host_p->common = common;
1657 host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
1658 host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
1663 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
1664 int slave, u8 *mac_addr)
1666 u32 mac_lo, mac_hi, offset;
1667 struct regmap *syscon;
1670 syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
1671 if (IS_ERR(syscon)) {
1672 if (PTR_ERR(syscon) == -ENODEV)
1674 return PTR_ERR(syscon);
1677 ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
1682 regmap_read(syscon, offset, &mac_lo);
1683 regmap_read(syscon, offset + 4, &mac_hi);
1685 mac_addr[0] = (mac_hi >> 8) & 0xff;
1686 mac_addr[1] = mac_hi & 0xff;
1687 mac_addr[2] = (mac_lo >> 24) & 0xff;
1688 mac_addr[3] = (mac_lo >> 16) & 0xff;
1689 mac_addr[4] = (mac_lo >> 8) & 0xff;
1690 mac_addr[5] = mac_lo & 0xff;
1695 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
1697 struct device *dev = common->dev;
1698 struct device_node *node;
1699 struct am65_cpts *cpts;
1700 void __iomem *reg_base;
1702 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1705 node = of_get_child_by_name(dev->of_node, "cpts");
1707 dev_err(dev, "%s cpts not found\n", __func__);
1711 reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
1712 cpts = am65_cpts_create(dev, reg_base, node);
1714 int ret = PTR_ERR(cpts);
1716 if (ret == -EOPNOTSUPP) {
1717 dev_info(dev, "cpts disabled\n");
1721 dev_err(dev, "cpts create err %d\n", ret);
1724 common->cpts = cpts;
1729 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
1731 struct device_node *node, *port_np;
1732 struct device *dev = common->dev;
1735 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
1739 for_each_child_of_node(node, port_np) {
1740 struct am65_cpsw_port *port;
1741 const void *mac_addr;
1744 /* it is not a slave port node, continue */
1745 if (strcmp(port_np->name, "port"))
1748 ret = of_property_read_u32(port_np, "reg", &port_id);
1750 dev_err(dev, "%pOF error reading port_id %d\n",
1755 if (!port_id || port_id > common->port_num) {
1756 dev_err(dev, "%pOF has invalid port_id %u %s\n",
1757 port_np, port_id, port_np->name);
1761 port = am65_common_get_port(common, port_id);
1762 port->port_id = port_id;
1763 port->common = common;
1764 port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
1765 AM65_CPSW_NU_PORTS_OFFSET * (port_id);
1766 port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
1767 (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
1768 port->name = of_get_property(port_np, "label", NULL);
1769 port->fetch_ram_base =
1770 common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
1771 (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
1773 port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
1774 if (IS_ERR(port->slave.mac_sl))
1775 return PTR_ERR(port->slave.mac_sl);
1777 port->disabled = !of_device_is_available(port_np);
1781 port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
1782 if (IS_ERR(port->slave.ifphy)) {
1783 ret = PTR_ERR(port->slave.ifphy);
1784 dev_err(dev, "%pOF error retrieving port phy: %d\n",
1789 port->slave.mac_only =
1790 of_property_read_bool(port_np, "ti,mac-only");
1792 /* get phy/link info */
1793 if (of_phy_is_fixed_link(port_np)) {
1794 ret = of_phy_register_fixed_link(port_np);
1796 if (ret != -EPROBE_DEFER)
1797 dev_err(dev, "%pOF failed to register fixed-link phy: %d\n",
1801 port->slave.phy_node = of_node_get(port_np);
1803 port->slave.phy_node =
1804 of_parse_phandle(port_np, "phy-handle", 0);
1807 if (!port->slave.phy_node) {
1809 "slave[%d] no phy found\n", port_id);
1813 ret = of_get_phy_mode(port_np, &port->slave.phy_if);
1815 dev_err(dev, "%pOF read phy-mode err %d\n",
1820 mac_addr = of_get_mac_address(port_np);
1821 if (!IS_ERR(mac_addr)) {
1822 ether_addr_copy(port->slave.mac_addr, mac_addr);
1823 } else if (am65_cpsw_am654_get_efuse_macid(port_np,
1825 port->slave.mac_addr) ||
1826 !is_valid_ether_addr(port->slave.mac_addr)) {
1827 random_ether_addr(port->slave.mac_addr);
1828 dev_err(dev, "Use random MAC address\n");
1836 static void am65_cpsw_pcpu_stats_free(void *data)
1838 struct am65_cpsw_ndev_stats __percpu *stats = data;
1843 static int am65_cpsw_nuss_init_ndev_2g(struct am65_cpsw_common *common)
1845 struct am65_cpsw_ndev_priv *ndev_priv;
1846 struct device *dev = common->dev;
1847 struct am65_cpsw_port *port;
1850 port = am65_common_get_port(common, 1);
1853 port->ndev = devm_alloc_etherdev_mqs(common->dev,
1854 sizeof(struct am65_cpsw_ndev_priv),
1855 AM65_CPSW_MAX_TX_QUEUES,
1856 AM65_CPSW_MAX_RX_QUEUES);
1858 dev_err(dev, "error allocating slave net_device %u\n",
1863 ndev_priv = netdev_priv(port->ndev);
1864 ndev_priv->port = port;
1865 ndev_priv->msg_enable = AM65_CPSW_DEBUG;
1866 SET_NETDEV_DEV(port->ndev, dev);
1868 ether_addr_copy(port->ndev->dev_addr, port->slave.mac_addr);
1870 port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
1871 port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE;
1872 port->ndev->hw_features = NETIF_F_SG |
1875 port->ndev->features = port->ndev->hw_features |
1876 NETIF_F_HW_VLAN_CTAG_FILTER;
1877 port->ndev->vlan_features |= NETIF_F_SG;
1878 port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops_2g;
1879 port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
1881 /* Disable TX checksum offload by default due to HW bug */
1882 if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
1883 port->ndev->features &= ~NETIF_F_HW_CSUM;
1885 ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
1886 if (!ndev_priv->stats)
1889 ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
1892 dev_err(dev, "Failed to add percpu stat free action %d\n", ret);
1896 netif_napi_add(port->ndev, &common->napi_rx,
1897 am65_cpsw_nuss_rx_poll, NAPI_POLL_WEIGHT);
1902 static int am65_cpsw_nuss_ndev_add_napi_2g(struct am65_cpsw_common *common)
1904 struct device *dev = common->dev;
1905 struct am65_cpsw_port *port;
1908 port = am65_common_get_port(common, 1);
1910 for (i = 0; i < common->tx_ch_num; i++) {
1911 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1913 netif_tx_napi_add(port->ndev, &tx_chn->napi_tx,
1914 am65_cpsw_nuss_tx_poll, NAPI_POLL_WEIGHT);
1916 ret = devm_request_irq(dev, tx_chn->irq,
1917 am65_cpsw_nuss_tx_irq,
1919 tx_chn->tx_chn_name, tx_chn);
1921 dev_err(dev, "failure requesting tx%u irq %u, %d\n",
1922 tx_chn->id, tx_chn->irq, ret);
1931 static int am65_cpsw_nuss_ndev_reg_2g(struct am65_cpsw_common *common)
1933 struct device *dev = common->dev;
1934 struct am65_cpsw_port *port;
1937 port = am65_common_get_port(common, 1);
1938 ret = am65_cpsw_nuss_ndev_add_napi_2g(common);
1942 ret = devm_request_irq(dev, common->rx_chns.irq,
1943 am65_cpsw_nuss_rx_irq,
1944 IRQF_TRIGGER_HIGH, dev_name(dev), common);
1946 dev_err(dev, "failure requesting rx irq %u, %d\n",
1947 common->rx_chns.irq, ret);
1951 ret = register_netdev(port->ndev);
1953 dev_err(dev, "error registering slave net device %d\n", ret);
1955 /* can't auto unregister ndev using devm_add_action() due to
1956 * devres release sequence in DD core for DMA
1962 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
1966 common->tx_ch_num = num_tx;
1967 ret = am65_cpsw_nuss_init_tx_chns(common);
1971 return am65_cpsw_nuss_ndev_add_napi_2g(common);
1974 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
1976 struct am65_cpsw_port *port;
1979 for (i = 0; i < common->port_num; i++) {
1980 port = &common->ports[i];
1982 unregister_netdev(port->ndev);
1986 struct am65_cpsw_soc_pdata {
1990 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
1991 .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
1994 static const struct soc_device_attribute am65_cpsw_socinfo[] = {
1995 { .family = "AM65X",
1996 .revision = "SR2.0",
1997 .data = &am65x_soc_sr2_0
2002 static const struct am65_cpsw_pdata am65x_sr1_0 = {
2003 .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2006 static const struct am65_cpsw_pdata j721e_pdata = {
2010 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
2011 { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
2012 { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
2015 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
2017 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
2019 const struct soc_device_attribute *soc;
2021 soc = soc_device_match(am65_cpsw_socinfo);
2022 if (soc && soc->data) {
2023 const struct am65_cpsw_soc_pdata *socdata = soc->data;
2025 /* disable quirks */
2026 common->pdata.quirks &= ~socdata->quirks_dis;
2030 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
2032 struct cpsw_ale_params ale_params = { 0 };
2033 const struct of_device_id *of_id;
2034 struct device *dev = &pdev->dev;
2035 struct am65_cpsw_common *common;
2036 struct device_node *node;
2037 struct resource *res;
2040 common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
2045 of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
2048 common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
2050 am65_cpsw_nuss_apply_socinfo(common);
2052 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
2053 common->ss_base = devm_ioremap_resource(&pdev->dev, res);
2054 if (IS_ERR(common->ss_base))
2055 return PTR_ERR(common->ss_base);
2056 common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
2058 node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2061 common->port_num = of_get_child_count(node);
2062 if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
2066 if (common->port_num != 1)
2069 common->rx_flow_id_base = -1;
2070 init_completion(&common->tdown_complete);
2071 common->tx_ch_num = 1;
2072 common->pf_p0_rx_ptype_rrobin = false;
2074 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(48));
2076 dev_err(dev, "error setting dma mask: %d\n", ret);
2080 common->ports = devm_kcalloc(dev, common->port_num,
2081 sizeof(*common->ports),
2086 pm_runtime_enable(dev);
2087 ret = pm_runtime_get_sync(dev);
2089 pm_runtime_put_noidle(dev);
2090 pm_runtime_disable(dev);
2094 node = of_get_child_by_name(dev->of_node, "mdio");
2096 dev_warn(dev, "MDIO node not found\n");
2097 } else if (of_device_is_available(node)) {
2098 struct platform_device *mdio_pdev;
2100 mdio_pdev = of_platform_device_create(node, NULL, dev);
2106 common->mdio_dev = &mdio_pdev->dev;
2110 am65_cpsw_nuss_get_ver(common);
2112 /* init tx channels */
2113 ret = am65_cpsw_nuss_init_tx_chns(common);
2116 ret = am65_cpsw_nuss_init_rx_chns(common);
2120 ret = am65_cpsw_nuss_init_host_p(common);
2124 ret = am65_cpsw_nuss_init_slave_ports(common);
2128 /* init common data */
2129 ale_params.dev = dev;
2130 ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
2131 ale_params.ale_entries = 0;
2132 ale_params.ale_ports = common->port_num + 1;
2133 ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
2134 ale_params.nu_switch_ale = true;
2136 common->ale = cpsw_ale_create(&ale_params);
2137 if (IS_ERR(common->ale)) {
2138 dev_err(dev, "error initializing ale engine\n");
2139 ret = PTR_ERR(common->ale);
2143 ret = am65_cpsw_init_cpts(common);
2148 for (i = 0; i < common->port_num; i++)
2149 am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
2151 dev_set_drvdata(dev, common);
2153 ret = am65_cpsw_nuss_init_ndev_2g(common);
2157 ret = am65_cpsw_nuss_ndev_reg_2g(common);
2161 pm_runtime_put(dev);
2165 of_platform_device_destroy(common->mdio_dev, NULL);
2167 pm_runtime_put_sync(dev);
2168 pm_runtime_disable(dev);
2172 static int am65_cpsw_nuss_remove(struct platform_device *pdev)
2174 struct device *dev = &pdev->dev;
2175 struct am65_cpsw_common *common;
2178 common = dev_get_drvdata(dev);
2180 ret = pm_runtime_get_sync(&pdev->dev);
2182 pm_runtime_put_noidle(&pdev->dev);
2186 /* must unregister ndevs here because DD release_driver routine calls
2187 * dma_deconfigure(dev) before devres_release_all(dev)
2189 am65_cpsw_nuss_cleanup_ndev(common);
2191 of_platform_device_destroy(common->mdio_dev, NULL);
2193 pm_runtime_put_sync(&pdev->dev);
2194 pm_runtime_disable(&pdev->dev);
2198 static struct platform_driver am65_cpsw_nuss_driver = {
2200 .name = AM65_CPSW_DRV_NAME,
2201 .of_match_table = am65_cpsw_nuss_of_mtable,
2203 .probe = am65_cpsw_nuss_probe,
2204 .remove = am65_cpsw_nuss_remove,
2207 module_platform_driver(am65_cpsw_nuss_driver);
2209 MODULE_LICENSE("GPL v2");
2210 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
2211 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");