193a76530SGrygorii Strashko // SPDX-License-Identifier: GPL-2.0 293a76530SGrygorii Strashko /* Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver 393a76530SGrygorii Strashko * 493a76530SGrygorii Strashko * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/ 593a76530SGrygorii Strashko * 693a76530SGrygorii Strashko */ 793a76530SGrygorii Strashko 823015ff1SGrygorii Strashko #include <linux/clk.h> 993a76530SGrygorii Strashko #include <linux/etherdevice.h> 1093a76530SGrygorii Strashko #include <linux/if_vlan.h> 1193a76530SGrygorii Strashko #include <linux/interrupt.h> 120b718007SSiddharth Vadapalli #include <linux/irqdomain.h> 1393a76530SGrygorii Strashko #include <linux/kernel.h> 1493a76530SGrygorii Strashko #include <linux/kmemleak.h> 1593a76530SGrygorii Strashko #include <linux/module.h> 1693a76530SGrygorii Strashko #include <linux/netdevice.h> 1793a76530SGrygorii Strashko #include <linux/net_tstamp.h> 1893a76530SGrygorii Strashko #include <linux/of.h> 1993a76530SGrygorii Strashko #include <linux/of_mdio.h> 2093a76530SGrygorii Strashko #include <linux/of_net.h> 2193a76530SGrygorii Strashko #include <linux/of_device.h> 22e8609e69SSiddharth Vadapalli #include <linux/phylink.h> 2393a76530SGrygorii Strashko #include <linux/phy/phy.h> 2493a76530SGrygorii Strashko #include <linux/platform_device.h> 2593a76530SGrygorii Strashko #include <linux/pm_runtime.h> 2693a76530SGrygorii Strashko #include <linux/regmap.h> 2793a76530SGrygorii Strashko #include <linux/mfd/syscon.h> 2838389aa6SGrygorii Strashko #include <linux/sys_soc.h> 2993a76530SGrygorii Strashko #include <linux/dma/ti-cppi5.h> 3093a76530SGrygorii Strashko #include <linux/dma/k3-udma-glue.h> 31957e2235SVladimir Oltean #include <net/switchdev.h> 3293a76530SGrygorii Strashko 3393a76530SGrygorii Strashko #include "cpsw_ale.h" 3493a76530SGrygorii Strashko #include "cpsw_sl.h" 3593a76530SGrygorii Strashko #include "am65-cpsw-nuss.h" 3686e8b070SVignesh Raghavendra #include "am65-cpsw-switchdev.h" 3793a76530SGrygorii Strashko #include "k3-cppi-desc-pool.h" 38b1f66a5bSGrygorii Strashko #include "am65-cpts.h" 3993a76530SGrygorii Strashko 4093a76530SGrygorii Strashko #define AM65_CPSW_SS_BASE 0x0 4193a76530SGrygorii Strashko #define AM65_CPSW_SGMII_BASE 0x100 4293a76530SGrygorii Strashko #define AM65_CPSW_XGMII_BASE 0x2100 4393a76530SGrygorii Strashko #define AM65_CPSW_CPSW_NU_BASE 0x20000 4493a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_BASE 0x1000 458127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_BASE 0x12000 4693a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_BASE 0x1a000 4793a76530SGrygorii Strashko #define AM65_CPSW_NU_ALE_BASE 0x1e000 4893a76530SGrygorii Strashko #define AM65_CPSW_NU_CPTS_BASE 0x1d000 4993a76530SGrygorii Strashko 5093a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_OFFSET 0x1000 5193a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200 528127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200 5393a76530SGrygorii Strashko 5493a76530SGrygorii Strashko #define AM65_CPSW_MAX_PORTS 8 5593a76530SGrygorii Strashko 5693a76530SGrygorii Strashko #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN 5793a76530SGrygorii Strashko #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 5893a76530SGrygorii Strashko 5993a76530SGrygorii Strashko #define AM65_CPSW_REG_CTL 0x004 6093a76530SGrygorii Strashko #define AM65_CPSW_REG_STAT_PORT_EN 0x014 6193a76530SGrygorii Strashko #define AM65_CPSW_REG_PTYPE 0x018 6293a76530SGrygorii Strashko 6393a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL 0x004 6493a76530SGrygorii Strashko #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008 6593a76530SGrygorii Strashko 6693a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c 6793a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020 6893a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024 6993a76530SGrygorii Strashko 7093a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_L 0x308 7193a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_H 0x30c 7293a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL 0x310 7393a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314 7493a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318 7593a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C 7693a76530SGrygorii Strashko 7737184fc1SSiddharth Vadapalli #define AM65_CPSW_SGMII_CONTROL_REG 0x010 7837184fc1SSiddharth Vadapalli #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0) 7937184fc1SSiddharth Vadapalli 8093a76530SGrygorii Strashko #define AM65_CPSW_CTL_VLAN_AWARE BIT(1) 8193a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_ENABLE BIT(2) 8293a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13) 8393a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_RX_PAD BIT(14) 8493a76530SGrygorii Strashko 8593a76530SGrygorii Strashko /* AM65_CPSW_P0_REG_CTL */ 8693a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0) 8793a76530SGrygorii Strashko 8893a76530SGrygorii Strashko /* AM65_CPSW_PORT_REG_PRI_CTL */ 8993a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8) 9093a76530SGrygorii Strashko 9193a76530SGrygorii Strashko /* AM65_CPSW_PN_TS_CTL register fields */ 9293a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4) 9393a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5) 9493a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6) 9593a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7) 9693a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10) 9793a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11) 9893a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16 9993a76530SGrygorii Strashko 10093a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */ 10193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16 10293a76530SGrygorii Strashko 10393a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */ 10493a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16) 10593a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17) 10693a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18) 10793a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19) 10893a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20) 10993a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21) 11093a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22) 11193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23) 11293a76530SGrygorii Strashko 11393a76530SGrygorii Strashko /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 11493a76530SGrygorii Strashko #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 11593a76530SGrygorii Strashko 11693a76530SGrygorii Strashko #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e) 11793a76530SGrygorii Strashko 11893a76530SGrygorii Strashko #define AM65_CPSW_TS_TX_ANX_ALL_EN \ 11993a76530SGrygorii Strashko (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \ 12093a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \ 12193a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN) 12293a76530SGrygorii Strashko 12393a76530SGrygorii Strashko #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30 12493a76530SGrygorii Strashko /* Number of TX/RX descriptors */ 12593a76530SGrygorii Strashko #define AM65_CPSW_MAX_TX_DESC 500 12693a76530SGrygorii Strashko #define AM65_CPSW_MAX_RX_DESC 500 12793a76530SGrygorii Strashko 12893a76530SGrygorii Strashko #define AM65_CPSW_NAV_PS_DATA_SIZE 16 12993a76530SGrygorii Strashko #define AM65_CPSW_NAV_SW_DATA_SIZE 16 13093a76530SGrygorii Strashko 13193a76530SGrygorii Strashko #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \ 13293a76530SGrygorii Strashko NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \ 13393a76530SGrygorii Strashko NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR) 13493a76530SGrygorii Strashko 13593a76530SGrygorii Strashko static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave, 13693a76530SGrygorii Strashko const u8 *dev_addr) 13793a76530SGrygorii Strashko { 13893a76530SGrygorii Strashko u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) | 13993a76530SGrygorii Strashko (dev_addr[2] << 16) | (dev_addr[3] << 24); 14093a76530SGrygorii Strashko u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8); 14193a76530SGrygorii Strashko 14293a76530SGrygorii Strashko writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H); 14393a76530SGrygorii Strashko writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L); 14493a76530SGrygorii Strashko } 14593a76530SGrygorii Strashko 14693a76530SGrygorii Strashko static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port) 14793a76530SGrygorii Strashko { 14893a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 14993a76530SGrygorii Strashko /* Max length register has to be restored after MAC SL reset */ 15093a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 15193a76530SGrygorii Strashko port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 15293a76530SGrygorii Strashko } 15393a76530SGrygorii Strashko 15493a76530SGrygorii Strashko static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common) 15593a76530SGrygorii Strashko { 15693a76530SGrygorii Strashko common->nuss_ver = readl(common->ss_base); 15793a76530SGrygorii Strashko common->cpsw_ver = readl(common->cpsw_base); 15893a76530SGrygorii Strashko dev_info(common->dev, 15938389aa6SGrygorii Strashko "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n", 16093a76530SGrygorii Strashko common->nuss_ver, 16193a76530SGrygorii Strashko common->cpsw_ver, 16238389aa6SGrygorii Strashko common->port_num + 1, 16338389aa6SGrygorii Strashko common->pdata.quirks); 16493a76530SGrygorii Strashko } 16593a76530SGrygorii Strashko 16693a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev, 16793a76530SGrygorii Strashko __be16 proto, u16 vid) 16893a76530SGrygorii Strashko { 16993a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 17093a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 17193a76530SGrygorii Strashko u32 port_mask, unreg_mcast = 0; 17293a76530SGrygorii Strashko int ret; 17393a76530SGrygorii Strashko 17486e8b070SVignesh Raghavendra if (!common->is_emac_mode) 17586e8b070SVignesh Raghavendra return 0; 17686e8b070SVignesh Raghavendra 1777bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 1787bcffde0SGrygorii Strashko return 0; 1797bcffde0SGrygorii Strashko 1802240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 1812240514cSMinghao Chi if (ret < 0) 18293a76530SGrygorii Strashko return ret; 18393a76530SGrygorii Strashko 18493a76530SGrygorii Strashko port_mask = BIT(port->port_id) | ALE_PORT_HOST; 18593a76530SGrygorii Strashko if (!vid) 18693a76530SGrygorii Strashko unreg_mcast = port_mask; 18793a76530SGrygorii Strashko dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid); 1882d64a034SGrygorii Strashko ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask, 18993a76530SGrygorii Strashko unreg_mcast, port_mask, 0); 19093a76530SGrygorii Strashko 19193a76530SGrygorii Strashko pm_runtime_put(common->dev); 19293a76530SGrygorii Strashko return ret; 19393a76530SGrygorii Strashko } 19493a76530SGrygorii Strashko 19593a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev, 19693a76530SGrygorii Strashko __be16 proto, u16 vid) 19793a76530SGrygorii Strashko { 19893a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1992d64a034SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 20093a76530SGrygorii Strashko int ret; 20193a76530SGrygorii Strashko 20286e8b070SVignesh Raghavendra if (!common->is_emac_mode) 20386e8b070SVignesh Raghavendra return 0; 20486e8b070SVignesh Raghavendra 2057bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 2067bcffde0SGrygorii Strashko return 0; 2077bcffde0SGrygorii Strashko 2082240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 2092240514cSMinghao Chi if (ret < 0) 21093a76530SGrygorii Strashko return ret; 21193a76530SGrygorii Strashko 21293a76530SGrygorii Strashko dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid); 2132d64a034SGrygorii Strashko ret = cpsw_ale_del_vlan(common->ale, vid, 2142d64a034SGrygorii Strashko BIT(port->port_id) | ALE_PORT_HOST); 21593a76530SGrygorii Strashko 21693a76530SGrygorii Strashko pm_runtime_put(common->dev); 21793a76530SGrygorii Strashko return ret; 21893a76530SGrygorii Strashko } 21993a76530SGrygorii Strashko 22084b4aa49SGrygorii Strashko static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port, 22193a76530SGrygorii Strashko bool promisc) 22293a76530SGrygorii Strashko { 22393a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 22493a76530SGrygorii Strashko 22586e8b070SVignesh Raghavendra if (promisc && !common->is_emac_mode) { 22686e8b070SVignesh Raghavendra dev_dbg(common->dev, "promisc mode requested in switch mode"); 22786e8b070SVignesh Raghavendra return; 22886e8b070SVignesh Raghavendra } 22986e8b070SVignesh Raghavendra 23093a76530SGrygorii Strashko if (promisc) { 23193a76530SGrygorii Strashko /* Enable promiscuous mode */ 23293a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 23393a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 1); 23493a76530SGrygorii Strashko dev_dbg(common->dev, "promisc enabled\n"); 23593a76530SGrygorii Strashko } else { 23693a76530SGrygorii Strashko /* Disable promiscuous mode */ 23793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 23893a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 0); 23993a76530SGrygorii Strashko dev_dbg(common->dev, "promisc disabled\n"); 24093a76530SGrygorii Strashko } 24193a76530SGrygorii Strashko } 24293a76530SGrygorii Strashko 24393a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev) 24493a76530SGrygorii Strashko { 24593a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 24693a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 24793a76530SGrygorii Strashko u32 port_mask; 24893a76530SGrygorii Strashko bool promisc; 24993a76530SGrygorii Strashko 25093a76530SGrygorii Strashko promisc = !!(ndev->flags & IFF_PROMISC); 25184b4aa49SGrygorii Strashko am65_cpsw_slave_set_promisc(port, promisc); 25293a76530SGrygorii Strashko 25393a76530SGrygorii Strashko if (promisc) 25493a76530SGrygorii Strashko return; 25593a76530SGrygorii Strashko 25693a76530SGrygorii Strashko /* Restore allmulti on vlans if necessary */ 25793a76530SGrygorii Strashko cpsw_ale_set_allmulti(common->ale, 25893a76530SGrygorii Strashko ndev->flags & IFF_ALLMULTI, port->port_id); 25993a76530SGrygorii Strashko 26093a76530SGrygorii Strashko port_mask = ALE_PORT_HOST; 26193a76530SGrygorii Strashko /* Clear all mcast from ALE */ 26293a76530SGrygorii Strashko cpsw_ale_flush_multicast(common->ale, port_mask, -1); 26393a76530SGrygorii Strashko 26493a76530SGrygorii Strashko if (!netdev_mc_empty(ndev)) { 26593a76530SGrygorii Strashko struct netdev_hw_addr *ha; 26693a76530SGrygorii Strashko 26793a76530SGrygorii Strashko /* program multicast address list into ALE register */ 26893a76530SGrygorii Strashko netdev_for_each_mc_addr(ha, ndev) { 26993a76530SGrygorii Strashko cpsw_ale_add_mcast(common->ale, ha->addr, 27093a76530SGrygorii Strashko port_mask, 0, 0, 0); 27193a76530SGrygorii Strashko } 27293a76530SGrygorii Strashko } 27393a76530SGrygorii Strashko } 27493a76530SGrygorii Strashko 27593a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev, 27693a76530SGrygorii Strashko unsigned int txqueue) 27793a76530SGrygorii Strashko { 27893a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 27993a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 28093a76530SGrygorii Strashko struct netdev_queue *netif_txq; 28193a76530SGrygorii Strashko unsigned long trans_start; 28293a76530SGrygorii Strashko 28393a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, txqueue); 28493a76530SGrygorii Strashko tx_chn = &common->tx_chns[txqueue]; 2855337824fSEric Dumazet trans_start = READ_ONCE(netif_txq->trans_start); 28693a76530SGrygorii Strashko 28793a76530SGrygorii Strashko netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n", 28893a76530SGrygorii Strashko txqueue, 28993a76530SGrygorii Strashko netif_tx_queue_stopped(netif_txq), 29093a76530SGrygorii Strashko jiffies_to_msecs(jiffies - trans_start), 29193a76530SGrygorii Strashko dql_avail(&netif_txq->dql), 29293a76530SGrygorii Strashko k3_cppi_desc_pool_avail(tx_chn->desc_pool)); 29393a76530SGrygorii Strashko 29493a76530SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 29593a76530SGrygorii Strashko /* try recover if stopped by us */ 29693a76530SGrygorii Strashko txq_trans_update(netif_txq); 29793a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 29893a76530SGrygorii Strashko } 29993a76530SGrygorii Strashko } 30093a76530SGrygorii Strashko 30193a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common, 30293a76530SGrygorii Strashko struct sk_buff *skb) 30393a76530SGrygorii Strashko { 30493a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 30593a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 30693a76530SGrygorii Strashko struct device *dev = common->dev; 30793a76530SGrygorii Strashko u32 pkt_len = skb_tailroom(skb); 30893a76530SGrygorii Strashko dma_addr_t desc_dma; 30993a76530SGrygorii Strashko dma_addr_t buf_dma; 31093a76530SGrygorii Strashko void *swdata; 31193a76530SGrygorii Strashko 31293a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool); 31393a76530SGrygorii Strashko if (!desc_rx) { 31493a76530SGrygorii Strashko dev_err(dev, "Failed to allocate RXFDQ descriptor\n"); 31593a76530SGrygorii Strashko return -ENOMEM; 31693a76530SGrygorii Strashko } 31793a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx); 31893a76530SGrygorii Strashko 319ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len, 320ed569ed9SPeter Ujfalusi DMA_FROM_DEVICE); 321ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) { 32293a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 32393a76530SGrygorii Strashko dev_err(dev, "Failed to map rx skb buffer\n"); 32493a76530SGrygorii Strashko return -EINVAL; 32593a76530SGrygorii Strashko } 32693a76530SGrygorii Strashko 32793a76530SGrygorii Strashko cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT, 32893a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 32939fd0547SPeter Ujfalusi k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma); 330a9e60cf0SGrygorii Strashko cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb)); 33193a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 33293a76530SGrygorii Strashko *((void **)swdata) = skb; 33393a76530SGrygorii Strashko 33493a76530SGrygorii Strashko return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma); 33593a76530SGrygorii Strashko } 33693a76530SGrygorii Strashko 33793a76530SGrygorii Strashko void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common) 33893a76530SGrygorii Strashko { 33993a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 34093a76530SGrygorii Strashko u32 val, pri_map; 34193a76530SGrygorii Strashko 34293a76530SGrygorii Strashko /* P0 set Receive Priority Type */ 34393a76530SGrygorii Strashko val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 34493a76530SGrygorii Strashko 34593a76530SGrygorii Strashko if (common->pf_p0_rx_ptype_rrobin) { 34693a76530SGrygorii Strashko val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 34793a76530SGrygorii Strashko /* Enet Ports fifos works in fixed priority mode only, so 34893a76530SGrygorii Strashko * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0 34993a76530SGrygorii Strashko */ 35093a76530SGrygorii Strashko pri_map = 0x0; 35193a76530SGrygorii Strashko } else { 35293a76530SGrygorii Strashko val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 35393a76530SGrygorii Strashko /* restore P0_Rx_Pri_Map */ 35493a76530SGrygorii Strashko pri_map = 0x76543210; 35593a76530SGrygorii Strashko } 35693a76530SGrygorii Strashko 35793a76530SGrygorii Strashko writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP); 35893a76530SGrygorii Strashko writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 35993a76530SGrygorii Strashko } 36093a76530SGrygorii Strashko 36158356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common); 36258356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common); 36358356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port); 36458356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port); 36558356eb3SVignesh Raghavendra 3663342a10fSJian Shen static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common) 36793a76530SGrygorii Strashko { 36893a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 36993a76530SGrygorii Strashko int port_idx, i, ret; 37093a76530SGrygorii Strashko struct sk_buff *skb; 37193a76530SGrygorii Strashko u32 val, port_mask; 37293a76530SGrygorii Strashko 37393a76530SGrygorii Strashko if (common->usage_count) 37493a76530SGrygorii Strashko return 0; 37593a76530SGrygorii Strashko 37693a76530SGrygorii Strashko /* Control register */ 37793a76530SGrygorii Strashko writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE | 37893a76530SGrygorii Strashko AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD, 37993a76530SGrygorii Strashko common->cpsw_base + AM65_CPSW_REG_CTL); 38093a76530SGrygorii Strashko /* Max length register */ 38193a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 38293a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 38393a76530SGrygorii Strashko /* set base flow_id */ 38493a76530SGrygorii Strashko writel(common->rx_flow_id_base, 38593a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET); 38693a76530SGrygorii Strashko /* en tx crc offload */ 38797067aafSGrygorii Strashko writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN, host_p->port_base + AM65_CPSW_P0_REG_CTL); 38893a76530SGrygorii Strashko 38993a76530SGrygorii Strashko am65_cpsw_nuss_set_p0_ptype(common); 39093a76530SGrygorii Strashko 39193a76530SGrygorii Strashko /* enable statistic */ 39293a76530SGrygorii Strashko val = BIT(HOST_PORT_NUM); 39393a76530SGrygorii Strashko for (port_idx = 0; port_idx < common->port_num; port_idx++) { 39493a76530SGrygorii Strashko struct am65_cpsw_port *port = &common->ports[port_idx]; 39593a76530SGrygorii Strashko 39693a76530SGrygorii Strashko if (!port->disabled) 39793a76530SGrygorii Strashko val |= BIT(port->port_id); 39893a76530SGrygorii Strashko } 39993a76530SGrygorii Strashko writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 40093a76530SGrygorii Strashko 40193a76530SGrygorii Strashko /* disable priority elevation */ 40293a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE); 40393a76530SGrygorii Strashko 40493a76530SGrygorii Strashko cpsw_ale_start(common->ale); 40593a76530SGrygorii Strashko 40693a76530SGrygorii Strashko /* limit to one RX flow only */ 40793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 40893a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ID, 0); 40993a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 41093a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ENABLE, 1); 41193a76530SGrygorii Strashko /* switch to vlan unaware mode */ 41293a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1); 41393a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 41493a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 41593a76530SGrygorii Strashko 41693a76530SGrygorii Strashko /* default vlan cfg: create mask based on enabled ports */ 41793a76530SGrygorii Strashko port_mask = GENMASK(common->port_num, 0) & 41893a76530SGrygorii Strashko ~common->disabled_ports_mask; 41993a76530SGrygorii Strashko 42093a76530SGrygorii Strashko cpsw_ale_add_vlan(common->ale, 0, port_mask, 42193a76530SGrygorii Strashko port_mask, port_mask, 42293a76530SGrygorii Strashko port_mask & ~ALE_PORT_HOST); 42393a76530SGrygorii Strashko 42458356eb3SVignesh Raghavendra if (common->is_emac_mode) 42558356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(common); 42658356eb3SVignesh Raghavendra else 42758356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(common); 42858356eb3SVignesh Raghavendra 42993a76530SGrygorii Strashko for (i = 0; i < common->rx_chns.descs_num; i++) { 43093a76530SGrygorii Strashko skb = __netdev_alloc_skb_ip_align(NULL, 43193a76530SGrygorii Strashko AM65_CPSW_MAX_PACKET_SIZE, 43293a76530SGrygorii Strashko GFP_KERNEL); 43393a76530SGrygorii Strashko if (!skb) { 43493a76530SGrygorii Strashko dev_err(common->dev, "cannot allocate skb\n"); 43593a76530SGrygorii Strashko return -ENOMEM; 43693a76530SGrygorii Strashko } 43793a76530SGrygorii Strashko 43893a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, skb); 43993a76530SGrygorii Strashko if (ret < 0) { 44093a76530SGrygorii Strashko dev_err(common->dev, 44193a76530SGrygorii Strashko "cannot submit skb to channel rx, error %d\n", 44293a76530SGrygorii Strashko ret); 44393a76530SGrygorii Strashko kfree_skb(skb); 44493a76530SGrygorii Strashko return ret; 44593a76530SGrygorii Strashko } 44693a76530SGrygorii Strashko kmemleak_not_leak(skb); 44793a76530SGrygorii Strashko } 44893a76530SGrygorii Strashko k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn); 44993a76530SGrygorii Strashko 45093a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 45193a76530SGrygorii Strashko ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn); 45293a76530SGrygorii Strashko if (ret) 45393a76530SGrygorii Strashko return ret; 45493a76530SGrygorii Strashko napi_enable(&common->tx_chns[i].napi_tx); 45593a76530SGrygorii Strashko } 45693a76530SGrygorii Strashko 45793a76530SGrygorii Strashko napi_enable(&common->napi_rx); 45847bfc4d1SVignesh Raghavendra if (common->rx_irq_disabled) { 45947bfc4d1SVignesh Raghavendra common->rx_irq_disabled = false; 46047bfc4d1SVignesh Raghavendra enable_irq(common->rx_chns.irq); 46147bfc4d1SVignesh Raghavendra } 46293a76530SGrygorii Strashko 46393a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss started\n"); 46493a76530SGrygorii Strashko return 0; 46593a76530SGrygorii Strashko } 46693a76530SGrygorii Strashko 46793a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma); 46893a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma); 46993a76530SGrygorii Strashko 47093a76530SGrygorii Strashko static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common) 47193a76530SGrygorii Strashko { 47293a76530SGrygorii Strashko int i; 47393a76530SGrygorii Strashko 47493a76530SGrygorii Strashko if (common->usage_count != 1) 47593a76530SGrygorii Strashko return 0; 47693a76530SGrygorii Strashko 47793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 47893a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 47993a76530SGrygorii Strashko 48093a76530SGrygorii Strashko /* shutdown tx channels */ 48193a76530SGrygorii Strashko atomic_set(&common->tdown_cnt, common->tx_ch_num); 48293a76530SGrygorii Strashko /* ensure new tdown_cnt value is visible */ 48393a76530SGrygorii Strashko smp_mb__after_atomic(); 48493a76530SGrygorii Strashko reinit_completion(&common->tdown_complete); 48593a76530SGrygorii Strashko 48693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 48793a76530SGrygorii Strashko k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false); 48893a76530SGrygorii Strashko 48993a76530SGrygorii Strashko i = wait_for_completion_timeout(&common->tdown_complete, 49093a76530SGrygorii Strashko msecs_to_jiffies(1000)); 49193a76530SGrygorii Strashko if (!i) 49293a76530SGrygorii Strashko dev_err(common->dev, "tx timeout\n"); 49393a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 49493a76530SGrygorii Strashko napi_disable(&common->tx_chns[i].napi_tx); 49593a76530SGrygorii Strashko 49693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 49793a76530SGrygorii Strashko k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn, 49893a76530SGrygorii Strashko &common->tx_chns[i], 49993a76530SGrygorii Strashko am65_cpsw_nuss_tx_cleanup); 50093a76530SGrygorii Strashko k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn); 50193a76530SGrygorii Strashko } 50293a76530SGrygorii Strashko 50393a76530SGrygorii Strashko k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true); 50493a76530SGrygorii Strashko napi_disable(&common->napi_rx); 50593a76530SGrygorii Strashko 50693a76530SGrygorii Strashko for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++) 50793a76530SGrygorii Strashko k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i, 50893a76530SGrygorii Strashko &common->rx_chns, 50993a76530SGrygorii Strashko am65_cpsw_nuss_rx_cleanup, !!i); 51093a76530SGrygorii Strashko 51193a76530SGrygorii Strashko k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn); 51293a76530SGrygorii Strashko 51393a76530SGrygorii Strashko cpsw_ale_stop(common->ale); 51493a76530SGrygorii Strashko 51593a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_CTL); 51693a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 51793a76530SGrygorii Strashko 51893a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss stopped\n"); 51993a76530SGrygorii Strashko return 0; 52093a76530SGrygorii Strashko } 52193a76530SGrygorii Strashko 52293a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev) 52393a76530SGrygorii Strashko { 52493a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 52593a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 52693a76530SGrygorii Strashko int ret; 52793a76530SGrygorii Strashko 528e8609e69SSiddharth Vadapalli phylink_stop(port->slave.phylink); 52993a76530SGrygorii Strashko 53093a76530SGrygorii Strashko netif_tx_stop_all_queues(ndev); 53193a76530SGrygorii Strashko 532e8609e69SSiddharth Vadapalli phylink_disconnect_phy(port->slave.phylink); 53393a76530SGrygorii Strashko 53493a76530SGrygorii Strashko ret = am65_cpsw_nuss_common_stop(common); 53593a76530SGrygorii Strashko if (ret) 53693a76530SGrygorii Strashko return ret; 53793a76530SGrygorii Strashko 53893a76530SGrygorii Strashko common->usage_count--; 53993a76530SGrygorii Strashko pm_runtime_put(common->dev); 54093a76530SGrygorii Strashko return 0; 54193a76530SGrygorii Strashko } 54293a76530SGrygorii Strashko 5437bcffde0SGrygorii Strashko static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) 5447bcffde0SGrygorii Strashko { 5457bcffde0SGrygorii Strashko struct am65_cpsw_port *port = arg; 5467bcffde0SGrygorii Strashko 5477bcffde0SGrygorii Strashko if (!vdev) 5487bcffde0SGrygorii Strashko return 0; 5497bcffde0SGrygorii Strashko 5507bcffde0SGrygorii Strashko return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid); 5517bcffde0SGrygorii Strashko } 5527bcffde0SGrygorii Strashko 55393a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev) 55493a76530SGrygorii Strashko { 55593a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 55693a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 55793a76530SGrygorii Strashko int ret, i; 55893a76530SGrygorii Strashko 5592240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 5602240514cSMinghao Chi if (ret < 0) 56193a76530SGrygorii Strashko return ret; 56293a76530SGrygorii Strashko 56393a76530SGrygorii Strashko /* Notify the stack of the actual queue counts. */ 56493a76530SGrygorii Strashko ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num); 56593a76530SGrygorii Strashko if (ret) { 56693a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of tx queues\n"); 56793a76530SGrygorii Strashko return ret; 56893a76530SGrygorii Strashko } 56993a76530SGrygorii Strashko 57093a76530SGrygorii Strashko ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES); 57193a76530SGrygorii Strashko if (ret) { 57293a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of rx queues\n"); 57393a76530SGrygorii Strashko return ret; 57493a76530SGrygorii Strashko } 57593a76530SGrygorii Strashko 57693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 57793a76530SGrygorii Strashko netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i)); 57893a76530SGrygorii Strashko 5793342a10fSJian Shen ret = am65_cpsw_nuss_common_open(common); 58093a76530SGrygorii Strashko if (ret) 58193a76530SGrygorii Strashko return ret; 58293a76530SGrygorii Strashko 58393a76530SGrygorii Strashko common->usage_count++; 58493a76530SGrygorii Strashko 58593a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, ndev->dev_addr); 58693a76530SGrygorii Strashko 58758356eb3SVignesh Raghavendra if (common->is_emac_mode) 58858356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 58958356eb3SVignesh Raghavendra else 59058356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 59193a76530SGrygorii Strashko 59293a76530SGrygorii Strashko /* mac_sl should be configured via phy-link interface */ 59393a76530SGrygorii Strashko am65_cpsw_sl_ctl_reset(port); 59493a76530SGrygorii Strashko 595e8609e69SSiddharth Vadapalli ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0); 596e8609e69SSiddharth Vadapalli if (ret) 59793a76530SGrygorii Strashko goto error_cleanup; 59893a76530SGrygorii Strashko 5997bcffde0SGrygorii Strashko /* restore vlan configurations */ 6007bcffde0SGrygorii Strashko vlan_for_each(ndev, cpsw_restore_vlans, port); 6017bcffde0SGrygorii Strashko 602e8609e69SSiddharth Vadapalli phylink_start(port->slave.phylink); 60393a76530SGrygorii Strashko 60493a76530SGrygorii Strashko return 0; 60593a76530SGrygorii Strashko 60693a76530SGrygorii Strashko error_cleanup: 60793a76530SGrygorii Strashko am65_cpsw_nuss_ndo_slave_stop(ndev); 60893a76530SGrygorii Strashko return ret; 60993a76530SGrygorii Strashko } 61093a76530SGrygorii Strashko 61193a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma) 61293a76530SGrygorii Strashko { 61393a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = data; 61493a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 61593a76530SGrygorii Strashko struct sk_buff *skb; 61693a76530SGrygorii Strashko dma_addr_t buf_dma; 61793a76530SGrygorii Strashko u32 buf_dma_len; 61893a76530SGrygorii Strashko void **swdata; 61993a76530SGrygorii Strashko 62093a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 62193a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 62293a76530SGrygorii Strashko skb = *swdata; 62393a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 62439fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 62593a76530SGrygorii Strashko 626ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 62793a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 62893a76530SGrygorii Strashko 62993a76530SGrygorii Strashko dev_kfree_skb_any(skb); 63093a76530SGrygorii Strashko } 63193a76530SGrygorii Strashko 632b1f66a5bSGrygorii Strashko static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata) 633b1f66a5bSGrygorii Strashko { 634b1f66a5bSGrygorii Strashko struct skb_shared_hwtstamps *ssh; 635b1f66a5bSGrygorii Strashko u64 ns; 636b1f66a5bSGrygorii Strashko 637b1f66a5bSGrygorii Strashko ns = ((u64)psdata[1] << 32) | psdata[0]; 638b1f66a5bSGrygorii Strashko 639b1f66a5bSGrygorii Strashko ssh = skb_hwtstamps(skb); 640b1f66a5bSGrygorii Strashko memset(ssh, 0, sizeof(*ssh)); 641b1f66a5bSGrygorii Strashko ssh->hwtstamp = ns_to_ktime(ns); 642b1f66a5bSGrygorii Strashko } 643b1f66a5bSGrygorii Strashko 64493a76530SGrygorii Strashko /* RX psdata[2] word format - checksum information */ 64593a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0) 64693a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16) 64793a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17) 64893a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_TCP BIT(18) 64993a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19) 65093a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20) 65193a76530SGrygorii Strashko 65293a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info) 65393a76530SGrygorii Strashko { 65493a76530SGrygorii Strashko /* HW can verify IPv4/IPv6 TCP/UDP packets checksum 65593a76530SGrygorii Strashko * csum information provides in psdata[2] word: 65693a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error 65793a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID 65893a76530SGrygorii Strashko * bits - indicates IPv4/IPv6 packet 65993a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet 66093a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets 66193a76530SGrygorii Strashko * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR 66293a76530SGrygorii Strashko */ 66393a76530SGrygorii Strashko skb_checksum_none_assert(skb); 66493a76530SGrygorii Strashko 66593a76530SGrygorii Strashko if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) 66693a76530SGrygorii Strashko return; 66793a76530SGrygorii Strashko 66893a76530SGrygorii Strashko if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID | 66993a76530SGrygorii Strashko AM65_CPSW_RX_PSD_IPV4_VALID)) && 67093a76530SGrygorii Strashko !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) { 67193a76530SGrygorii Strashko /* csum for fragmented packets is unsupported */ 67293a76530SGrygorii Strashko if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT)) 67393a76530SGrygorii Strashko skb->ip_summed = CHECKSUM_UNNECESSARY; 67493a76530SGrygorii Strashko } 67593a76530SGrygorii Strashko } 67693a76530SGrygorii Strashko 67793a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common, 67893a76530SGrygorii Strashko u32 flow_idx) 67993a76530SGrygorii Strashko { 68093a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 68193a76530SGrygorii Strashko u32 buf_dma_len, pkt_len, port_id = 0, csum_info; 68293a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 68393a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 68493a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 68593a76530SGrygorii Strashko struct device *dev = common->dev; 68693a76530SGrygorii Strashko struct sk_buff *skb, *new_skb; 68793a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 68893a76530SGrygorii Strashko struct am65_cpsw_port *port; 68993a76530SGrygorii Strashko struct net_device *ndev; 69093a76530SGrygorii Strashko void **swdata; 69193a76530SGrygorii Strashko u32 *psdata; 69293a76530SGrygorii Strashko int ret = 0; 69393a76530SGrygorii Strashko 69493a76530SGrygorii Strashko ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma); 69593a76530SGrygorii Strashko if (ret) { 69693a76530SGrygorii Strashko if (ret != -ENODATA) 69793a76530SGrygorii Strashko dev_err(dev, "RX: pop chn fail %d\n", ret); 69893a76530SGrygorii Strashko return ret; 69993a76530SGrygorii Strashko } 70093a76530SGrygorii Strashko 7016a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 70293a76530SGrygorii Strashko dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx); 70393a76530SGrygorii Strashko return 0; 70493a76530SGrygorii Strashko } 70593a76530SGrygorii Strashko 70693a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 70793a76530SGrygorii Strashko dev_dbg(dev, "%s flow_idx: %u desc %pad\n", 70893a76530SGrygorii Strashko __func__, flow_idx, &desc_dma); 70993a76530SGrygorii Strashko 71093a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 71193a76530SGrygorii Strashko skb = *swdata; 71293a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 71339fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 71493a76530SGrygorii Strashko pkt_len = cppi5_hdesc_get_pktlen(desc_rx); 71593a76530SGrygorii Strashko cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL); 71693a76530SGrygorii Strashko dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id); 71793a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 71893a76530SGrygorii Strashko ndev = port->ndev; 71993a76530SGrygorii Strashko skb->dev = ndev; 72093a76530SGrygorii Strashko 72193a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(desc_rx); 722b1f66a5bSGrygorii Strashko /* add RX timestamp */ 723b1f66a5bSGrygorii Strashko if (port->rx_ts_enabled) 724b1f66a5bSGrygorii Strashko am65_cpsw_nuss_rx_ts(skb, psdata); 72593a76530SGrygorii Strashko csum_info = psdata[2]; 72693a76530SGrygorii Strashko dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info); 72793a76530SGrygorii Strashko 728ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 72993a76530SGrygorii Strashko 73093a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 73193a76530SGrygorii Strashko 73293a76530SGrygorii Strashko new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE); 73393a76530SGrygorii Strashko if (new_skb) { 73486e8b070SVignesh Raghavendra ndev_priv = netdev_priv(ndev); 73586e8b070SVignesh Raghavendra am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark); 73693a76530SGrygorii Strashko skb_put(skb, pkt_len); 73793a76530SGrygorii Strashko skb->protocol = eth_type_trans(skb, ndev); 73893a76530SGrygorii Strashko am65_cpsw_nuss_rx_csum(skb, csum_info); 73993a76530SGrygorii Strashko napi_gro_receive(&common->napi_rx, skb); 74093a76530SGrygorii Strashko 74193a76530SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 74293a76530SGrygorii Strashko 74393a76530SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 74493a76530SGrygorii Strashko stats->rx_packets++; 74593a76530SGrygorii Strashko stats->rx_bytes += pkt_len; 74693a76530SGrygorii Strashko u64_stats_update_end(&stats->syncp); 74793a76530SGrygorii Strashko kmemleak_not_leak(new_skb); 74893a76530SGrygorii Strashko } else { 74993a76530SGrygorii Strashko ndev->stats.rx_dropped++; 75093a76530SGrygorii Strashko new_skb = skb; 75193a76530SGrygorii Strashko } 75293a76530SGrygorii Strashko 75393a76530SGrygorii Strashko if (netif_dormant(ndev)) { 75493a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 75593a76530SGrygorii Strashko ndev->stats.rx_dropped++; 75693a76530SGrygorii Strashko return 0; 75793a76530SGrygorii Strashko } 75893a76530SGrygorii Strashko 75993a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, new_skb); 76093a76530SGrygorii Strashko if (WARN_ON(ret < 0)) { 76193a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 76293a76530SGrygorii Strashko ndev->stats.rx_errors++; 76393a76530SGrygorii Strashko ndev->stats.rx_dropped++; 76493a76530SGrygorii Strashko } 76593a76530SGrygorii Strashko 76693a76530SGrygorii Strashko return ret; 76793a76530SGrygorii Strashko } 76893a76530SGrygorii Strashko 76993a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget) 77093a76530SGrygorii Strashko { 77193a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx); 77293a76530SGrygorii Strashko int flow = AM65_CPSW_MAX_RX_FLOWS; 77393a76530SGrygorii Strashko int cur_budget, ret; 77493a76530SGrygorii Strashko int num_rx = 0; 77593a76530SGrygorii Strashko 77693a76530SGrygorii Strashko /* process every flow */ 77793a76530SGrygorii Strashko while (flow--) { 77893a76530SGrygorii Strashko cur_budget = budget - num_rx; 77993a76530SGrygorii Strashko 78093a76530SGrygorii Strashko while (cur_budget--) { 78193a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_packets(common, flow); 78293a76530SGrygorii Strashko if (ret) 78393a76530SGrygorii Strashko break; 78493a76530SGrygorii Strashko num_rx++; 78593a76530SGrygorii Strashko } 78693a76530SGrygorii Strashko 78793a76530SGrygorii Strashko if (num_rx >= budget) 78893a76530SGrygorii Strashko break; 78993a76530SGrygorii Strashko } 79093a76530SGrygorii Strashko 79193a76530SGrygorii Strashko dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget); 79293a76530SGrygorii Strashko 79347bfc4d1SVignesh Raghavendra if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) { 79447bfc4d1SVignesh Raghavendra if (common->rx_irq_disabled) { 79547bfc4d1SVignesh Raghavendra common->rx_irq_disabled = false; 79693a76530SGrygorii Strashko enable_irq(common->rx_chns.irq); 79747bfc4d1SVignesh Raghavendra } 79847bfc4d1SVignesh Raghavendra } 79993a76530SGrygorii Strashko 80093a76530SGrygorii Strashko return num_rx; 80193a76530SGrygorii Strashko } 80293a76530SGrygorii Strashko 80393a76530SGrygorii Strashko static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn, 80493a76530SGrygorii Strashko struct cppi5_host_desc_t *desc) 80593a76530SGrygorii Strashko { 80693a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc; 80793a76530SGrygorii Strashko dma_addr_t buf_dma, next_desc_dma; 80893a76530SGrygorii Strashko u32 buf_dma_len; 80993a76530SGrygorii Strashko 81093a76530SGrygorii Strashko first_desc = desc; 81193a76530SGrygorii Strashko next_desc = first_desc; 81293a76530SGrygorii Strashko 81393a76530SGrygorii Strashko cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len); 81439fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 81593a76530SGrygorii Strashko 816ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE); 81793a76530SGrygorii Strashko 81893a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc); 81939fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 82093a76530SGrygorii Strashko while (next_desc_dma) { 82193a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 82293a76530SGrygorii Strashko next_desc_dma); 82393a76530SGrygorii Strashko cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len); 82439fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 82593a76530SGrygorii Strashko 826ed569ed9SPeter Ujfalusi dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len, 82793a76530SGrygorii Strashko DMA_TO_DEVICE); 82893a76530SGrygorii Strashko 82993a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc); 83039fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 83193a76530SGrygorii Strashko 83293a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 83393a76530SGrygorii Strashko } 83493a76530SGrygorii Strashko 83593a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc); 83693a76530SGrygorii Strashko } 83793a76530SGrygorii Strashko 83893a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma) 83993a76530SGrygorii Strashko { 84093a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = data; 84193a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 84293a76530SGrygorii Strashko struct sk_buff *skb; 84393a76530SGrygorii Strashko void **swdata; 84493a76530SGrygorii Strashko 84593a76530SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma); 84693a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 84793a76530SGrygorii Strashko skb = *(swdata); 848ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 84993a76530SGrygorii Strashko 85093a76530SGrygorii Strashko dev_kfree_skb_any(skb); 85193a76530SGrygorii Strashko } 85293a76530SGrygorii Strashko 853a9e60cf0SGrygorii Strashko static struct sk_buff * 854a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn, 855a9e60cf0SGrygorii Strashko dma_addr_t desc_dma) 856a9e60cf0SGrygorii Strashko { 857a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 858a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 859a9e60cf0SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 860a9e60cf0SGrygorii Strashko struct net_device *ndev; 861a9e60cf0SGrygorii Strashko struct sk_buff *skb; 862a9e60cf0SGrygorii Strashko void **swdata; 863a9e60cf0SGrygorii Strashko 864a9e60cf0SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 865a9e60cf0SGrygorii Strashko desc_dma); 866a9e60cf0SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 867a9e60cf0SGrygorii Strashko skb = *(swdata); 868ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 869a9e60cf0SGrygorii Strashko 870a9e60cf0SGrygorii Strashko ndev = skb->dev; 871a9e60cf0SGrygorii Strashko 872a9e60cf0SGrygorii Strashko am65_cpts_tx_timestamp(tx_chn->common->cpts, skb); 873a9e60cf0SGrygorii Strashko 874a9e60cf0SGrygorii Strashko ndev_priv = netdev_priv(ndev); 875a9e60cf0SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 876a9e60cf0SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 877a9e60cf0SGrygorii Strashko stats->tx_packets++; 878a9e60cf0SGrygorii Strashko stats->tx_bytes += skb->len; 879a9e60cf0SGrygorii Strashko u64_stats_update_end(&stats->syncp); 880a9e60cf0SGrygorii Strashko 881a9e60cf0SGrygorii Strashko return skb; 882a9e60cf0SGrygorii Strashko } 883a9e60cf0SGrygorii Strashko 884a9e60cf0SGrygorii Strashko static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev, 885a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq) 886a9e60cf0SGrygorii Strashko { 887a9e60cf0SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 888a9e60cf0SGrygorii Strashko /* Check whether the queue is stopped due to stalled 889a9e60cf0SGrygorii Strashko * tx dma, if the queue is stopped then wake the queue 890a9e60cf0SGrygorii Strashko * as we have free desc for tx 891a9e60cf0SGrygorii Strashko */ 892a9e60cf0SGrygorii Strashko __netif_tx_lock(netif_txq, smp_processor_id()); 893a9e60cf0SGrygorii Strashko if (netif_running(ndev) && 894a9e60cf0SGrygorii Strashko (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS)) 895a9e60cf0SGrygorii Strashko netif_tx_wake_queue(netif_txq); 896a9e60cf0SGrygorii Strashko 897a9e60cf0SGrygorii Strashko __netif_tx_unlock(netif_txq); 898a9e60cf0SGrygorii Strashko } 899a9e60cf0SGrygorii Strashko } 900a9e60cf0SGrygorii Strashko 90193a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common, 90293a76530SGrygorii Strashko int chn, unsigned int budget) 90393a76530SGrygorii Strashko { 90493a76530SGrygorii Strashko struct device *dev = common->dev; 90593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 90693a76530SGrygorii Strashko struct netdev_queue *netif_txq; 90793a76530SGrygorii Strashko unsigned int total_bytes = 0; 90893a76530SGrygorii Strashko struct net_device *ndev; 90993a76530SGrygorii Strashko struct sk_buff *skb; 91093a76530SGrygorii Strashko dma_addr_t desc_dma; 91193a76530SGrygorii Strashko int res, num_tx = 0; 91293a76530SGrygorii Strashko 91393a76530SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 91493a76530SGrygorii Strashko 91593a76530SGrygorii Strashko while (true) { 916a9e60cf0SGrygorii Strashko spin_lock(&tx_chn->lock); 917a9e60cf0SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 918a9e60cf0SGrygorii Strashko spin_unlock(&tx_chn->lock); 919a9e60cf0SGrygorii Strashko if (res == -ENODATA) 920a9e60cf0SGrygorii Strashko break; 92193a76530SGrygorii Strashko 922a9e60cf0SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 923a9e60cf0SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 924a9e60cf0SGrygorii Strashko complete(&common->tdown_complete); 925a9e60cf0SGrygorii Strashko break; 926a9e60cf0SGrygorii Strashko } 927a9e60cf0SGrygorii Strashko 928a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 929a9e60cf0SGrygorii Strashko total_bytes = skb->len; 930a9e60cf0SGrygorii Strashko ndev = skb->dev; 931a9e60cf0SGrygorii Strashko napi_consume_skb(skb, budget); 932a9e60cf0SGrygorii Strashko num_tx++; 933a9e60cf0SGrygorii Strashko 934a9e60cf0SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 935a9e60cf0SGrygorii Strashko 936a9e60cf0SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 937a9e60cf0SGrygorii Strashko 938a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 939a9e60cf0SGrygorii Strashko } 940a9e60cf0SGrygorii Strashko 941a9e60cf0SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 942a9e60cf0SGrygorii Strashko 943a9e60cf0SGrygorii Strashko return num_tx; 944a9e60cf0SGrygorii Strashko } 945a9e60cf0SGrygorii Strashko 946a9e60cf0SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common, 947a9e60cf0SGrygorii Strashko int chn, unsigned int budget) 948a9e60cf0SGrygorii Strashko { 949a9e60cf0SGrygorii Strashko struct device *dev = common->dev; 950a9e60cf0SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 951a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq; 952a9e60cf0SGrygorii Strashko unsigned int total_bytes = 0; 953a9e60cf0SGrygorii Strashko struct net_device *ndev; 954a9e60cf0SGrygorii Strashko struct sk_buff *skb; 955a9e60cf0SGrygorii Strashko dma_addr_t desc_dma; 956a9e60cf0SGrygorii Strashko int res, num_tx = 0; 957a9e60cf0SGrygorii Strashko 958a9e60cf0SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 959a9e60cf0SGrygorii Strashko 960a9e60cf0SGrygorii Strashko while (true) { 96193a76530SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 96293a76530SGrygorii Strashko if (res == -ENODATA) 96393a76530SGrygorii Strashko break; 96493a76530SGrygorii Strashko 9656a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 96693a76530SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 96793a76530SGrygorii Strashko complete(&common->tdown_complete); 96893a76530SGrygorii Strashko break; 96993a76530SGrygorii Strashko } 97093a76530SGrygorii Strashko 971a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 97293a76530SGrygorii Strashko 97393a76530SGrygorii Strashko ndev = skb->dev; 97493a76530SGrygorii Strashko total_bytes += skb->len; 97593a76530SGrygorii Strashko napi_consume_skb(skb, budget); 97693a76530SGrygorii Strashko num_tx++; 97793a76530SGrygorii Strashko } 97893a76530SGrygorii Strashko 97993a76530SGrygorii Strashko if (!num_tx) 98093a76530SGrygorii Strashko return 0; 98193a76530SGrygorii Strashko 98293a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 98393a76530SGrygorii Strashko 98493a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 98593a76530SGrygorii Strashko 986a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 98793a76530SGrygorii Strashko 98893a76530SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 98993a76530SGrygorii Strashko 99093a76530SGrygorii Strashko return num_tx; 99193a76530SGrygorii Strashko } 99293a76530SGrygorii Strashko 99393a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget) 99493a76530SGrygorii Strashko { 99593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx); 99693a76530SGrygorii Strashko int num_tx; 99793a76530SGrygorii Strashko 998a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(tx_chn->common)) 999a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget); 1000a9e60cf0SGrygorii Strashko else 1001a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget); 1002a9e60cf0SGrygorii Strashko 10033bacbe04SGrygorii Strashko if (num_tx >= budget) 10043bacbe04SGrygorii Strashko return budget; 100593a76530SGrygorii Strashko 10063bacbe04SGrygorii Strashko if (napi_complete_done(napi_tx, num_tx)) 10073bacbe04SGrygorii Strashko enable_irq(tx_chn->irq); 10083bacbe04SGrygorii Strashko 10093bacbe04SGrygorii Strashko return 0; 101093a76530SGrygorii Strashko } 101193a76530SGrygorii Strashko 101293a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id) 101393a76530SGrygorii Strashko { 101493a76530SGrygorii Strashko struct am65_cpsw_common *common = dev_id; 101593a76530SGrygorii Strashko 101647bfc4d1SVignesh Raghavendra common->rx_irq_disabled = true; 101793a76530SGrygorii Strashko disable_irq_nosync(irq); 101893a76530SGrygorii Strashko napi_schedule(&common->napi_rx); 101993a76530SGrygorii Strashko 102093a76530SGrygorii Strashko return IRQ_HANDLED; 102193a76530SGrygorii Strashko } 102293a76530SGrygorii Strashko 102393a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id) 102493a76530SGrygorii Strashko { 102593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = dev_id; 102693a76530SGrygorii Strashko 102793a76530SGrygorii Strashko disable_irq_nosync(irq); 102893a76530SGrygorii Strashko napi_schedule(&tx_chn->napi_tx); 102993a76530SGrygorii Strashko 103093a76530SGrygorii Strashko return IRQ_HANDLED; 103193a76530SGrygorii Strashko } 103293a76530SGrygorii Strashko 103393a76530SGrygorii Strashko static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb, 103493a76530SGrygorii Strashko struct net_device *ndev) 103593a76530SGrygorii Strashko { 103693a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 103793a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc; 103893a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 103993a76530SGrygorii Strashko struct device *dev = common->dev; 104093a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 104193a76530SGrygorii Strashko struct netdev_queue *netif_txq; 104293a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 104393a76530SGrygorii Strashko int ret, q_idx, i; 104493a76530SGrygorii Strashko void **swdata; 104593a76530SGrygorii Strashko u32 *psdata; 104693a76530SGrygorii Strashko u32 pkt_len; 104793a76530SGrygorii Strashko 104893a76530SGrygorii Strashko /* padding enabled in hw */ 104993a76530SGrygorii Strashko pkt_len = skb_headlen(skb); 105093a76530SGrygorii Strashko 1051b1f66a5bSGrygorii Strashko /* SKB TX timestamp */ 1052b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1053b1f66a5bSGrygorii Strashko am65_cpts_prep_tx_timestamp(common->cpts, skb); 1054b1f66a5bSGrygorii Strashko 105593a76530SGrygorii Strashko q_idx = skb_get_queue_mapping(skb); 105693a76530SGrygorii Strashko dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx); 105793a76530SGrygorii Strashko 105893a76530SGrygorii Strashko tx_chn = &common->tx_chns[q_idx]; 105993a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, q_idx); 106093a76530SGrygorii Strashko 106193a76530SGrygorii Strashko /* Map the linear buffer */ 1062ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len, 106393a76530SGrygorii Strashko DMA_TO_DEVICE); 1064ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 106593a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb buffer\n"); 106693a76530SGrygorii Strashko ndev->stats.tx_errors++; 106793a76530SGrygorii Strashko goto err_free_skb; 106893a76530SGrygorii Strashko } 106993a76530SGrygorii Strashko 107093a76530SGrygorii Strashko first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 107193a76530SGrygorii Strashko if (!first_desc) { 107293a76530SGrygorii Strashko dev_dbg(dev, "Failed to allocate descriptor\n"); 1073ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len, 1074ed569ed9SPeter Ujfalusi DMA_TO_DEVICE); 107593a76530SGrygorii Strashko goto busy_stop_q; 107693a76530SGrygorii Strashko } 107793a76530SGrygorii Strashko 107893a76530SGrygorii Strashko cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT, 107993a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 108093a76530SGrygorii Strashko cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF); 108193a76530SGrygorii Strashko cppi5_hdesc_set_pkttype(first_desc, 0x7); 108293a76530SGrygorii Strashko cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id); 108393a76530SGrygorii Strashko 108439fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 108593a76530SGrygorii Strashko cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len); 108693a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(first_desc); 108793a76530SGrygorii Strashko *(swdata) = skb; 108893a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(first_desc); 108993a76530SGrygorii Strashko 109093a76530SGrygorii Strashko /* HW csum offload if enabled */ 109193a76530SGrygorii Strashko psdata[2] = 0; 109293a76530SGrygorii Strashko if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) { 109393a76530SGrygorii Strashko unsigned int cs_start, cs_offset; 109493a76530SGrygorii Strashko 109593a76530SGrygorii Strashko cs_start = skb_transport_offset(skb); 109693a76530SGrygorii Strashko cs_offset = cs_start + skb->csum_offset; 109793a76530SGrygorii Strashko /* HW numerates bytes starting from 1 */ 109893a76530SGrygorii Strashko psdata[2] = ((cs_offset + 1) << 24) | 109993a76530SGrygorii Strashko ((cs_start + 1) << 16) | (skb->len - cs_start); 110093a76530SGrygorii Strashko dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]); 110193a76530SGrygorii Strashko } 110293a76530SGrygorii Strashko 110393a76530SGrygorii Strashko if (!skb_is_nonlinear(skb)) 110493a76530SGrygorii Strashko goto done_tx; 110593a76530SGrygorii Strashko 110693a76530SGrygorii Strashko dev_dbg(dev, "fragmented SKB\n"); 110793a76530SGrygorii Strashko 110893a76530SGrygorii Strashko /* Handle the case where skb is fragmented in pages */ 110993a76530SGrygorii Strashko cur_desc = first_desc; 111093a76530SGrygorii Strashko for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 111193a76530SGrygorii Strashko skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 111293a76530SGrygorii Strashko u32 frag_size = skb_frag_size(frag); 111393a76530SGrygorii Strashko 111493a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 111593a76530SGrygorii Strashko if (!next_desc) { 111693a76530SGrygorii Strashko dev_err(dev, "Failed to allocate descriptor\n"); 111793a76530SGrygorii Strashko goto busy_free_descs; 111893a76530SGrygorii Strashko } 111993a76530SGrygorii Strashko 1120ed569ed9SPeter Ujfalusi buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size, 112193a76530SGrygorii Strashko DMA_TO_DEVICE); 1122ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 112393a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb page\n"); 112493a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 112593a76530SGrygorii Strashko ndev->stats.tx_errors++; 112693a76530SGrygorii Strashko goto err_free_descs; 112793a76530SGrygorii Strashko } 112893a76530SGrygorii Strashko 112993a76530SGrygorii Strashko cppi5_hdesc_reset_hbdesc(next_desc); 113039fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 113193a76530SGrygorii Strashko cppi5_hdesc_attach_buf(next_desc, 113293a76530SGrygorii Strashko buf_dma, frag_size, buf_dma, frag_size); 113393a76530SGrygorii Strashko 113493a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, 113593a76530SGrygorii Strashko next_desc); 113639fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma); 113793a76530SGrygorii Strashko cppi5_hdesc_link_hbdesc(cur_desc, desc_dma); 113893a76530SGrygorii Strashko 113993a76530SGrygorii Strashko pkt_len += frag_size; 114093a76530SGrygorii Strashko cur_desc = next_desc; 114193a76530SGrygorii Strashko } 114293a76530SGrygorii Strashko WARN_ON(pkt_len != skb->len); 114393a76530SGrygorii Strashko 114493a76530SGrygorii Strashko done_tx: 114593a76530SGrygorii Strashko skb_tx_timestamp(skb); 114693a76530SGrygorii Strashko 114793a76530SGrygorii Strashko /* report bql before sending packet */ 114893a76530SGrygorii Strashko netdev_tx_sent_queue(netif_txq, pkt_len); 114993a76530SGrygorii Strashko 115093a76530SGrygorii Strashko cppi5_hdesc_set_pktlen(first_desc, pkt_len); 115193a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc); 1152a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(common)) { 115393a76530SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1154a9e60cf0SGrygorii Strashko } else { 1155a9e60cf0SGrygorii Strashko spin_lock_bh(&tx_chn->lock); 1156a9e60cf0SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1157a9e60cf0SGrygorii Strashko spin_unlock_bh(&tx_chn->lock); 1158a9e60cf0SGrygorii Strashko } 115993a76530SGrygorii Strashko if (ret) { 116093a76530SGrygorii Strashko dev_err(dev, "can't push desc %d\n", ret); 116193a76530SGrygorii Strashko /* inform bql */ 116293a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, 1, pkt_len); 116393a76530SGrygorii Strashko ndev->stats.tx_errors++; 116493a76530SGrygorii Strashko goto err_free_descs; 116593a76530SGrygorii Strashko } 116693a76530SGrygorii Strashko 116793a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) { 116893a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 116993a76530SGrygorii Strashko /* Barrier, so that stop_queue visible to other cpus */ 117093a76530SGrygorii Strashko smp_mb__after_atomic(); 117193a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx); 117293a76530SGrygorii Strashko 117393a76530SGrygorii Strashko /* re-check for smp */ 117493a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= 117593a76530SGrygorii Strashko MAX_SKB_FRAGS) { 117693a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 117793a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx); 117893a76530SGrygorii Strashko } 117993a76530SGrygorii Strashko } 118093a76530SGrygorii Strashko 118193a76530SGrygorii Strashko return NETDEV_TX_OK; 118293a76530SGrygorii Strashko 118393a76530SGrygorii Strashko err_free_descs: 1184ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 118593a76530SGrygorii Strashko err_free_skb: 118693a76530SGrygorii Strashko ndev->stats.tx_dropped++; 118793a76530SGrygorii Strashko dev_kfree_skb_any(skb); 118893a76530SGrygorii Strashko return NETDEV_TX_OK; 118993a76530SGrygorii Strashko 119093a76530SGrygorii Strashko busy_free_descs: 1191ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 119293a76530SGrygorii Strashko busy_stop_q: 119393a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 119493a76530SGrygorii Strashko return NETDEV_TX_BUSY; 119593a76530SGrygorii Strashko } 119693a76530SGrygorii Strashko 119793a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev, 119893a76530SGrygorii Strashko void *addr) 119993a76530SGrygorii Strashko { 120093a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 120193a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 120293a76530SGrygorii Strashko struct sockaddr *sockaddr = (struct sockaddr *)addr; 120393a76530SGrygorii Strashko int ret; 120493a76530SGrygorii Strashko 120593a76530SGrygorii Strashko ret = eth_prepare_mac_addr_change(ndev, addr); 120693a76530SGrygorii Strashko if (ret < 0) 120793a76530SGrygorii Strashko return ret; 120893a76530SGrygorii Strashko 12092240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 12102240514cSMinghao Chi if (ret < 0) 121193a76530SGrygorii Strashko return ret; 121293a76530SGrygorii Strashko 121393a76530SGrygorii Strashko cpsw_ale_del_ucast(common->ale, ndev->dev_addr, 121493a76530SGrygorii Strashko HOST_PORT_NUM, 0, 0); 121593a76530SGrygorii Strashko cpsw_ale_add_ucast(common->ale, sockaddr->sa_data, 121693a76530SGrygorii Strashko HOST_PORT_NUM, ALE_SECURE, 0); 121793a76530SGrygorii Strashko 121893a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, addr); 121993a76530SGrygorii Strashko eth_commit_mac_addr_change(ndev, sockaddr); 122093a76530SGrygorii Strashko 122193a76530SGrygorii Strashko pm_runtime_put(common->dev); 122293a76530SGrygorii Strashko 122393a76530SGrygorii Strashko return 0; 122493a76530SGrygorii Strashko } 122593a76530SGrygorii Strashko 1226b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev, 1227b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1228b1f66a5bSGrygorii Strashko { 1229b1f66a5bSGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1230b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1231b1f66a5bSGrygorii Strashko u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype; 1232b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1233b1f66a5bSGrygorii Strashko 1234b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1235b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1236b1f66a5bSGrygorii Strashko 1237b1f66a5bSGrygorii Strashko if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1238b1f66a5bSGrygorii Strashko return -EFAULT; 1239b1f66a5bSGrygorii Strashko 1240b1f66a5bSGrygorii Strashko /* TX HW timestamp */ 1241b1f66a5bSGrygorii Strashko switch (cfg.tx_type) { 1242b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_OFF: 1243b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_ON: 1244b1f66a5bSGrygorii Strashko break; 1245b1f66a5bSGrygorii Strashko default: 1246b1f66a5bSGrygorii Strashko return -ERANGE; 1247b1f66a5bSGrygorii Strashko } 1248b1f66a5bSGrygorii Strashko 1249b1f66a5bSGrygorii Strashko switch (cfg.rx_filter) { 1250b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NONE: 1251b1f66a5bSGrygorii Strashko port->rx_ts_enabled = false; 1252b1f66a5bSGrygorii Strashko break; 1253b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_ALL: 1254b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_SOME: 1255b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1256b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1257b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1258b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1259b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1260b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1261b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1262b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1263b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1264b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_EVENT: 1265b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_SYNC: 1266b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1267b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NTP_ALL: 1268b1f66a5bSGrygorii Strashko port->rx_ts_enabled = true; 1269b1f66a5bSGrygorii Strashko cfg.rx_filter = HWTSTAMP_FILTER_ALL; 1270b1f66a5bSGrygorii Strashko break; 1271b1f66a5bSGrygorii Strashko default: 1272b1f66a5bSGrygorii Strashko return -ERANGE; 1273b1f66a5bSGrygorii Strashko } 1274b1f66a5bSGrygorii Strashko 1275b1f66a5bSGrygorii Strashko port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON); 1276b1f66a5bSGrygorii Strashko 1277b1f66a5bSGrygorii Strashko /* cfg TX timestamp */ 1278b1f66a5bSGrygorii Strashko seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET << 1279b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588; 1280b1f66a5bSGrygorii Strashko 1281b1f66a5bSGrygorii Strashko ts_vlan_ltype = ETH_P_8021Q; 1282b1f66a5bSGrygorii Strashko 1283b1f66a5bSGrygorii Strashko ts_ctrl_ltype2 = ETH_P_1588 | 1284b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 | 1285b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 | 1286b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 | 1287b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 | 1288b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 | 1289b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 | 1290b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 | 1291b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO; 1292b1f66a5bSGrygorii Strashko 1293b1f66a5bSGrygorii Strashko ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS << 1294b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT; 1295b1f66a5bSGrygorii Strashko 1296b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1297b1f66a5bSGrygorii Strashko ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN | 1298b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN; 1299b1f66a5bSGrygorii Strashko 1300b1f66a5bSGrygorii Strashko writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG); 1301b1f66a5bSGrygorii Strashko writel(ts_vlan_ltype, port->port_base + 1302b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG); 1303b1f66a5bSGrygorii Strashko writel(ts_ctrl_ltype2, port->port_base + 1304b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2); 1305b1f66a5bSGrygorii Strashko writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL); 1306b1f66a5bSGrygorii Strashko 1307b1f66a5bSGrygorii Strashko /* en/dis RX timestamp */ 1308b1f66a5bSGrygorii Strashko am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled); 1309b1f66a5bSGrygorii Strashko 1310b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1311b1f66a5bSGrygorii Strashko } 1312b1f66a5bSGrygorii Strashko 1313b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev, 1314b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1315b1f66a5bSGrygorii Strashko { 1316b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1317b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1318b1f66a5bSGrygorii Strashko 1319b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1320b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1321b1f66a5bSGrygorii Strashko 1322b1f66a5bSGrygorii Strashko cfg.flags = 0; 1323b1f66a5bSGrygorii Strashko cfg.tx_type = port->tx_ts_enabled ? 1324b1f66a5bSGrygorii Strashko HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1325b1f66a5bSGrygorii Strashko cfg.rx_filter = port->rx_ts_enabled ? 1326b1f66a5bSGrygorii Strashko HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 1327b1f66a5bSGrygorii Strashko 1328b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1329b1f66a5bSGrygorii Strashko } 1330b1f66a5bSGrygorii Strashko 133193a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev, 133293a76530SGrygorii Strashko struct ifreq *req, int cmd) 133393a76530SGrygorii Strashko { 133493a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 133593a76530SGrygorii Strashko 133693a76530SGrygorii Strashko if (!netif_running(ndev)) 133793a76530SGrygorii Strashko return -EINVAL; 133893a76530SGrygorii Strashko 1339b1f66a5bSGrygorii Strashko switch (cmd) { 1340b1f66a5bSGrygorii Strashko case SIOCSHWTSTAMP: 1341b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_set(ndev, req); 1342b1f66a5bSGrygorii Strashko case SIOCGHWTSTAMP: 1343b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_get(ndev, req); 1344b1f66a5bSGrygorii Strashko } 1345b1f66a5bSGrygorii Strashko 1346e8609e69SSiddharth Vadapalli return phylink_mii_ioctl(port->slave.phylink, req, cmd); 134793a76530SGrygorii Strashko } 134893a76530SGrygorii Strashko 134993a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev, 135093a76530SGrygorii Strashko struct rtnl_link_stats64 *stats) 135193a76530SGrygorii Strashko { 135293a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev); 135393a76530SGrygorii Strashko unsigned int start; 135493a76530SGrygorii Strashko int cpu; 135593a76530SGrygorii Strashko 135693a76530SGrygorii Strashko for_each_possible_cpu(cpu) { 135793a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *cpu_stats; 135893a76530SGrygorii Strashko u64 rx_packets; 135993a76530SGrygorii Strashko u64 rx_bytes; 136093a76530SGrygorii Strashko u64 tx_packets; 136193a76530SGrygorii Strashko u64 tx_bytes; 136293a76530SGrygorii Strashko 136393a76530SGrygorii Strashko cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu); 136493a76530SGrygorii Strashko do { 1365*068c38adSThomas Gleixner start = u64_stats_fetch_begin(&cpu_stats->syncp); 136693a76530SGrygorii Strashko rx_packets = cpu_stats->rx_packets; 136793a76530SGrygorii Strashko rx_bytes = cpu_stats->rx_bytes; 136893a76530SGrygorii Strashko tx_packets = cpu_stats->tx_packets; 136993a76530SGrygorii Strashko tx_bytes = cpu_stats->tx_bytes; 1370*068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&cpu_stats->syncp, start)); 137193a76530SGrygorii Strashko 137293a76530SGrygorii Strashko stats->rx_packets += rx_packets; 137393a76530SGrygorii Strashko stats->rx_bytes += rx_bytes; 137493a76530SGrygorii Strashko stats->tx_packets += tx_packets; 137593a76530SGrygorii Strashko stats->tx_bytes += tx_bytes; 137693a76530SGrygorii Strashko } 137793a76530SGrygorii Strashko 137893a76530SGrygorii Strashko stats->rx_errors = dev->stats.rx_errors; 137993a76530SGrygorii Strashko stats->rx_dropped = dev->stats.rx_dropped; 138093a76530SGrygorii Strashko stats->tx_dropped = dev->stats.tx_dropped; 138193a76530SGrygorii Strashko } 138293a76530SGrygorii Strashko 138358356eb3SVignesh Raghavendra static struct devlink_port *am65_cpsw_ndo_get_devlink_port(struct net_device *ndev) 138458356eb3SVignesh Raghavendra { 138558356eb3SVignesh Raghavendra struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 138658356eb3SVignesh Raghavendra 138758356eb3SVignesh Raghavendra return &port->devlink_port; 138858356eb3SVignesh Raghavendra } 138958356eb3SVignesh Raghavendra 139084b4aa49SGrygorii Strashko static const struct net_device_ops am65_cpsw_nuss_netdev_ops = { 139193a76530SGrygorii Strashko .ndo_open = am65_cpsw_nuss_ndo_slave_open, 139293a76530SGrygorii Strashko .ndo_stop = am65_cpsw_nuss_ndo_slave_stop, 139393a76530SGrygorii Strashko .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit, 139493a76530SGrygorii Strashko .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode, 139593a76530SGrygorii Strashko .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats, 139693a76530SGrygorii Strashko .ndo_validate_addr = eth_validate_addr, 139793a76530SGrygorii Strashko .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address, 139893a76530SGrygorii Strashko .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout, 139993a76530SGrygorii Strashko .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid, 140093a76530SGrygorii Strashko .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid, 1401a7605370SArnd Bergmann .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl, 14028127224cSIvan Khoronzhuk .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc, 140358356eb3SVignesh Raghavendra .ndo_get_devlink_port = am65_cpsw_ndo_get_devlink_port, 140493a76530SGrygorii Strashko }; 140593a76530SGrygorii Strashko 1406e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode, 1407e8609e69SSiddharth Vadapalli const struct phylink_link_state *state) 1408e8609e69SSiddharth Vadapalli { 140937184fc1SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 141037184fc1SSiddharth Vadapalli phylink_config); 141137184fc1SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 141237184fc1SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 141337184fc1SSiddharth Vadapalli 141437184fc1SSiddharth Vadapalli if (common->pdata.extra_modes & BIT(state->interface)) 141537184fc1SSiddharth Vadapalli writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE, 141637184fc1SSiddharth Vadapalli port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG); 1417e8609e69SSiddharth Vadapalli } 1418e8609e69SSiddharth Vadapalli 1419e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode, 1420e8609e69SSiddharth Vadapalli phy_interface_t interface) 1421e8609e69SSiddharth Vadapalli { 1422e8609e69SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1423e8609e69SSiddharth Vadapalli phylink_config); 1424e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1425e8609e69SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 1426e8609e69SSiddharth Vadapalli struct net_device *ndev = port->ndev; 1427e8609e69SSiddharth Vadapalli int tmo; 1428e8609e69SSiddharth Vadapalli 1429e8609e69SSiddharth Vadapalli /* disable forwarding */ 1430e8609e69SSiddharth Vadapalli cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1431e8609e69SSiddharth Vadapalli 1432e8609e69SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 1433e8609e69SSiddharth Vadapalli 1434e8609e69SSiddharth Vadapalli tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 1435e8609e69SSiddharth Vadapalli dev_dbg(common->dev, "down msc_sl %08x tmo %d\n", 1436e8609e69SSiddharth Vadapalli cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo); 1437e8609e69SSiddharth Vadapalli 1438e8609e69SSiddharth Vadapalli cpsw_sl_ctl_reset(port->slave.mac_sl); 1439e8609e69SSiddharth Vadapalli 1440e8609e69SSiddharth Vadapalli am65_cpsw_qos_link_down(ndev); 1441e8609e69SSiddharth Vadapalli netif_tx_stop_all_queues(ndev); 1442e8609e69SSiddharth Vadapalli } 1443e8609e69SSiddharth Vadapalli 1444e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy, 1445e8609e69SSiddharth Vadapalli unsigned int mode, phy_interface_t interface, int speed, 1446e8609e69SSiddharth Vadapalli int duplex, bool tx_pause, bool rx_pause) 1447e8609e69SSiddharth Vadapalli { 1448e8609e69SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1449e8609e69SSiddharth Vadapalli phylink_config); 1450e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1451e8609e69SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 1452e8609e69SSiddharth Vadapalli u32 mac_control = CPSW_SL_CTL_GMII_EN; 1453e8609e69SSiddharth Vadapalli struct net_device *ndev = port->ndev; 1454e8609e69SSiddharth Vadapalli 1455e8609e69SSiddharth Vadapalli if (speed == SPEED_1000) 1456e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_GIG; 1457e8609e69SSiddharth Vadapalli if (speed == SPEED_10 && interface == PHY_INTERFACE_MODE_RGMII) 1458e8609e69SSiddharth Vadapalli /* Can be used with in band mode only */ 1459e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_EXT_EN; 1460e8609e69SSiddharth Vadapalli if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII) 1461e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_IFCTL_A; 1462e8609e69SSiddharth Vadapalli if (duplex) 1463e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_FULLDUPLEX; 1464e8609e69SSiddharth Vadapalli 1465e8609e69SSiddharth Vadapalli /* rx_pause/tx_pause */ 1466e8609e69SSiddharth Vadapalli if (rx_pause) 1467e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_RX_FLOW_EN; 1468e8609e69SSiddharth Vadapalli 1469e8609e69SSiddharth Vadapalli if (tx_pause) 1470e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_TX_FLOW_EN; 1471e8609e69SSiddharth Vadapalli 1472e8609e69SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, mac_control); 1473e8609e69SSiddharth Vadapalli 1474e8609e69SSiddharth Vadapalli /* enable forwarding */ 1475e8609e69SSiddharth Vadapalli cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1476e8609e69SSiddharth Vadapalli 1477e8609e69SSiddharth Vadapalli am65_cpsw_qos_link_up(ndev, speed); 1478e8609e69SSiddharth Vadapalli netif_tx_wake_all_queues(ndev); 1479e8609e69SSiddharth Vadapalli } 1480e8609e69SSiddharth Vadapalli 1481e8609e69SSiddharth Vadapalli static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = { 1482e8609e69SSiddharth Vadapalli .validate = phylink_generic_validate, 1483e8609e69SSiddharth Vadapalli .mac_config = am65_cpsw_nuss_mac_config, 1484e8609e69SSiddharth Vadapalli .mac_link_down = am65_cpsw_nuss_mac_link_down, 1485e8609e69SSiddharth Vadapalli .mac_link_up = am65_cpsw_nuss_mac_link_up, 1486e8609e69SSiddharth Vadapalli }; 1487e8609e69SSiddharth Vadapalli 148893a76530SGrygorii Strashko static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port) 148993a76530SGrygorii Strashko { 149093a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 149193a76530SGrygorii Strashko 149293a76530SGrygorii Strashko if (!port->disabled) 149393a76530SGrygorii Strashko return; 149493a76530SGrygorii Strashko 149593a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 149693a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 149793a76530SGrygorii Strashko 149893a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 149993a76530SGrygorii Strashko cpsw_sl_ctl_reset(port->slave.mac_sl); 150093a76530SGrygorii Strashko } 150193a76530SGrygorii Strashko 150293a76530SGrygorii Strashko static void am65_cpsw_nuss_free_tx_chns(void *data) 150393a76530SGrygorii Strashko { 150493a76530SGrygorii Strashko struct am65_cpsw_common *common = data; 150593a76530SGrygorii Strashko int i; 150693a76530SGrygorii Strashko 150793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 150893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 150993a76530SGrygorii Strashko 151093a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 151193a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 151293a76530SGrygorii Strashko 1513ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1514ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1515ce8eb4c7SVignesh Raghavendra 151693a76530SGrygorii Strashko memset(tx_chn, 0, sizeof(*tx_chn)); 151793a76530SGrygorii Strashko } 151893a76530SGrygorii Strashko } 151993a76530SGrygorii Strashko 152093a76530SGrygorii Strashko void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common) 152193a76530SGrygorii Strashko { 152293a76530SGrygorii Strashko struct device *dev = common->dev; 152393a76530SGrygorii Strashko int i; 152493a76530SGrygorii Strashko 152593a76530SGrygorii Strashko devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common); 152693a76530SGrygorii Strashko 152793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 152893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 152993a76530SGrygorii Strashko 153093a76530SGrygorii Strashko if (tx_chn->irq) 153193a76530SGrygorii Strashko devm_free_irq(dev, tx_chn->irq, tx_chn); 153293a76530SGrygorii Strashko 153393a76530SGrygorii Strashko netif_napi_del(&tx_chn->napi_tx); 153493a76530SGrygorii Strashko 153593a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 153693a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 153793a76530SGrygorii Strashko 1538ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1539ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1540ce8eb4c7SVignesh Raghavendra 154193a76530SGrygorii Strashko memset(tx_chn, 0, sizeof(*tx_chn)); 154293a76530SGrygorii Strashko } 154393a76530SGrygorii Strashko } 154493a76530SGrygorii Strashko 154593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common) 154693a76530SGrygorii Strashko { 154793a76530SGrygorii Strashko u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS); 154893a76530SGrygorii Strashko struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 }; 154993a76530SGrygorii Strashko struct device *dev = common->dev; 155093a76530SGrygorii Strashko struct k3_ring_cfg ring_cfg = { 155193a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 155293a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 155393a76530SGrygorii Strashko .flags = 0 155493a76530SGrygorii Strashko }; 155593a76530SGrygorii Strashko u32 hdesc_size; 155693a76530SGrygorii Strashko int i, ret = 0; 155793a76530SGrygorii Strashko 155893a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 155993a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 156093a76530SGrygorii Strashko 156193a76530SGrygorii Strashko tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 156293a76530SGrygorii Strashko tx_cfg.tx_cfg = ring_cfg; 156393a76530SGrygorii Strashko tx_cfg.txcq_cfg = ring_cfg; 156493a76530SGrygorii Strashko tx_cfg.tx_cfg.size = max_desc_num; 156593a76530SGrygorii Strashko tx_cfg.txcq_cfg.size = max_desc_num; 156693a76530SGrygorii Strashko 156793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 156893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 156993a76530SGrygorii Strashko 157093a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 157193a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "tx%d", i); 157293a76530SGrygorii Strashko 1573a9e60cf0SGrygorii Strashko spin_lock_init(&tx_chn->lock); 157493a76530SGrygorii Strashko tx_chn->common = common; 157593a76530SGrygorii Strashko tx_chn->id = i; 157693a76530SGrygorii Strashko tx_chn->descs_num = max_desc_num; 157793a76530SGrygorii Strashko 157893a76530SGrygorii Strashko tx_chn->tx_chn = 157993a76530SGrygorii Strashko k3_udma_glue_request_tx_chn(dev, 158093a76530SGrygorii Strashko tx_chn->tx_chn_name, 158193a76530SGrygorii Strashko &tx_cfg); 158293a76530SGrygorii Strashko if (IS_ERR(tx_chn->tx_chn)) { 15838fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn), 15848fbc2f9eSGrygorii Strashko "Failed to request tx dma channel\n"); 158593a76530SGrygorii Strashko goto err; 158693a76530SGrygorii Strashko } 1587ed569ed9SPeter Ujfalusi tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn); 1588ed569ed9SPeter Ujfalusi 1589ed569ed9SPeter Ujfalusi tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev, 1590ed569ed9SPeter Ujfalusi tx_chn->descs_num, 1591ed569ed9SPeter Ujfalusi hdesc_size, 1592ed569ed9SPeter Ujfalusi tx_chn->tx_chn_name); 1593ed569ed9SPeter Ujfalusi if (IS_ERR(tx_chn->desc_pool)) { 1594ed569ed9SPeter Ujfalusi ret = PTR_ERR(tx_chn->desc_pool); 1595ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create poll %d\n", ret); 1596ed569ed9SPeter Ujfalusi goto err; 1597ed569ed9SPeter Ujfalusi } 159893a76530SGrygorii Strashko 159993a76530SGrygorii Strashko tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn); 160093a76530SGrygorii Strashko if (tx_chn->irq <= 0) { 160193a76530SGrygorii Strashko dev_err(dev, "Failed to get tx dma irq %d\n", 160293a76530SGrygorii Strashko tx_chn->irq); 160393a76530SGrygorii Strashko goto err; 160493a76530SGrygorii Strashko } 160593a76530SGrygorii Strashko 160693a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 160793a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "%s-tx%d", 160893a76530SGrygorii Strashko dev_name(dev), tx_chn->id); 160993a76530SGrygorii Strashko } 161093a76530SGrygorii Strashko 161193a76530SGrygorii Strashko err: 161293a76530SGrygorii Strashko i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common); 161393a76530SGrygorii Strashko if (i) { 1614e6aaeafdSChristophe JAILLET dev_err(dev, "Failed to add free_tx_chns action %d\n", i); 161593a76530SGrygorii Strashko return i; 161693a76530SGrygorii Strashko } 161793a76530SGrygorii Strashko 161893a76530SGrygorii Strashko return ret; 161993a76530SGrygorii Strashko } 162093a76530SGrygorii Strashko 162193a76530SGrygorii Strashko static void am65_cpsw_nuss_free_rx_chns(void *data) 162293a76530SGrygorii Strashko { 162393a76530SGrygorii Strashko struct am65_cpsw_common *common = data; 162493a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn; 162593a76530SGrygorii Strashko 162693a76530SGrygorii Strashko rx_chn = &common->rx_chns; 162793a76530SGrygorii Strashko 162893a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 162993a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 1630ce8eb4c7SVignesh Raghavendra 1631ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 1632ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 163393a76530SGrygorii Strashko } 163493a76530SGrygorii Strashko 163593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common) 163693a76530SGrygorii Strashko { 163793a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 163893a76530SGrygorii Strashko struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 }; 163993a76530SGrygorii Strashko u32 max_desc_num = AM65_CPSW_MAX_RX_DESC; 164093a76530SGrygorii Strashko struct device *dev = common->dev; 164193a76530SGrygorii Strashko u32 hdesc_size; 164293a76530SGrygorii Strashko u32 fdqring_id; 164393a76530SGrygorii Strashko int i, ret = 0; 164493a76530SGrygorii Strashko 164593a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 164693a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 164793a76530SGrygorii Strashko 164893a76530SGrygorii Strashko rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 164993a76530SGrygorii Strashko rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS; 165093a76530SGrygorii Strashko rx_cfg.flow_id_base = common->rx_flow_id_base; 165193a76530SGrygorii Strashko 165293a76530SGrygorii Strashko /* init all flows */ 165393a76530SGrygorii Strashko rx_chn->dev = dev; 165493a76530SGrygorii Strashko rx_chn->descs_num = max_desc_num; 165593a76530SGrygorii Strashko 165693a76530SGrygorii Strashko rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg); 165793a76530SGrygorii Strashko if (IS_ERR(rx_chn->rx_chn)) { 16588fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn), 16598fbc2f9eSGrygorii Strashko "Failed to request rx dma channel\n"); 166093a76530SGrygorii Strashko goto err; 166193a76530SGrygorii Strashko } 1662ed569ed9SPeter Ujfalusi rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn); 1663ed569ed9SPeter Ujfalusi 1664ed569ed9SPeter Ujfalusi rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev, 1665ed569ed9SPeter Ujfalusi rx_chn->descs_num, 1666ed569ed9SPeter Ujfalusi hdesc_size, "rx"); 1667ed569ed9SPeter Ujfalusi if (IS_ERR(rx_chn->desc_pool)) { 1668ed569ed9SPeter Ujfalusi ret = PTR_ERR(rx_chn->desc_pool); 1669ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create rx poll %d\n", ret); 1670ed569ed9SPeter Ujfalusi goto err; 1671ed569ed9SPeter Ujfalusi } 167293a76530SGrygorii Strashko 167393a76530SGrygorii Strashko common->rx_flow_id_base = 167493a76530SGrygorii Strashko k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn); 167593a76530SGrygorii Strashko dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base); 167693a76530SGrygorii Strashko 167793a76530SGrygorii Strashko fdqring_id = K3_RINGACC_RING_ID_ANY; 167893a76530SGrygorii Strashko for (i = 0; i < rx_cfg.flow_id_num; i++) { 167993a76530SGrygorii Strashko struct k3_ring_cfg rxring_cfg = { 168093a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 168193a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 168293a76530SGrygorii Strashko .flags = 0, 168393a76530SGrygorii Strashko }; 168493a76530SGrygorii Strashko struct k3_ring_cfg fdqring_cfg = { 168593a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 168693a76530SGrygorii Strashko .flags = K3_RINGACC_RING_SHARED, 168793a76530SGrygorii Strashko }; 168893a76530SGrygorii Strashko struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = { 168993a76530SGrygorii Strashko .rx_cfg = rxring_cfg, 169093a76530SGrygorii Strashko .rxfdq_cfg = fdqring_cfg, 169193a76530SGrygorii Strashko .ring_rxq_id = K3_RINGACC_RING_ID_ANY, 169293a76530SGrygorii Strashko .src_tag_lo_sel = 169393a76530SGrygorii Strashko K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG, 169493a76530SGrygorii Strashko }; 169593a76530SGrygorii Strashko 169693a76530SGrygorii Strashko rx_flow_cfg.ring_rxfdq0_id = fdqring_id; 169793a76530SGrygorii Strashko rx_flow_cfg.rx_cfg.size = max_desc_num; 169893a76530SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.size = max_desc_num; 1699c6275c02SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode; 170093a76530SGrygorii Strashko 170193a76530SGrygorii Strashko ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn, 170293a76530SGrygorii Strashko i, &rx_flow_cfg); 170393a76530SGrygorii Strashko if (ret) { 170493a76530SGrygorii Strashko dev_err(dev, "Failed to init rx flow%d %d\n", i, ret); 170593a76530SGrygorii Strashko goto err; 170693a76530SGrygorii Strashko } 170793a76530SGrygorii Strashko if (!i) 170893a76530SGrygorii Strashko fdqring_id = 170993a76530SGrygorii Strashko k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn, 171093a76530SGrygorii Strashko i); 171193a76530SGrygorii Strashko 171293a76530SGrygorii Strashko rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i); 171393a76530SGrygorii Strashko 171493a76530SGrygorii Strashko if (rx_chn->irq <= 0) { 171593a76530SGrygorii Strashko dev_err(dev, "Failed to get rx dma irq %d\n", 171693a76530SGrygorii Strashko rx_chn->irq); 171793a76530SGrygorii Strashko ret = -ENXIO; 171893a76530SGrygorii Strashko goto err; 171993a76530SGrygorii Strashko } 172093a76530SGrygorii Strashko } 172193a76530SGrygorii Strashko 172293a76530SGrygorii Strashko err: 172393a76530SGrygorii Strashko i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common); 172493a76530SGrygorii Strashko if (i) { 1725e6aaeafdSChristophe JAILLET dev_err(dev, "Failed to add free_rx_chns action %d\n", i); 172693a76530SGrygorii Strashko return i; 172793a76530SGrygorii Strashko } 172893a76530SGrygorii Strashko 172993a76530SGrygorii Strashko return ret; 173093a76530SGrygorii Strashko } 173193a76530SGrygorii Strashko 173293a76530SGrygorii Strashko static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common) 173393a76530SGrygorii Strashko { 173493a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 173593a76530SGrygorii Strashko 173693a76530SGrygorii Strashko host_p->common = common; 173793a76530SGrygorii Strashko host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE; 173893a76530SGrygorii Strashko host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE; 173993a76530SGrygorii Strashko 174093a76530SGrygorii Strashko return 0; 174193a76530SGrygorii Strashko } 174293a76530SGrygorii Strashko 174393a76530SGrygorii Strashko static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node, 174493a76530SGrygorii Strashko int slave, u8 *mac_addr) 174593a76530SGrygorii Strashko { 174693a76530SGrygorii Strashko u32 mac_lo, mac_hi, offset; 174793a76530SGrygorii Strashko struct regmap *syscon; 174893a76530SGrygorii Strashko int ret; 174993a76530SGrygorii Strashko 175093a76530SGrygorii Strashko syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse"); 175193a76530SGrygorii Strashko if (IS_ERR(syscon)) { 175293a76530SGrygorii Strashko if (PTR_ERR(syscon) == -ENODEV) 175393a76530SGrygorii Strashko return 0; 175493a76530SGrygorii Strashko return PTR_ERR(syscon); 175593a76530SGrygorii Strashko } 175693a76530SGrygorii Strashko 175793a76530SGrygorii Strashko ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1, 175893a76530SGrygorii Strashko &offset); 175993a76530SGrygorii Strashko if (ret) 176093a76530SGrygorii Strashko return ret; 176193a76530SGrygorii Strashko 176293a76530SGrygorii Strashko regmap_read(syscon, offset, &mac_lo); 176393a76530SGrygorii Strashko regmap_read(syscon, offset + 4, &mac_hi); 176493a76530SGrygorii Strashko 176593a76530SGrygorii Strashko mac_addr[0] = (mac_hi >> 8) & 0xff; 176693a76530SGrygorii Strashko mac_addr[1] = mac_hi & 0xff; 176793a76530SGrygorii Strashko mac_addr[2] = (mac_lo >> 24) & 0xff; 176893a76530SGrygorii Strashko mac_addr[3] = (mac_lo >> 16) & 0xff; 176993a76530SGrygorii Strashko mac_addr[4] = (mac_lo >> 8) & 0xff; 177093a76530SGrygorii Strashko mac_addr[5] = mac_lo & 0xff; 177193a76530SGrygorii Strashko 177293a76530SGrygorii Strashko return 0; 177393a76530SGrygorii Strashko } 177493a76530SGrygorii Strashko 1775b1f66a5bSGrygorii Strashko static int am65_cpsw_init_cpts(struct am65_cpsw_common *common) 1776b1f66a5bSGrygorii Strashko { 1777b1f66a5bSGrygorii Strashko struct device *dev = common->dev; 1778b1f66a5bSGrygorii Strashko struct device_node *node; 1779b1f66a5bSGrygorii Strashko struct am65_cpts *cpts; 1780b1f66a5bSGrygorii Strashko void __iomem *reg_base; 1781b1f66a5bSGrygorii Strashko 1782b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1783b1f66a5bSGrygorii Strashko return 0; 1784b1f66a5bSGrygorii Strashko 1785b1f66a5bSGrygorii Strashko node = of_get_child_by_name(dev->of_node, "cpts"); 1786b1f66a5bSGrygorii Strashko if (!node) { 1787b1f66a5bSGrygorii Strashko dev_err(dev, "%s cpts not found\n", __func__); 1788b1f66a5bSGrygorii Strashko return -ENOENT; 1789b1f66a5bSGrygorii Strashko } 1790b1f66a5bSGrygorii Strashko 1791b1f66a5bSGrygorii Strashko reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE; 1792b1f66a5bSGrygorii Strashko cpts = am65_cpts_create(dev, reg_base, node); 1793b1f66a5bSGrygorii Strashko if (IS_ERR(cpts)) { 1794b1f66a5bSGrygorii Strashko int ret = PTR_ERR(cpts); 1795b1f66a5bSGrygorii Strashko 17965dd89d2fSMiaoqian Lin of_node_put(node); 1797b1f66a5bSGrygorii Strashko if (ret == -EOPNOTSUPP) { 1798b1f66a5bSGrygorii Strashko dev_info(dev, "cpts disabled\n"); 1799b1f66a5bSGrygorii Strashko return 0; 1800b1f66a5bSGrygorii Strashko } 1801b1f66a5bSGrygorii Strashko 1802b1f66a5bSGrygorii Strashko dev_err(dev, "cpts create err %d\n", ret); 1803b1f66a5bSGrygorii Strashko return ret; 1804b1f66a5bSGrygorii Strashko } 1805b1f66a5bSGrygorii Strashko common->cpts = cpts; 1806a9c74700SGrygorii Strashko /* Forbid PM runtime if CPTS is running. 1807a9c74700SGrygorii Strashko * K3 CPSWxG modules may completely lose context during ON->OFF 1808a9c74700SGrygorii Strashko * transitions depending on integration. 1809a9c74700SGrygorii Strashko * AM65x/J721E MCU CPSW2G: false 1810a9c74700SGrygorii Strashko * J721E MAIN_CPSW9G: true 1811a9c74700SGrygorii Strashko */ 1812a9c74700SGrygorii Strashko pm_runtime_forbid(dev); 1813b1f66a5bSGrygorii Strashko 1814b1f66a5bSGrygorii Strashko return 0; 1815b1f66a5bSGrygorii Strashko } 1816b1f66a5bSGrygorii Strashko 181793a76530SGrygorii Strashko static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common) 181893a76530SGrygorii Strashko { 181993a76530SGrygorii Strashko struct device_node *node, *port_np; 182093a76530SGrygorii Strashko struct device *dev = common->dev; 182193a76530SGrygorii Strashko int ret; 182293a76530SGrygorii Strashko 182393a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 182493a76530SGrygorii Strashko if (!node) 182593a76530SGrygorii Strashko return -ENOENT; 182693a76530SGrygorii Strashko 182793a76530SGrygorii Strashko for_each_child_of_node(node, port_np) { 182893a76530SGrygorii Strashko struct am65_cpsw_port *port; 182993a76530SGrygorii Strashko u32 port_id; 183093a76530SGrygorii Strashko 183193a76530SGrygorii Strashko /* it is not a slave port node, continue */ 183293a76530SGrygorii Strashko if (strcmp(port_np->name, "port")) 183393a76530SGrygorii Strashko continue; 183493a76530SGrygorii Strashko 183593a76530SGrygorii Strashko ret = of_property_read_u32(port_np, "reg", &port_id); 183693a76530SGrygorii Strashko if (ret < 0) { 183793a76530SGrygorii Strashko dev_err(dev, "%pOF error reading port_id %d\n", 183893a76530SGrygorii Strashko port_np, ret); 1839be565ec7SWang Qing goto of_node_put; 184093a76530SGrygorii Strashko } 184193a76530SGrygorii Strashko 184293a76530SGrygorii Strashko if (!port_id || port_id > common->port_num) { 184393a76530SGrygorii Strashko dev_err(dev, "%pOF has invalid port_id %u %s\n", 184493a76530SGrygorii Strashko port_np, port_id, port_np->name); 1845be565ec7SWang Qing ret = -EINVAL; 1846be565ec7SWang Qing goto of_node_put; 184793a76530SGrygorii Strashko } 184893a76530SGrygorii Strashko 184993a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 185093a76530SGrygorii Strashko port->port_id = port_id; 185193a76530SGrygorii Strashko port->common = common; 185293a76530SGrygorii Strashko port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE + 185393a76530SGrygorii Strashko AM65_CPSW_NU_PORTS_OFFSET * (port_id); 185437184fc1SSiddharth Vadapalli if (common->pdata.extra_modes) 185537184fc1SSiddharth Vadapalli port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id); 185693a76530SGrygorii Strashko port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE + 185793a76530SGrygorii Strashko (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id); 185893a76530SGrygorii Strashko port->name = of_get_property(port_np, "label", NULL); 18598127224cSIvan Khoronzhuk port->fetch_ram_base = 18608127224cSIvan Khoronzhuk common->cpsw_base + AM65_CPSW_NU_FRAM_BASE + 18618127224cSIvan Khoronzhuk (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1)); 186293a76530SGrygorii Strashko 1863d6d0aeafSGrygorii Strashko port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base); 1864be565ec7SWang Qing if (IS_ERR(port->slave.mac_sl)) { 1865be565ec7SWang Qing ret = PTR_ERR(port->slave.mac_sl); 1866be565ec7SWang Qing goto of_node_put; 1867be565ec7SWang Qing } 1868d6d0aeafSGrygorii Strashko 186993a76530SGrygorii Strashko port->disabled = !of_device_is_available(port_np); 187084b4aa49SGrygorii Strashko if (port->disabled) { 187184b4aa49SGrygorii Strashko common->disabled_ports_mask |= BIT(port->port_id); 187293a76530SGrygorii Strashko continue; 187384b4aa49SGrygorii Strashko } 187493a76530SGrygorii Strashko 187593a76530SGrygorii Strashko port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL); 187693a76530SGrygorii Strashko if (IS_ERR(port->slave.ifphy)) { 187793a76530SGrygorii Strashko ret = PTR_ERR(port->slave.ifphy); 187893a76530SGrygorii Strashko dev_err(dev, "%pOF error retrieving port phy: %d\n", 187993a76530SGrygorii Strashko port_np, ret); 1880be565ec7SWang Qing goto of_node_put; 188193a76530SGrygorii Strashko } 188293a76530SGrygorii Strashko 188393a76530SGrygorii Strashko port->slave.mac_only = 188493a76530SGrygorii Strashko of_property_read_bool(port_np, "ti,mac-only"); 188593a76530SGrygorii Strashko 188693a76530SGrygorii Strashko /* get phy/link info */ 1887e8609e69SSiddharth Vadapalli port->slave.phy_node = port_np; 188893a76530SGrygorii Strashko ret = of_get_phy_mode(port_np, &port->slave.phy_if); 188993a76530SGrygorii Strashko if (ret) { 189093a76530SGrygorii Strashko dev_err(dev, "%pOF read phy-mode err %d\n", 189193a76530SGrygorii Strashko port_np, ret); 1892be565ec7SWang Qing goto of_node_put; 189393a76530SGrygorii Strashko } 189493a76530SGrygorii Strashko 1895763015a7SSiddharth Vadapalli ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if); 1896763015a7SSiddharth Vadapalli if (ret) 1897763015a7SSiddharth Vadapalli goto of_node_put; 1898763015a7SSiddharth Vadapalli 189983216e39SMichael Walle ret = of_get_mac_address(port_np, port->slave.mac_addr); 190083216e39SMichael Walle if (ret) { 190183216e39SMichael Walle am65_cpsw_am654_get_efuse_macid(port_np, 190293a76530SGrygorii Strashko port->port_id, 190383216e39SMichael Walle port->slave.mac_addr); 190483216e39SMichael Walle if (!is_valid_ether_addr(port->slave.mac_addr)) { 1905ba530feaSJakub Kicinski eth_random_addr(port->slave.mac_addr); 190676dcbd23SColin Ian King dev_err(dev, "Use random MAC address\n"); 190793a76530SGrygorii Strashko } 190893a76530SGrygorii Strashko } 190983216e39SMichael Walle } 191093a76530SGrygorii Strashko of_node_put(node); 191193a76530SGrygorii Strashko 191284b4aa49SGrygorii Strashko /* is there at least one ext.port */ 191384b4aa49SGrygorii Strashko if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) { 191484b4aa49SGrygorii Strashko dev_err(dev, "No Ext. port are available\n"); 191584b4aa49SGrygorii Strashko return -ENODEV; 191684b4aa49SGrygorii Strashko } 191784b4aa49SGrygorii Strashko 191893a76530SGrygorii Strashko return 0; 1919be565ec7SWang Qing 1920be565ec7SWang Qing of_node_put: 1921be565ec7SWang Qing of_node_put(port_np); 1922be565ec7SWang Qing of_node_put(node); 1923be565ec7SWang Qing return ret; 192493a76530SGrygorii Strashko } 192593a76530SGrygorii Strashko 192693a76530SGrygorii Strashko static void am65_cpsw_pcpu_stats_free(void *data) 192793a76530SGrygorii Strashko { 192893a76530SGrygorii Strashko struct am65_cpsw_ndev_stats __percpu *stats = data; 192993a76530SGrygorii Strashko 193093a76530SGrygorii Strashko free_percpu(stats); 193193a76530SGrygorii Strashko } 193293a76530SGrygorii Strashko 1933e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common) 1934e8609e69SSiddharth Vadapalli { 1935e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port; 1936e8609e69SSiddharth Vadapalli int i; 1937e8609e69SSiddharth Vadapalli 1938e8609e69SSiddharth Vadapalli for (i = 0; i < common->port_num; i++) { 1939e8609e69SSiddharth Vadapalli port = &common->ports[i]; 1940e8609e69SSiddharth Vadapalli if (port->slave.phylink) 1941e8609e69SSiddharth Vadapalli phylink_destroy(port->slave.phylink); 1942e8609e69SSiddharth Vadapalli } 1943e8609e69SSiddharth Vadapalli } 1944e8609e69SSiddharth Vadapalli 194584b4aa49SGrygorii Strashko static int 194684b4aa49SGrygorii Strashko am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx) 194793a76530SGrygorii Strashko { 194893a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 194993a76530SGrygorii Strashko struct device *dev = common->dev; 195093a76530SGrygorii Strashko struct am65_cpsw_port *port; 1951e8609e69SSiddharth Vadapalli struct phylink *phylink; 195293a76530SGrygorii Strashko int ret; 195393a76530SGrygorii Strashko 195484b4aa49SGrygorii Strashko port = &common->ports[port_idx]; 195584b4aa49SGrygorii Strashko 195684b4aa49SGrygorii Strashko if (port->disabled) 195784b4aa49SGrygorii Strashko return 0; 195893a76530SGrygorii Strashko 195993a76530SGrygorii Strashko /* alloc netdev */ 196093a76530SGrygorii Strashko port->ndev = devm_alloc_etherdev_mqs(common->dev, 196193a76530SGrygorii Strashko sizeof(struct am65_cpsw_ndev_priv), 196293a76530SGrygorii Strashko AM65_CPSW_MAX_TX_QUEUES, 196393a76530SGrygorii Strashko AM65_CPSW_MAX_RX_QUEUES); 196493a76530SGrygorii Strashko if (!port->ndev) { 196593a76530SGrygorii Strashko dev_err(dev, "error allocating slave net_device %u\n", 196693a76530SGrygorii Strashko port->port_id); 196793a76530SGrygorii Strashko return -ENOMEM; 196893a76530SGrygorii Strashko } 196993a76530SGrygorii Strashko 197093a76530SGrygorii Strashko ndev_priv = netdev_priv(port->ndev); 197193a76530SGrygorii Strashko ndev_priv->port = port; 197293a76530SGrygorii Strashko ndev_priv->msg_enable = AM65_CPSW_DEBUG; 197393a76530SGrygorii Strashko SET_NETDEV_DEV(port->ndev, dev); 197493a76530SGrygorii Strashko 1975f3956ebbSJakub Kicinski eth_hw_addr_set(port->ndev, port->slave.mac_addr); 197693a76530SGrygorii Strashko 197793a76530SGrygorii Strashko port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE; 197893a76530SGrygorii Strashko port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE; 197993a76530SGrygorii Strashko port->ndev->hw_features = NETIF_F_SG | 198093a76530SGrygorii Strashko NETIF_F_RXCSUM | 19812c4dc314SMurali Karicheri NETIF_F_HW_CSUM | 19822c4dc314SMurali Karicheri NETIF_F_HW_TC; 198393a76530SGrygorii Strashko port->ndev->features = port->ndev->hw_features | 198493a76530SGrygorii Strashko NETIF_F_HW_VLAN_CTAG_FILTER; 198593a76530SGrygorii Strashko port->ndev->vlan_features |= NETIF_F_SG; 198684b4aa49SGrygorii Strashko port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops; 198793a76530SGrygorii Strashko port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave; 198893a76530SGrygorii Strashko 1989e8609e69SSiddharth Vadapalli /* Configuring Phylink */ 1990e8609e69SSiddharth Vadapalli port->slave.phylink_config.dev = &port->ndev->dev; 1991e8609e69SSiddharth Vadapalli port->slave.phylink_config.type = PHYLINK_NETDEV; 1992e8609e69SSiddharth Vadapalli port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000FD; 1993e8609e69SSiddharth Vadapalli 199437184fc1SSiddharth Vadapalli if (phy_interface_mode_is_rgmii(port->slave.phy_if)) { 1995e8609e69SSiddharth Vadapalli phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces); 199637184fc1SSiddharth Vadapalli } else if (port->slave.phy_if == PHY_INTERFACE_MODE_RMII) { 199737184fc1SSiddharth Vadapalli __set_bit(PHY_INTERFACE_MODE_RMII, 199837184fc1SSiddharth Vadapalli port->slave.phylink_config.supported_interfaces); 199937184fc1SSiddharth Vadapalli } else if (common->pdata.extra_modes & BIT(port->slave.phy_if)) { 200037184fc1SSiddharth Vadapalli __set_bit(PHY_INTERFACE_MODE_QSGMII, 200137184fc1SSiddharth Vadapalli port->slave.phylink_config.supported_interfaces); 200237184fc1SSiddharth Vadapalli } else { 200337184fc1SSiddharth Vadapalli dev_err(dev, "selected phy-mode is not supported\n"); 200437184fc1SSiddharth Vadapalli return -EOPNOTSUPP; 200537184fc1SSiddharth Vadapalli } 2006e8609e69SSiddharth Vadapalli 20070b718007SSiddharth Vadapalli phylink = phylink_create(&port->slave.phylink_config, 20080b718007SSiddharth Vadapalli of_node_to_fwnode(port->slave.phy_node), 20090b718007SSiddharth Vadapalli port->slave.phy_if, 2010e8609e69SSiddharth Vadapalli &am65_cpsw_phylink_mac_ops); 2011e8609e69SSiddharth Vadapalli if (IS_ERR(phylink)) 2012e8609e69SSiddharth Vadapalli return PTR_ERR(phylink); 2013e8609e69SSiddharth Vadapalli 2014e8609e69SSiddharth Vadapalli port->slave.phylink = phylink; 2015e8609e69SSiddharth Vadapalli 201693a76530SGrygorii Strashko /* Disable TX checksum offload by default due to HW bug */ 201738389aa6SGrygorii Strashko if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM) 201893a76530SGrygorii Strashko port->ndev->features &= ~NETIF_F_HW_CSUM; 201993a76530SGrygorii Strashko 202093a76530SGrygorii Strashko ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats); 202193a76530SGrygorii Strashko if (!ndev_priv->stats) 202293a76530SGrygorii Strashko return -ENOMEM; 202393a76530SGrygorii Strashko 202493a76530SGrygorii Strashko ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free, 202593a76530SGrygorii Strashko ndev_priv->stats); 202684b4aa49SGrygorii Strashko if (ret) 202784b4aa49SGrygorii Strashko dev_err(dev, "failed to add percpu stat free action %d\n", ret); 202884b4aa49SGrygorii Strashko 202984b4aa49SGrygorii Strashko if (!common->dma_ndev) 203084b4aa49SGrygorii Strashko common->dma_ndev = port->ndev; 203184b4aa49SGrygorii Strashko 203293a76530SGrygorii Strashko return ret; 203393a76530SGrygorii Strashko } 203493a76530SGrygorii Strashko 203584b4aa49SGrygorii Strashko static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common) 203684b4aa49SGrygorii Strashko { 203784b4aa49SGrygorii Strashko int ret; 203884b4aa49SGrygorii Strashko int i; 203984b4aa49SGrygorii Strashko 204084b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 204184b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_port_ndev(common, i); 204284b4aa49SGrygorii Strashko if (ret) 204384b4aa49SGrygorii Strashko return ret; 204484b4aa49SGrygorii Strashko } 204584b4aa49SGrygorii Strashko 204684b4aa49SGrygorii Strashko netif_napi_add(common->dma_ndev, &common->napi_rx, 2047b48b89f9SJakub Kicinski am65_cpsw_nuss_rx_poll); 204893a76530SGrygorii Strashko 204993a76530SGrygorii Strashko return ret; 205093a76530SGrygorii Strashko } 205193a76530SGrygorii Strashko 205284b4aa49SGrygorii Strashko static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common) 205393a76530SGrygorii Strashko { 205493a76530SGrygorii Strashko struct device *dev = common->dev; 205593a76530SGrygorii Strashko int i, ret = 0; 205693a76530SGrygorii Strashko 205793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 205893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 205993a76530SGrygorii Strashko 206016d083e2SJakub Kicinski netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx, 206116d083e2SJakub Kicinski am65_cpsw_nuss_tx_poll); 206293a76530SGrygorii Strashko 206393a76530SGrygorii Strashko ret = devm_request_irq(dev, tx_chn->irq, 206493a76530SGrygorii Strashko am65_cpsw_nuss_tx_irq, 20656f5c27f9SGrygorii Strashko IRQF_TRIGGER_HIGH, 20666f5c27f9SGrygorii Strashko tx_chn->tx_chn_name, tx_chn); 206793a76530SGrygorii Strashko if (ret) { 206893a76530SGrygorii Strashko dev_err(dev, "failure requesting tx%u irq %u, %d\n", 206993a76530SGrygorii Strashko tx_chn->id, tx_chn->irq, ret); 207093a76530SGrygorii Strashko goto err; 207193a76530SGrygorii Strashko } 207293a76530SGrygorii Strashko } 207393a76530SGrygorii Strashko 207493a76530SGrygorii Strashko err: 207593a76530SGrygorii Strashko return ret; 207693a76530SGrygorii Strashko } 207793a76530SGrygorii Strashko 207884b4aa49SGrygorii Strashko static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common) 207984b4aa49SGrygorii Strashko { 208084b4aa49SGrygorii Strashko struct am65_cpsw_port *port; 208184b4aa49SGrygorii Strashko int i; 208284b4aa49SGrygorii Strashko 208384b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 208484b4aa49SGrygorii Strashko port = &common->ports[i]; 208584b4aa49SGrygorii Strashko if (port->ndev) 208684b4aa49SGrygorii Strashko unregister_netdev(port->ndev); 208784b4aa49SGrygorii Strashko } 208884b4aa49SGrygorii Strashko } 208984b4aa49SGrygorii Strashko 20902934db9bSVignesh Raghavendra static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common) 20912934db9bSVignesh Raghavendra { 20922934db9bSVignesh Raghavendra int set_val = 0; 20932934db9bSVignesh Raghavendra int i; 20942934db9bSVignesh Raghavendra 20952934db9bSVignesh Raghavendra if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask)) 20962934db9bSVignesh Raghavendra set_val = 1; 20972934db9bSVignesh Raghavendra 20982934db9bSVignesh Raghavendra dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val); 20992934db9bSVignesh Raghavendra 21002934db9bSVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 21012934db9bSVignesh Raghavendra struct am65_cpsw_port *port = am65_common_get_port(common, i); 2102ae03d189SGrygorii Strashko struct am65_cpsw_ndev_priv *priv; 21032934db9bSVignesh Raghavendra 2104ae03d189SGrygorii Strashko if (!port->ndev) 2105ae03d189SGrygorii Strashko continue; 2106ae03d189SGrygorii Strashko 2107ae03d189SGrygorii Strashko priv = am65_ndev_to_priv(port->ndev); 21082934db9bSVignesh Raghavendra priv->offload_fwd_mark = set_val; 21092934db9bSVignesh Raghavendra } 21102934db9bSVignesh Raghavendra } 21112934db9bSVignesh Raghavendra 21122934db9bSVignesh Raghavendra bool am65_cpsw_port_dev_check(const struct net_device *ndev) 21132934db9bSVignesh Raghavendra { 21142934db9bSVignesh Raghavendra if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) { 21152934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 21162934db9bSVignesh Raghavendra 21172934db9bSVignesh Raghavendra return !common->is_emac_mode; 21182934db9bSVignesh Raghavendra } 21192934db9bSVignesh Raghavendra 21202934db9bSVignesh Raghavendra return false; 21212934db9bSVignesh Raghavendra } 21222934db9bSVignesh Raghavendra 21232f5dc00fSVladimir Oltean static int am65_cpsw_netdevice_port_link(struct net_device *ndev, 21242f5dc00fSVladimir Oltean struct net_device *br_ndev, 21252f5dc00fSVladimir Oltean struct netlink_ext_ack *extack) 21262934db9bSVignesh Raghavendra { 21272934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 21282934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 21292f5dc00fSVladimir Oltean int err; 21302934db9bSVignesh Raghavendra 21312934db9bSVignesh Raghavendra if (!common->br_members) { 21322934db9bSVignesh Raghavendra common->hw_bridge_dev = br_ndev; 21332934db9bSVignesh Raghavendra } else { 21342934db9bSVignesh Raghavendra /* This is adding the port to a second bridge, this is 21352934db9bSVignesh Raghavendra * unsupported 21362934db9bSVignesh Raghavendra */ 21372934db9bSVignesh Raghavendra if (common->hw_bridge_dev != br_ndev) 21382934db9bSVignesh Raghavendra return -EOPNOTSUPP; 21392934db9bSVignesh Raghavendra } 21402934db9bSVignesh Raghavendra 21414e51bf44SVladimir Oltean err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL, 214247211192STobias Waldekranz false, extack); 21432f5dc00fSVladimir Oltean if (err) 21442f5dc00fSVladimir Oltean return err; 21452f5dc00fSVladimir Oltean 21462934db9bSVignesh Raghavendra common->br_members |= BIT(priv->port->port_id); 21472934db9bSVignesh Raghavendra 21482934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 21492934db9bSVignesh Raghavendra 21502934db9bSVignesh Raghavendra return NOTIFY_DONE; 21512934db9bSVignesh Raghavendra } 21522934db9bSVignesh Raghavendra 21532934db9bSVignesh Raghavendra static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev) 21542934db9bSVignesh Raghavendra { 21552934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 21562934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 21572934db9bSVignesh Raghavendra 21584e51bf44SVladimir Oltean switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL); 21592f5dc00fSVladimir Oltean 21602934db9bSVignesh Raghavendra common->br_members &= ~BIT(priv->port->port_id); 21612934db9bSVignesh Raghavendra 21622934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 21632934db9bSVignesh Raghavendra 21642934db9bSVignesh Raghavendra if (!common->br_members) 21652934db9bSVignesh Raghavendra common->hw_bridge_dev = NULL; 21662934db9bSVignesh Raghavendra } 21672934db9bSVignesh Raghavendra 21682934db9bSVignesh Raghavendra /* netdev notifier */ 21692934db9bSVignesh Raghavendra static int am65_cpsw_netdevice_event(struct notifier_block *unused, 21702934db9bSVignesh Raghavendra unsigned long event, void *ptr) 21712934db9bSVignesh Raghavendra { 21722f5dc00fSVladimir Oltean struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 21732934db9bSVignesh Raghavendra struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 21742934db9bSVignesh Raghavendra struct netdev_notifier_changeupper_info *info; 21752934db9bSVignesh Raghavendra int ret = NOTIFY_DONE; 21762934db9bSVignesh Raghavendra 21772934db9bSVignesh Raghavendra if (!am65_cpsw_port_dev_check(ndev)) 21782934db9bSVignesh Raghavendra return NOTIFY_DONE; 21792934db9bSVignesh Raghavendra 21802934db9bSVignesh Raghavendra switch (event) { 21812934db9bSVignesh Raghavendra case NETDEV_CHANGEUPPER: 21822934db9bSVignesh Raghavendra info = ptr; 21832934db9bSVignesh Raghavendra 21842934db9bSVignesh Raghavendra if (netif_is_bridge_master(info->upper_dev)) { 21852934db9bSVignesh Raghavendra if (info->linking) 21862f5dc00fSVladimir Oltean ret = am65_cpsw_netdevice_port_link(ndev, 21872f5dc00fSVladimir Oltean info->upper_dev, 21882f5dc00fSVladimir Oltean extack); 21892934db9bSVignesh Raghavendra else 21902934db9bSVignesh Raghavendra am65_cpsw_netdevice_port_unlink(ndev); 21912934db9bSVignesh Raghavendra } 21922934db9bSVignesh Raghavendra break; 21932934db9bSVignesh Raghavendra default: 21942934db9bSVignesh Raghavendra return NOTIFY_DONE; 21952934db9bSVignesh Raghavendra } 21962934db9bSVignesh Raghavendra 21972934db9bSVignesh Raghavendra return notifier_from_errno(ret); 21982934db9bSVignesh Raghavendra } 21992934db9bSVignesh Raghavendra 22002934db9bSVignesh Raghavendra static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw) 22012934db9bSVignesh Raghavendra { 22022934db9bSVignesh Raghavendra int ret = 0; 22032934db9bSVignesh Raghavendra 22042934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 22052934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 22062934db9bSVignesh Raghavendra return 0; 22072934db9bSVignesh Raghavendra 22082934db9bSVignesh Raghavendra cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event; 22092934db9bSVignesh Raghavendra ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 22102934db9bSVignesh Raghavendra if (ret) { 22112934db9bSVignesh Raghavendra dev_err(cpsw->dev, "can't register netdevice notifier\n"); 22122934db9bSVignesh Raghavendra return ret; 22132934db9bSVignesh Raghavendra } 22142934db9bSVignesh Raghavendra 221586e8b070SVignesh Raghavendra ret = am65_cpsw_switchdev_register_notifiers(cpsw); 221686e8b070SVignesh Raghavendra if (ret) 221786e8b070SVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 221886e8b070SVignesh Raghavendra 22192934db9bSVignesh Raghavendra return ret; 22202934db9bSVignesh Raghavendra } 22212934db9bSVignesh Raghavendra 22222934db9bSVignesh Raghavendra static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw) 22232934db9bSVignesh Raghavendra { 22242934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 22252934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 22262934db9bSVignesh Raghavendra return; 22272934db9bSVignesh Raghavendra 222886e8b070SVignesh Raghavendra am65_cpsw_switchdev_unregister_notifiers(cpsw); 22292934db9bSVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 22302934db9bSVignesh Raghavendra } 22312934db9bSVignesh Raghavendra 223258356eb3SVignesh Raghavendra static const struct devlink_ops am65_cpsw_devlink_ops = {}; 223358356eb3SVignesh Raghavendra 223458356eb3SVignesh Raghavendra static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw) 223558356eb3SVignesh Raghavendra { 223658356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0, 223758356eb3SVignesh Raghavendra ALE_MCAST_BLOCK_LEARN_FWD); 223858356eb3SVignesh Raghavendra } 223958356eb3SVignesh Raghavendra 224058356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common) 224158356eb3SVignesh Raghavendra { 224258356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 224358356eb3SVignesh Raghavendra 224458356eb3SVignesh Raghavendra writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 224558356eb3SVignesh Raghavendra 224658356eb3SVignesh Raghavendra am65_cpsw_init_stp_ale_entry(common); 224758356eb3SVignesh Raghavendra 224858356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1); 224958356eb3SVignesh Raghavendra dev_dbg(common->dev, "Set P0_UNI_FLOOD\n"); 225058356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0); 225158356eb3SVignesh Raghavendra } 225258356eb3SVignesh Raghavendra 225358356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common) 225458356eb3SVignesh Raghavendra { 225558356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 225658356eb3SVignesh Raghavendra 225758356eb3SVignesh Raghavendra writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 225858356eb3SVignesh Raghavendra 225958356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0); 226058356eb3SVignesh Raghavendra dev_dbg(common->dev, "unset P0_UNI_FLOOD\n"); 226158356eb3SVignesh Raghavendra 226258356eb3SVignesh Raghavendra /* learning make no sense in multi-mac mode */ 226358356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1); 226458356eb3SVignesh Raghavendra } 226558356eb3SVignesh Raghavendra 226658356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id, 226758356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 226858356eb3SVignesh Raghavendra { 226958356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 227058356eb3SVignesh Raghavendra struct am65_cpsw_common *common = dl_priv->common; 227158356eb3SVignesh Raghavendra 227258356eb3SVignesh Raghavendra dev_dbg(common->dev, "%s id:%u\n", __func__, id); 227358356eb3SVignesh Raghavendra 227458356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 227558356eb3SVignesh Raghavendra return -EOPNOTSUPP; 227658356eb3SVignesh Raghavendra 227758356eb3SVignesh Raghavendra ctx->val.vbool = !common->is_emac_mode; 227858356eb3SVignesh Raghavendra 227958356eb3SVignesh Raghavendra return 0; 228058356eb3SVignesh Raghavendra } 228158356eb3SVignesh Raghavendra 228258356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port) 228358356eb3SVignesh Raghavendra { 228458356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 228558356eb3SVignesh Raghavendra struct am65_cpsw_common *common = port->common; 228658356eb3SVignesh Raghavendra u32 port_mask; 228758356eb3SVignesh Raghavendra 228858356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 228958356eb3SVignesh Raghavendra 229058356eb3SVignesh Raghavendra if (slave->mac_only) 229158356eb3SVignesh Raghavendra /* enable mac-only mode on port */ 229258356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, 229358356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 1); 229458356eb3SVignesh Raghavendra 229558356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1); 229658356eb3SVignesh Raghavendra 229758356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 229858356eb3SVignesh Raghavendra 229958356eb3SVignesh Raghavendra cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr, 230058356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE, slave->port_vlan); 230158356eb3SVignesh Raghavendra cpsw_ale_add_mcast(common->ale, port->ndev->broadcast, 230258356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2); 230358356eb3SVignesh Raghavendra } 230458356eb3SVignesh Raghavendra 230558356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port) 230658356eb3SVignesh Raghavendra { 230758356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 230858356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = port->common; 230958356eb3SVignesh Raghavendra u32 port_mask; 231058356eb3SVignesh Raghavendra 231158356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 231258356eb3SVignesh Raghavendra ALE_PORT_NOLEARN, 0); 231358356eb3SVignesh Raghavendra 231458356eb3SVignesh Raghavendra cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr, 231558356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN, 231658356eb3SVignesh Raghavendra slave->port_vlan); 231758356eb3SVignesh Raghavendra 231858356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 231958356eb3SVignesh Raghavendra 232058356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast, 232158356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, 232258356eb3SVignesh Raghavendra ALE_MCAST_FWD_2); 232358356eb3SVignesh Raghavendra 232458356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 232558356eb3SVignesh Raghavendra 232658356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 232758356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 0); 232858356eb3SVignesh Raghavendra } 232958356eb3SVignesh Raghavendra 233058356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id, 233158356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 233258356eb3SVignesh Raghavendra { 233358356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 233458356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = dl_priv->common; 233558356eb3SVignesh Raghavendra bool switch_en = ctx->val.vbool; 233658356eb3SVignesh Raghavendra bool if_running = false; 233758356eb3SVignesh Raghavendra int i; 233858356eb3SVignesh Raghavendra 233958356eb3SVignesh Raghavendra dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 234058356eb3SVignesh Raghavendra 234158356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 234258356eb3SVignesh Raghavendra return -EOPNOTSUPP; 234358356eb3SVignesh Raghavendra 234458356eb3SVignesh Raghavendra if (switch_en == !cpsw->is_emac_mode) 234558356eb3SVignesh Raghavendra return 0; 234658356eb3SVignesh Raghavendra 234758356eb3SVignesh Raghavendra if (!switch_en && cpsw->br_members) { 234858356eb3SVignesh Raghavendra dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n"); 234958356eb3SVignesh Raghavendra return -EINVAL; 235058356eb3SVignesh Raghavendra } 235158356eb3SVignesh Raghavendra 235258356eb3SVignesh Raghavendra rtnl_lock(); 235358356eb3SVignesh Raghavendra 235458356eb3SVignesh Raghavendra cpsw->is_emac_mode = !switch_en; 235558356eb3SVignesh Raghavendra 235658356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 235758356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 235858356eb3SVignesh Raghavendra 235958356eb3SVignesh Raghavendra if (!sl_ndev || !netif_running(sl_ndev)) 236058356eb3SVignesh Raghavendra continue; 236158356eb3SVignesh Raghavendra 236258356eb3SVignesh Raghavendra if_running = true; 236358356eb3SVignesh Raghavendra } 236458356eb3SVignesh Raghavendra 236558356eb3SVignesh Raghavendra if (!if_running) { 236658356eb3SVignesh Raghavendra /* all ndevs are down */ 236758356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 236858356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 236958356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 237058356eb3SVignesh Raghavendra 237158356eb3SVignesh Raghavendra if (!sl_ndev) 237258356eb3SVignesh Raghavendra continue; 237358356eb3SVignesh Raghavendra 237458356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 237558356eb3SVignesh Raghavendra if (switch_en) 237658356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 237758356eb3SVignesh Raghavendra else 237858356eb3SVignesh Raghavendra slave->port_vlan = 0; 237958356eb3SVignesh Raghavendra } 238058356eb3SVignesh Raghavendra 238158356eb3SVignesh Raghavendra goto exit; 238258356eb3SVignesh Raghavendra } 238358356eb3SVignesh Raghavendra 238458356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 238558356eb3SVignesh Raghavendra /* clean up ALE table */ 238658356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1); 238758356eb3SVignesh Raghavendra cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT); 238858356eb3SVignesh Raghavendra 238958356eb3SVignesh Raghavendra if (switch_en) { 239058356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Enable switch mode\n"); 239158356eb3SVignesh Raghavendra 239258356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(cpsw); 239358356eb3SVignesh Raghavendra 239458356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 239558356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 239658356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 239758356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 239858356eb3SVignesh Raghavendra 239958356eb3SVignesh Raghavendra if (!sl_ndev) 240058356eb3SVignesh Raghavendra continue; 240158356eb3SVignesh Raghavendra 240258356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 240358356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 240458356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 240558356eb3SVignesh Raghavendra 240658356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 240758356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 240858356eb3SVignesh Raghavendra } 240958356eb3SVignesh Raghavendra 241058356eb3SVignesh Raghavendra } else { 241158356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Disable switch mode\n"); 241258356eb3SVignesh Raghavendra 241358356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(cpsw); 241458356eb3SVignesh Raghavendra 241558356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 241658356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 241758356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 241858356eb3SVignesh Raghavendra 241958356eb3SVignesh Raghavendra if (!sl_ndev) 242058356eb3SVignesh Raghavendra continue; 242158356eb3SVignesh Raghavendra 242258356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 242358356eb3SVignesh Raghavendra port->slave.port_vlan = 0; 242458356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 242558356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 242658356eb3SVignesh Raghavendra } 242758356eb3SVignesh Raghavendra } 242858356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0); 242958356eb3SVignesh Raghavendra exit: 243058356eb3SVignesh Raghavendra rtnl_unlock(); 243158356eb3SVignesh Raghavendra 243258356eb3SVignesh Raghavendra return 0; 243358356eb3SVignesh Raghavendra } 243458356eb3SVignesh Raghavendra 243558356eb3SVignesh Raghavendra static const struct devlink_param am65_cpsw_devlink_params[] = { 243658356eb3SVignesh Raghavendra DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode", 243758356eb3SVignesh Raghavendra DEVLINK_PARAM_TYPE_BOOL, 243858356eb3SVignesh Raghavendra BIT(DEVLINK_PARAM_CMODE_RUNTIME), 243958356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_get, 244058356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_set, NULL), 244158356eb3SVignesh Raghavendra }; 244258356eb3SVignesh Raghavendra 244358356eb3SVignesh Raghavendra static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common) 244458356eb3SVignesh Raghavendra { 244558356eb3SVignesh Raghavendra struct devlink_port_attrs attrs = {}; 244658356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv; 244758356eb3SVignesh Raghavendra struct device *dev = common->dev; 244858356eb3SVignesh Raghavendra struct devlink_port *dl_port; 244958356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 245058356eb3SVignesh Raghavendra int ret = 0; 245158356eb3SVignesh Raghavendra int i; 245258356eb3SVignesh Raghavendra 245358356eb3SVignesh Raghavendra common->devlink = 2454919d13a7SLeon Romanovsky devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev); 245558356eb3SVignesh Raghavendra if (!common->devlink) 245658356eb3SVignesh Raghavendra return -ENOMEM; 245758356eb3SVignesh Raghavendra 245858356eb3SVignesh Raghavendra dl_priv = devlink_priv(common->devlink); 245958356eb3SVignesh Raghavendra dl_priv->common = common; 246058356eb3SVignesh Raghavendra 246158356eb3SVignesh Raghavendra /* Provide devlink hook to switch mode when multiple external ports 246258356eb3SVignesh Raghavendra * are present NUSS switchdev driver is enabled. 246358356eb3SVignesh Raghavendra */ 246458356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 246558356eb3SVignesh Raghavendra IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) { 246658356eb3SVignesh Raghavendra ret = devlink_params_register(common->devlink, 246758356eb3SVignesh Raghavendra am65_cpsw_devlink_params, 246858356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 246958356eb3SVignesh Raghavendra if (ret) { 247058356eb3SVignesh Raghavendra dev_err(dev, "devlink params reg fail ret:%d\n", ret); 247158356eb3SVignesh Raghavendra goto dl_unreg; 247258356eb3SVignesh Raghavendra } 247358356eb3SVignesh Raghavendra } 247458356eb3SVignesh Raghavendra 247558356eb3SVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 247658356eb3SVignesh Raghavendra port = am65_common_get_port(common, i); 247758356eb3SVignesh Raghavendra dl_port = &port->devlink_port; 247858356eb3SVignesh Raghavendra 24797e777b1bSMatthias Schiffer if (port->ndev) 248058356eb3SVignesh Raghavendra attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 24817e777b1bSMatthias Schiffer else 24827e777b1bSMatthias Schiffer attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; 248358356eb3SVignesh Raghavendra attrs.phys.port_number = port->port_id; 248458356eb3SVignesh Raghavendra attrs.switch_id.id_len = sizeof(resource_size_t); 248558356eb3SVignesh Raghavendra memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len); 248658356eb3SVignesh Raghavendra devlink_port_attrs_set(dl_port, &attrs); 248758356eb3SVignesh Raghavendra 248858356eb3SVignesh Raghavendra ret = devlink_port_register(common->devlink, dl_port, port->port_id); 248958356eb3SVignesh Raghavendra if (ret) { 249058356eb3SVignesh Raghavendra dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n", 249158356eb3SVignesh Raghavendra port->port_id, ret); 249258356eb3SVignesh Raghavendra goto dl_port_unreg; 249358356eb3SVignesh Raghavendra } 249458356eb3SVignesh Raghavendra } 24950d98ff22SLeon Romanovsky devlink_register(common->devlink); 249658356eb3SVignesh Raghavendra return ret; 249758356eb3SVignesh Raghavendra 249858356eb3SVignesh Raghavendra dl_port_unreg: 2499acf34954SLeon Romanovsky for (i = i - 1; i >= 1; i--) { 2500acf34954SLeon Romanovsky port = am65_common_get_port(common, i); 2501acf34954SLeon Romanovsky dl_port = &port->devlink_port; 2502acf34954SLeon Romanovsky 2503acf34954SLeon Romanovsky devlink_port_unregister(dl_port); 2504acf34954SLeon Romanovsky } 250558356eb3SVignesh Raghavendra dl_unreg: 250658356eb3SVignesh Raghavendra devlink_free(common->devlink); 250758356eb3SVignesh Raghavendra return ret; 250858356eb3SVignesh Raghavendra } 250958356eb3SVignesh Raghavendra 251058356eb3SVignesh Raghavendra static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common) 251158356eb3SVignesh Raghavendra { 2512acf34954SLeon Romanovsky struct devlink_port *dl_port; 2513acf34954SLeon Romanovsky struct am65_cpsw_port *port; 2514acf34954SLeon Romanovsky int i; 2515acf34954SLeon Romanovsky 25160d98ff22SLeon Romanovsky devlink_unregister(common->devlink); 25170d98ff22SLeon Romanovsky 2518acf34954SLeon Romanovsky for (i = 1; i <= common->port_num; i++) { 2519acf34954SLeon Romanovsky port = am65_common_get_port(common, i); 2520acf34954SLeon Romanovsky dl_port = &port->devlink_port; 2521acf34954SLeon Romanovsky 2522acf34954SLeon Romanovsky devlink_port_unregister(dl_port); 2523acf34954SLeon Romanovsky } 2524acf34954SLeon Romanovsky 252558356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 25260d98ff22SLeon Romanovsky IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 25270d98ff22SLeon Romanovsky devlink_params_unregister(common->devlink, 25280d98ff22SLeon Romanovsky am65_cpsw_devlink_params, 252958356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 253058356eb3SVignesh Raghavendra 253158356eb3SVignesh Raghavendra devlink_free(common->devlink); 253258356eb3SVignesh Raghavendra } 253358356eb3SVignesh Raghavendra 253484b4aa49SGrygorii Strashko static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common) 253593a76530SGrygorii Strashko { 253693a76530SGrygorii Strashko struct device *dev = common->dev; 25370680e20aSSiddharth Vadapalli struct devlink_port *dl_port; 253893a76530SGrygorii Strashko struct am65_cpsw_port *port; 253984b4aa49SGrygorii Strashko int ret = 0, i; 254093a76530SGrygorii Strashko 254184b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_ndev_add_tx_napi(common); 254293a76530SGrygorii Strashko if (ret) 254384b4aa49SGrygorii Strashko return ret; 254493a76530SGrygorii Strashko 254593a76530SGrygorii Strashko ret = devm_request_irq(dev, common->rx_chns.irq, 254693a76530SGrygorii Strashko am65_cpsw_nuss_rx_irq, 25476f5c27f9SGrygorii Strashko IRQF_TRIGGER_HIGH, dev_name(dev), common); 254893a76530SGrygorii Strashko if (ret) { 254993a76530SGrygorii Strashko dev_err(dev, "failure requesting rx irq %u, %d\n", 255093a76530SGrygorii Strashko common->rx_chns.irq, ret); 255184b4aa49SGrygorii Strashko return ret; 255293a76530SGrygorii Strashko } 255393a76530SGrygorii Strashko 25540680e20aSSiddharth Vadapalli ret = am65_cpsw_nuss_register_devlink(common); 25550680e20aSSiddharth Vadapalli if (ret) 25560680e20aSSiddharth Vadapalli return ret; 25570680e20aSSiddharth Vadapalli 255884b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 255984b4aa49SGrygorii Strashko port = &common->ports[i]; 256084b4aa49SGrygorii Strashko 256184b4aa49SGrygorii Strashko if (!port->ndev) 256284b4aa49SGrygorii Strashko continue; 256384b4aa49SGrygorii Strashko 256493a76530SGrygorii Strashko ret = register_netdev(port->ndev); 256584b4aa49SGrygorii Strashko if (ret) { 256684b4aa49SGrygorii Strashko dev_err(dev, "error registering slave net device%i %d\n", 256784b4aa49SGrygorii Strashko i, ret); 256884b4aa49SGrygorii Strashko goto err_cleanup_ndev; 256984b4aa49SGrygorii Strashko } 25700680e20aSSiddharth Vadapalli 25710680e20aSSiddharth Vadapalli dl_port = &port->devlink_port; 25720680e20aSSiddharth Vadapalli devlink_port_type_eth_set(dl_port, port->ndev); 257384b4aa49SGrygorii Strashko } 257484b4aa49SGrygorii Strashko 25752934db9bSVignesh Raghavendra ret = am65_cpsw_register_notifiers(common); 257658356eb3SVignesh Raghavendra if (ret) 257758356eb3SVignesh Raghavendra goto err_cleanup_ndev; 257893a76530SGrygorii Strashko 257993a76530SGrygorii Strashko /* can't auto unregister ndev using devm_add_action() due to 258093a76530SGrygorii Strashko * devres release sequence in DD core for DMA 258193a76530SGrygorii Strashko */ 258284b4aa49SGrygorii Strashko 258358356eb3SVignesh Raghavendra return 0; 25840680e20aSSiddharth Vadapalli 258584b4aa49SGrygorii Strashko err_cleanup_ndev: 258684b4aa49SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 25870680e20aSSiddharth Vadapalli am65_cpsw_unregister_devlink(common); 25882934db9bSVignesh Raghavendra 258993a76530SGrygorii Strashko return ret; 259093a76530SGrygorii Strashko } 259193a76530SGrygorii Strashko 259293a76530SGrygorii Strashko int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx) 259393a76530SGrygorii Strashko { 259493a76530SGrygorii Strashko int ret; 259593a76530SGrygorii Strashko 259693a76530SGrygorii Strashko common->tx_ch_num = num_tx; 259793a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_tx_chns(common); 259893a76530SGrygorii Strashko if (ret) 259993a76530SGrygorii Strashko return ret; 260093a76530SGrygorii Strashko 260184b4aa49SGrygorii Strashko return am65_cpsw_nuss_ndev_add_tx_napi(common); 260293a76530SGrygorii Strashko } 260393a76530SGrygorii Strashko 260438389aa6SGrygorii Strashko struct am65_cpsw_soc_pdata { 260538389aa6SGrygorii Strashko u32 quirks_dis; 260638389aa6SGrygorii Strashko }; 260738389aa6SGrygorii Strashko 260838389aa6SGrygorii Strashko static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = { 260938389aa6SGrygorii Strashko .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 261038389aa6SGrygorii Strashko }; 261138389aa6SGrygorii Strashko 261238389aa6SGrygorii Strashko static const struct soc_device_attribute am65_cpsw_socinfo[] = { 261338389aa6SGrygorii Strashko { .family = "AM65X", 261438389aa6SGrygorii Strashko .revision = "SR2.0", 261538389aa6SGrygorii Strashko .data = &am65x_soc_sr2_0 261638389aa6SGrygorii Strashko }, 261738389aa6SGrygorii Strashko {/* sentinel */} 261838389aa6SGrygorii Strashko }; 261938389aa6SGrygorii Strashko 262093a76530SGrygorii Strashko static const struct am65_cpsw_pdata am65x_sr1_0 = { 262193a76530SGrygorii Strashko .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 26227747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2623c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 262493a76530SGrygorii Strashko }; 262593a76530SGrygorii Strashko 262638389aa6SGrygorii Strashko static const struct am65_cpsw_pdata j721e_pdata = { 262793a76530SGrygorii Strashko .quirks = 0, 26287747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2629c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 263093a76530SGrygorii Strashko }; 263193a76530SGrygorii Strashko 26324f7cce27SVignesh Raghavendra static const struct am65_cpsw_pdata am64x_cpswxg_pdata = { 26334f7cce27SVignesh Raghavendra .quirks = 0, 26344f7cce27SVignesh Raghavendra .ale_dev_id = "am64-cpswxg", 26354f7cce27SVignesh Raghavendra .fdqring_mode = K3_RINGACC_RING_MODE_RING, 26364f7cce27SVignesh Raghavendra }; 26374f7cce27SVignesh Raghavendra 263837184fc1SSiddharth Vadapalli static const struct am65_cpsw_pdata j7200_cpswxg_pdata = { 263937184fc1SSiddharth Vadapalli .quirks = 0, 264037184fc1SSiddharth Vadapalli .ale_dev_id = "am64-cpswxg", 264137184fc1SSiddharth Vadapalli .fdqring_mode = K3_RINGACC_RING_MODE_RING, 264237184fc1SSiddharth Vadapalli .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII), 264337184fc1SSiddharth Vadapalli }; 264437184fc1SSiddharth Vadapalli 264593a76530SGrygorii Strashko static const struct of_device_id am65_cpsw_nuss_of_mtable[] = { 264693a76530SGrygorii Strashko { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0}, 264738389aa6SGrygorii Strashko { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata}, 26484f7cce27SVignesh Raghavendra { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata}, 264937184fc1SSiddharth Vadapalli { .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata}, 265093a76530SGrygorii Strashko { /* sentinel */ }, 265193a76530SGrygorii Strashko }; 265293a76530SGrygorii Strashko MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable); 265393a76530SGrygorii Strashko 265438389aa6SGrygorii Strashko static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common) 265538389aa6SGrygorii Strashko { 265638389aa6SGrygorii Strashko const struct soc_device_attribute *soc; 265738389aa6SGrygorii Strashko 265838389aa6SGrygorii Strashko soc = soc_device_match(am65_cpsw_socinfo); 265938389aa6SGrygorii Strashko if (soc && soc->data) { 266038389aa6SGrygorii Strashko const struct am65_cpsw_soc_pdata *socdata = soc->data; 266138389aa6SGrygorii Strashko 266238389aa6SGrygorii Strashko /* disable quirks */ 266338389aa6SGrygorii Strashko common->pdata.quirks &= ~socdata->quirks_dis; 266438389aa6SGrygorii Strashko } 266538389aa6SGrygorii Strashko } 266638389aa6SGrygorii Strashko 266793a76530SGrygorii Strashko static int am65_cpsw_nuss_probe(struct platform_device *pdev) 266893a76530SGrygorii Strashko { 26692074f9eaSGrygorii Strashko struct cpsw_ale_params ale_params = { 0 }; 267093a76530SGrygorii Strashko const struct of_device_id *of_id; 267193a76530SGrygorii Strashko struct device *dev = &pdev->dev; 267293a76530SGrygorii Strashko struct am65_cpsw_common *common; 267393a76530SGrygorii Strashko struct device_node *node; 267493a76530SGrygorii Strashko struct resource *res; 267523015ff1SGrygorii Strashko struct clk *clk; 267658356eb3SVignesh Raghavendra u64 id_temp; 267793a76530SGrygorii Strashko int ret, i; 267893a76530SGrygorii Strashko 267993a76530SGrygorii Strashko common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL); 268093a76530SGrygorii Strashko if (!common) 268193a76530SGrygorii Strashko return -ENOMEM; 268293a76530SGrygorii Strashko common->dev = dev; 268393a76530SGrygorii Strashko 268493a76530SGrygorii Strashko of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev); 268593a76530SGrygorii Strashko if (!of_id) 268693a76530SGrygorii Strashko return -EINVAL; 268738389aa6SGrygorii Strashko common->pdata = *(const struct am65_cpsw_pdata *)of_id->data; 268838389aa6SGrygorii Strashko 268938389aa6SGrygorii Strashko am65_cpsw_nuss_apply_socinfo(common); 269093a76530SGrygorii Strashko 269193a76530SGrygorii Strashko res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss"); 269293a76530SGrygorii Strashko common->ss_base = devm_ioremap_resource(&pdev->dev, res); 269393a76530SGrygorii Strashko if (IS_ERR(common->ss_base)) 269493a76530SGrygorii Strashko return PTR_ERR(common->ss_base); 269593a76530SGrygorii Strashko common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE; 269658356eb3SVignesh Raghavendra /* Use device's physical base address as switch id */ 269758356eb3SVignesh Raghavendra id_temp = cpu_to_be64(res->start); 269858356eb3SVignesh Raghavendra memcpy(common->switch_id, &id_temp, sizeof(res->start)); 269993a76530SGrygorii Strashko 270093a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 270193a76530SGrygorii Strashko if (!node) 270293a76530SGrygorii Strashko return -ENOENT; 270393a76530SGrygorii Strashko common->port_num = of_get_child_count(node); 27045dd89d2fSMiaoqian Lin of_node_put(node); 270593a76530SGrygorii Strashko if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS) 270693a76530SGrygorii Strashko return -ENOENT; 270793a76530SGrygorii Strashko 270893a76530SGrygorii Strashko common->rx_flow_id_base = -1; 270993a76530SGrygorii Strashko init_completion(&common->tdown_complete); 271093a76530SGrygorii Strashko common->tx_ch_num = 1; 271151824048SGrygorii Strashko common->pf_p0_rx_ptype_rrobin = false; 271258356eb3SVignesh Raghavendra common->default_vlan = 1; 271393a76530SGrygorii Strashko 271493a76530SGrygorii Strashko common->ports = devm_kcalloc(dev, common->port_num, 271593a76530SGrygorii Strashko sizeof(*common->ports), 271693a76530SGrygorii Strashko GFP_KERNEL); 271793a76530SGrygorii Strashko if (!common->ports) 271893a76530SGrygorii Strashko return -ENOMEM; 271993a76530SGrygorii Strashko 272023015ff1SGrygorii Strashko clk = devm_clk_get(dev, "fck"); 27218fbc2f9eSGrygorii Strashko if (IS_ERR(clk)) 27228fbc2f9eSGrygorii Strashko return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n"); 272323015ff1SGrygorii Strashko common->bus_freq = clk_get_rate(clk); 272423015ff1SGrygorii Strashko 272593a76530SGrygorii Strashko pm_runtime_enable(dev); 27262240514cSMinghao Chi ret = pm_runtime_resume_and_get(dev); 272793a76530SGrygorii Strashko if (ret < 0) { 272893a76530SGrygorii Strashko pm_runtime_disable(dev); 272993a76530SGrygorii Strashko return ret; 273093a76530SGrygorii Strashko } 273193a76530SGrygorii Strashko 2732a45cfcc6SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "mdio"); 2733a45cfcc6SGrygorii Strashko if (!node) { 2734a45cfcc6SGrygorii Strashko dev_warn(dev, "MDIO node not found\n"); 2735a45cfcc6SGrygorii Strashko } else if (of_device_is_available(node)) { 2736a45cfcc6SGrygorii Strashko struct platform_device *mdio_pdev; 2737a45cfcc6SGrygorii Strashko 2738a45cfcc6SGrygorii Strashko mdio_pdev = of_platform_device_create(node, NULL, dev); 2739a45cfcc6SGrygorii Strashko if (!mdio_pdev) { 2740a45cfcc6SGrygorii Strashko ret = -ENODEV; 2741a45cfcc6SGrygorii Strashko goto err_pm_clear; 2742a45cfcc6SGrygorii Strashko } 2743a45cfcc6SGrygorii Strashko 2744a45cfcc6SGrygorii Strashko common->mdio_dev = &mdio_pdev->dev; 2745a45cfcc6SGrygorii Strashko } 2746a45cfcc6SGrygorii Strashko of_node_put(node); 274793a76530SGrygorii Strashko 274893a76530SGrygorii Strashko am65_cpsw_nuss_get_ver(common); 274993a76530SGrygorii Strashko 275093a76530SGrygorii Strashko /* init tx channels */ 275193a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_tx_chns(common); 275293a76530SGrygorii Strashko if (ret) 275393a76530SGrygorii Strashko goto err_of_clear; 275493a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_rx_chns(common); 275593a76530SGrygorii Strashko if (ret) 275693a76530SGrygorii Strashko goto err_of_clear; 275793a76530SGrygorii Strashko 275893a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_host_p(common); 275993a76530SGrygorii Strashko if (ret) 276093a76530SGrygorii Strashko goto err_of_clear; 276193a76530SGrygorii Strashko 276293a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_slave_ports(common); 276393a76530SGrygorii Strashko if (ret) 276493a76530SGrygorii Strashko goto err_of_clear; 276593a76530SGrygorii Strashko 276693a76530SGrygorii Strashko /* init common data */ 276793a76530SGrygorii Strashko ale_params.dev = dev; 276893a76530SGrygorii Strashko ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT; 276993a76530SGrygorii Strashko ale_params.ale_ports = common->port_num + 1; 277093a76530SGrygorii Strashko ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE; 27717747d4b7SGrygorii Strashko ale_params.dev_id = common->pdata.ale_dev_id; 277223015ff1SGrygorii Strashko ale_params.bus_freq = common->bus_freq; 277393a76530SGrygorii Strashko 277493a76530SGrygorii Strashko common->ale = cpsw_ale_create(&ale_params); 27751401cf60SWei Yongjun if (IS_ERR(common->ale)) { 277693a76530SGrygorii Strashko dev_err(dev, "error initializing ale engine\n"); 27771401cf60SWei Yongjun ret = PTR_ERR(common->ale); 277893a76530SGrygorii Strashko goto err_of_clear; 277993a76530SGrygorii Strashko } 278093a76530SGrygorii Strashko 2781b1f66a5bSGrygorii Strashko ret = am65_cpsw_init_cpts(common); 2782b1f66a5bSGrygorii Strashko if (ret) 2783b1f66a5bSGrygorii Strashko goto err_of_clear; 2784b1f66a5bSGrygorii Strashko 278593a76530SGrygorii Strashko /* init ports */ 278693a76530SGrygorii Strashko for (i = 0; i < common->port_num; i++) 278793a76530SGrygorii Strashko am65_cpsw_nuss_slave_disable_unused(&common->ports[i]); 278893a76530SGrygorii Strashko 278993a76530SGrygorii Strashko dev_set_drvdata(dev, common); 279093a76530SGrygorii Strashko 279158356eb3SVignesh Raghavendra common->is_emac_mode = true; 279258356eb3SVignesh Raghavendra 279384b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_ndevs(common); 279493a76530SGrygorii Strashko if (ret) 2795e8609e69SSiddharth Vadapalli goto err_free_phylink; 279693a76530SGrygorii Strashko 279784b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_register_ndevs(common); 279893a76530SGrygorii Strashko if (ret) 2799e8609e69SSiddharth Vadapalli goto err_free_phylink; 280093a76530SGrygorii Strashko 280193a76530SGrygorii Strashko pm_runtime_put(dev); 280293a76530SGrygorii Strashko return 0; 280393a76530SGrygorii Strashko 2804e8609e69SSiddharth Vadapalli err_free_phylink: 2805e8609e69SSiddharth Vadapalli am65_cpsw_nuss_phylink_cleanup(common); 280693a76530SGrygorii Strashko err_of_clear: 2807a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 2808a45cfcc6SGrygorii Strashko err_pm_clear: 280993a76530SGrygorii Strashko pm_runtime_put_sync(dev); 281093a76530SGrygorii Strashko pm_runtime_disable(dev); 281193a76530SGrygorii Strashko return ret; 281293a76530SGrygorii Strashko } 281393a76530SGrygorii Strashko 281493a76530SGrygorii Strashko static int am65_cpsw_nuss_remove(struct platform_device *pdev) 281593a76530SGrygorii Strashko { 281693a76530SGrygorii Strashko struct device *dev = &pdev->dev; 281793a76530SGrygorii Strashko struct am65_cpsw_common *common; 281893a76530SGrygorii Strashko int ret; 281993a76530SGrygorii Strashko 282093a76530SGrygorii Strashko common = dev_get_drvdata(dev); 282193a76530SGrygorii Strashko 28222240514cSMinghao Chi ret = pm_runtime_resume_and_get(&pdev->dev); 28232240514cSMinghao Chi if (ret < 0) 282493a76530SGrygorii Strashko return ret; 282593a76530SGrygorii Strashko 2826e8609e69SSiddharth Vadapalli am65_cpsw_nuss_phylink_cleanup(common); 282758356eb3SVignesh Raghavendra am65_cpsw_unregister_devlink(common); 28282934db9bSVignesh Raghavendra am65_cpsw_unregister_notifiers(common); 282958356eb3SVignesh Raghavendra 283093a76530SGrygorii Strashko /* must unregister ndevs here because DD release_driver routine calls 283193a76530SGrygorii Strashko * dma_deconfigure(dev) before devres_release_all(dev) 283293a76530SGrygorii Strashko */ 283393a76530SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 283493a76530SGrygorii Strashko 2835a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 283693a76530SGrygorii Strashko 283793a76530SGrygorii Strashko pm_runtime_put_sync(&pdev->dev); 283893a76530SGrygorii Strashko pm_runtime_disable(&pdev->dev); 283993a76530SGrygorii Strashko return 0; 284093a76530SGrygorii Strashko } 284193a76530SGrygorii Strashko 284293a76530SGrygorii Strashko static struct platform_driver am65_cpsw_nuss_driver = { 284393a76530SGrygorii Strashko .driver = { 284493a76530SGrygorii Strashko .name = AM65_CPSW_DRV_NAME, 284593a76530SGrygorii Strashko .of_match_table = am65_cpsw_nuss_of_mtable, 284693a76530SGrygorii Strashko }, 284793a76530SGrygorii Strashko .probe = am65_cpsw_nuss_probe, 284893a76530SGrygorii Strashko .remove = am65_cpsw_nuss_remove, 284993a76530SGrygorii Strashko }; 285093a76530SGrygorii Strashko 285193a76530SGrygorii Strashko module_platform_driver(am65_cpsw_nuss_driver); 285293a76530SGrygorii Strashko 285393a76530SGrygorii Strashko MODULE_LICENSE("GPL v2"); 285493a76530SGrygorii Strashko MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 285593a76530SGrygorii Strashko MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver"); 2856