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> 223d40aed8SRob Herring #include <linux/of_platform.h> 23e8609e69SSiddharth Vadapalli #include <linux/phylink.h> 2493a76530SGrygorii Strashko #include <linux/phy/phy.h> 2593a76530SGrygorii Strashko #include <linux/platform_device.h> 2693a76530SGrygorii Strashko #include <linux/pm_runtime.h> 2793a76530SGrygorii Strashko #include <linux/regmap.h> 2824bc19b0SRoger Quadros #include <linux/rtnetlink.h> 2993a76530SGrygorii Strashko #include <linux/mfd/syscon.h> 3038389aa6SGrygorii Strashko #include <linux/sys_soc.h> 3193a76530SGrygorii Strashko #include <linux/dma/ti-cppi5.h> 3293a76530SGrygorii Strashko #include <linux/dma/k3-udma-glue.h> 33957e2235SVladimir Oltean #include <net/switchdev.h> 3493a76530SGrygorii Strashko 3593a76530SGrygorii Strashko #include "cpsw_ale.h" 3693a76530SGrygorii Strashko #include "cpsw_sl.h" 3793a76530SGrygorii Strashko #include "am65-cpsw-nuss.h" 3886e8b070SVignesh Raghavendra #include "am65-cpsw-switchdev.h" 3993a76530SGrygorii Strashko #include "k3-cppi-desc-pool.h" 40b1f66a5bSGrygorii Strashko #include "am65-cpts.h" 4193a76530SGrygorii Strashko 4293a76530SGrygorii Strashko #define AM65_CPSW_SS_BASE 0x0 4393a76530SGrygorii Strashko #define AM65_CPSW_SGMII_BASE 0x100 4493a76530SGrygorii Strashko #define AM65_CPSW_XGMII_BASE 0x2100 4593a76530SGrygorii Strashko #define AM65_CPSW_CPSW_NU_BASE 0x20000 4693a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_BASE 0x1000 478127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_BASE 0x12000 4893a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_BASE 0x1a000 4993a76530SGrygorii Strashko #define AM65_CPSW_NU_ALE_BASE 0x1e000 5093a76530SGrygorii Strashko #define AM65_CPSW_NU_CPTS_BASE 0x1d000 5193a76530SGrygorii Strashko 5293a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_OFFSET 0x1000 5393a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200 548127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200 5593a76530SGrygorii Strashko 5693a76530SGrygorii Strashko #define AM65_CPSW_MAX_PORTS 8 5793a76530SGrygorii Strashko 5893a76530SGrygorii Strashko #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN 5993a76530SGrygorii Strashko #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 6093a76530SGrygorii Strashko 6193a76530SGrygorii Strashko #define AM65_CPSW_REG_CTL 0x004 6293a76530SGrygorii Strashko #define AM65_CPSW_REG_STAT_PORT_EN 0x014 6393a76530SGrygorii Strashko #define AM65_CPSW_REG_PTYPE 0x018 6493a76530SGrygorii Strashko 6593a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL 0x004 6693a76530SGrygorii Strashko #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008 6793a76530SGrygorii Strashko 6893a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c 6993a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020 7093a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024 7193a76530SGrygorii Strashko 7293a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_L 0x308 7393a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_H 0x30c 7493a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL 0x310 7593a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314 7693a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318 7793a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C 7893a76530SGrygorii Strashko 7937184fc1SSiddharth Vadapalli #define AM65_CPSW_SGMII_CONTROL_REG 0x010 80e0f72db3SSiddharth Vadapalli #define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG 0x018 8137184fc1SSiddharth Vadapalli #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0) 8237184fc1SSiddharth Vadapalli 8393a76530SGrygorii Strashko #define AM65_CPSW_CTL_VLAN_AWARE BIT(1) 8493a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_ENABLE BIT(2) 8593a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13) 8693a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_RX_PAD BIT(14) 8793a76530SGrygorii Strashko 8893a76530SGrygorii Strashko /* AM65_CPSW_P0_REG_CTL */ 8993a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0) 9086e2eca4SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN BIT(16) 9193a76530SGrygorii Strashko 9293a76530SGrygorii Strashko /* AM65_CPSW_PORT_REG_PRI_CTL */ 9393a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8) 9493a76530SGrygorii Strashko 9593a76530SGrygorii Strashko /* AM65_CPSW_PN_TS_CTL register fields */ 9693a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4) 9793a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5) 9893a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6) 9993a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7) 10093a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10) 10193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11) 10293a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16 10393a76530SGrygorii Strashko 10493a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */ 10593a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16 10693a76530SGrygorii Strashko 10793a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */ 10893a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16) 10993a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17) 11093a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18) 11193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19) 11293a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20) 11393a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21) 11493a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22) 11593a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23) 11693a76530SGrygorii Strashko 11793a76530SGrygorii Strashko /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 11893a76530SGrygorii Strashko #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 11993a76530SGrygorii Strashko 12093a76530SGrygorii Strashko #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e) 12193a76530SGrygorii Strashko 12293a76530SGrygorii Strashko #define AM65_CPSW_TS_TX_ANX_ALL_EN \ 12393a76530SGrygorii Strashko (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \ 12493a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \ 12593a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN) 12693a76530SGrygorii Strashko 12793a76530SGrygorii Strashko #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30 12893a76530SGrygorii Strashko /* Number of TX/RX descriptors */ 12993a76530SGrygorii Strashko #define AM65_CPSW_MAX_TX_DESC 500 13093a76530SGrygorii Strashko #define AM65_CPSW_MAX_RX_DESC 500 13193a76530SGrygorii Strashko 13293a76530SGrygorii Strashko #define AM65_CPSW_NAV_PS_DATA_SIZE 16 13393a76530SGrygorii Strashko #define AM65_CPSW_NAV_SW_DATA_SIZE 16 13493a76530SGrygorii Strashko 13593a76530SGrygorii Strashko #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \ 13693a76530SGrygorii Strashko NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \ 13793a76530SGrygorii Strashko NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR) 13893a76530SGrygorii Strashko 13993a76530SGrygorii Strashko static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave, 14093a76530SGrygorii Strashko const u8 *dev_addr) 14193a76530SGrygorii Strashko { 14293a76530SGrygorii Strashko u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) | 14393a76530SGrygorii Strashko (dev_addr[2] << 16) | (dev_addr[3] << 24); 14493a76530SGrygorii Strashko u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8); 14593a76530SGrygorii Strashko 14693a76530SGrygorii Strashko writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H); 14793a76530SGrygorii Strashko writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L); 14893a76530SGrygorii Strashko } 14993a76530SGrygorii Strashko 15093a76530SGrygorii Strashko static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port) 15193a76530SGrygorii Strashko { 15293a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 15393a76530SGrygorii Strashko /* Max length register has to be restored after MAC SL reset */ 15493a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 15593a76530SGrygorii Strashko port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 15693a76530SGrygorii Strashko } 15793a76530SGrygorii Strashko 15893a76530SGrygorii Strashko static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common) 15993a76530SGrygorii Strashko { 16093a76530SGrygorii Strashko common->nuss_ver = readl(common->ss_base); 16193a76530SGrygorii Strashko common->cpsw_ver = readl(common->cpsw_base); 16293a76530SGrygorii Strashko dev_info(common->dev, 16338389aa6SGrygorii Strashko "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n", 16493a76530SGrygorii Strashko common->nuss_ver, 16593a76530SGrygorii Strashko common->cpsw_ver, 16638389aa6SGrygorii Strashko common->port_num + 1, 16738389aa6SGrygorii Strashko common->pdata.quirks); 16893a76530SGrygorii Strashko } 16993a76530SGrygorii Strashko 17093a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev, 17193a76530SGrygorii Strashko __be16 proto, u16 vid) 17293a76530SGrygorii Strashko { 17393a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 17493a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 17593a76530SGrygorii Strashko u32 port_mask, unreg_mcast = 0; 17693a76530SGrygorii Strashko int ret; 17793a76530SGrygorii Strashko 17886e8b070SVignesh Raghavendra if (!common->is_emac_mode) 17986e8b070SVignesh Raghavendra return 0; 18086e8b070SVignesh Raghavendra 1817bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 1827bcffde0SGrygorii Strashko return 0; 1837bcffde0SGrygorii Strashko 1842240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 1852240514cSMinghao Chi if (ret < 0) 18693a76530SGrygorii Strashko return ret; 18793a76530SGrygorii Strashko 18893a76530SGrygorii Strashko port_mask = BIT(port->port_id) | ALE_PORT_HOST; 18993a76530SGrygorii Strashko if (!vid) 19093a76530SGrygorii Strashko unreg_mcast = port_mask; 19193a76530SGrygorii Strashko dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid); 1922d64a034SGrygorii Strashko ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask, 19393a76530SGrygorii Strashko unreg_mcast, port_mask, 0); 19493a76530SGrygorii Strashko 19593a76530SGrygorii Strashko pm_runtime_put(common->dev); 19693a76530SGrygorii Strashko return ret; 19793a76530SGrygorii Strashko } 19893a76530SGrygorii Strashko 19993a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev, 20093a76530SGrygorii Strashko __be16 proto, u16 vid) 20193a76530SGrygorii Strashko { 20293a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 2032d64a034SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 20493a76530SGrygorii Strashko int ret; 20593a76530SGrygorii Strashko 20686e8b070SVignesh Raghavendra if (!common->is_emac_mode) 20786e8b070SVignesh Raghavendra return 0; 20886e8b070SVignesh Raghavendra 2097bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 2107bcffde0SGrygorii Strashko return 0; 2117bcffde0SGrygorii Strashko 2122240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 2132240514cSMinghao Chi if (ret < 0) 21493a76530SGrygorii Strashko return ret; 21593a76530SGrygorii Strashko 21693a76530SGrygorii Strashko dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid); 2172d64a034SGrygorii Strashko ret = cpsw_ale_del_vlan(common->ale, vid, 2182d64a034SGrygorii Strashko BIT(port->port_id) | ALE_PORT_HOST); 21993a76530SGrygorii Strashko 22093a76530SGrygorii Strashko pm_runtime_put(common->dev); 22193a76530SGrygorii Strashko return ret; 22293a76530SGrygorii Strashko } 22393a76530SGrygorii Strashko 22484b4aa49SGrygorii Strashko static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port, 22593a76530SGrygorii Strashko bool promisc) 22693a76530SGrygorii Strashko { 22793a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 22893a76530SGrygorii Strashko 22986e8b070SVignesh Raghavendra if (promisc && !common->is_emac_mode) { 23086e8b070SVignesh Raghavendra dev_dbg(common->dev, "promisc mode requested in switch mode"); 23186e8b070SVignesh Raghavendra return; 23286e8b070SVignesh Raghavendra } 23386e8b070SVignesh Raghavendra 23493a76530SGrygorii Strashko if (promisc) { 23593a76530SGrygorii Strashko /* Enable promiscuous mode */ 23693a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 23793a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 1); 23893a76530SGrygorii Strashko dev_dbg(common->dev, "promisc enabled\n"); 23993a76530SGrygorii Strashko } else { 24093a76530SGrygorii Strashko /* Disable promiscuous mode */ 24193a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 24293a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 0); 24393a76530SGrygorii Strashko dev_dbg(common->dev, "promisc disabled\n"); 24493a76530SGrygorii Strashko } 24593a76530SGrygorii Strashko } 24693a76530SGrygorii Strashko 24793a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev) 24893a76530SGrygorii Strashko { 24993a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 25093a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 25193a76530SGrygorii Strashko u32 port_mask; 25293a76530SGrygorii Strashko bool promisc; 25393a76530SGrygorii Strashko 25493a76530SGrygorii Strashko promisc = !!(ndev->flags & IFF_PROMISC); 25584b4aa49SGrygorii Strashko am65_cpsw_slave_set_promisc(port, promisc); 25693a76530SGrygorii Strashko 25793a76530SGrygorii Strashko if (promisc) 25893a76530SGrygorii Strashko return; 25993a76530SGrygorii Strashko 26093a76530SGrygorii Strashko /* Restore allmulti on vlans if necessary */ 26193a76530SGrygorii Strashko cpsw_ale_set_allmulti(common->ale, 26293a76530SGrygorii Strashko ndev->flags & IFF_ALLMULTI, port->port_id); 26393a76530SGrygorii Strashko 26493a76530SGrygorii Strashko port_mask = ALE_PORT_HOST; 26593a76530SGrygorii Strashko /* Clear all mcast from ALE */ 26693a76530SGrygorii Strashko cpsw_ale_flush_multicast(common->ale, port_mask, -1); 26793a76530SGrygorii Strashko 26893a76530SGrygorii Strashko if (!netdev_mc_empty(ndev)) { 26993a76530SGrygorii Strashko struct netdev_hw_addr *ha; 27093a76530SGrygorii Strashko 27193a76530SGrygorii Strashko /* program multicast address list into ALE register */ 27293a76530SGrygorii Strashko netdev_for_each_mc_addr(ha, ndev) { 27393a76530SGrygorii Strashko cpsw_ale_add_mcast(common->ale, ha->addr, 27493a76530SGrygorii Strashko port_mask, 0, 0, 0); 27593a76530SGrygorii Strashko } 27693a76530SGrygorii Strashko } 27793a76530SGrygorii Strashko } 27893a76530SGrygorii Strashko 27993a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev, 28093a76530SGrygorii Strashko unsigned int txqueue) 28193a76530SGrygorii Strashko { 28293a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 28393a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 28493a76530SGrygorii Strashko struct netdev_queue *netif_txq; 28593a76530SGrygorii Strashko unsigned long trans_start; 28693a76530SGrygorii Strashko 28793a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, txqueue); 28893a76530SGrygorii Strashko tx_chn = &common->tx_chns[txqueue]; 2895337824fSEric Dumazet trans_start = READ_ONCE(netif_txq->trans_start); 29093a76530SGrygorii Strashko 29193a76530SGrygorii Strashko netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n", 29293a76530SGrygorii Strashko txqueue, 29393a76530SGrygorii Strashko netif_tx_queue_stopped(netif_txq), 29493a76530SGrygorii Strashko jiffies_to_msecs(jiffies - trans_start), 29593a76530SGrygorii Strashko dql_avail(&netif_txq->dql), 29693a76530SGrygorii Strashko k3_cppi_desc_pool_avail(tx_chn->desc_pool)); 29793a76530SGrygorii Strashko 29893a76530SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 29993a76530SGrygorii Strashko /* try recover if stopped by us */ 30093a76530SGrygorii Strashko txq_trans_update(netif_txq); 30193a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 30293a76530SGrygorii Strashko } 30393a76530SGrygorii Strashko } 30493a76530SGrygorii Strashko 30593a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common, 30693a76530SGrygorii Strashko struct sk_buff *skb) 30793a76530SGrygorii Strashko { 30893a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 30993a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 31093a76530SGrygorii Strashko struct device *dev = common->dev; 31193a76530SGrygorii Strashko u32 pkt_len = skb_tailroom(skb); 31293a76530SGrygorii Strashko dma_addr_t desc_dma; 31393a76530SGrygorii Strashko dma_addr_t buf_dma; 31493a76530SGrygorii Strashko void *swdata; 31593a76530SGrygorii Strashko 31693a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool); 31793a76530SGrygorii Strashko if (!desc_rx) { 31893a76530SGrygorii Strashko dev_err(dev, "Failed to allocate RXFDQ descriptor\n"); 31993a76530SGrygorii Strashko return -ENOMEM; 32093a76530SGrygorii Strashko } 32193a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx); 32293a76530SGrygorii Strashko 323ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len, 324ed569ed9SPeter Ujfalusi DMA_FROM_DEVICE); 325ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) { 32693a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 32793a76530SGrygorii Strashko dev_err(dev, "Failed to map rx skb buffer\n"); 32893a76530SGrygorii Strashko return -EINVAL; 32993a76530SGrygorii Strashko } 33093a76530SGrygorii Strashko 33193a76530SGrygorii Strashko cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT, 33293a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 33339fd0547SPeter Ujfalusi k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma); 334a9e60cf0SGrygorii Strashko cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb)); 33593a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 33693a76530SGrygorii Strashko *((void **)swdata) = skb; 33793a76530SGrygorii Strashko 33893a76530SGrygorii Strashko return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma); 33993a76530SGrygorii Strashko } 34093a76530SGrygorii Strashko 34193a76530SGrygorii Strashko void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common) 34293a76530SGrygorii Strashko { 34393a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 34493a76530SGrygorii Strashko u32 val, pri_map; 34593a76530SGrygorii Strashko 34693a76530SGrygorii Strashko /* P0 set Receive Priority Type */ 34793a76530SGrygorii Strashko val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 34893a76530SGrygorii Strashko 34993a76530SGrygorii Strashko if (common->pf_p0_rx_ptype_rrobin) { 35093a76530SGrygorii Strashko val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 35193a76530SGrygorii Strashko /* Enet Ports fifos works in fixed priority mode only, so 35293a76530SGrygorii Strashko * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0 35393a76530SGrygorii Strashko */ 35493a76530SGrygorii Strashko pri_map = 0x0; 35593a76530SGrygorii Strashko } else { 35693a76530SGrygorii Strashko val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 35793a76530SGrygorii Strashko /* restore P0_Rx_Pri_Map */ 35893a76530SGrygorii Strashko pri_map = 0x76543210; 35993a76530SGrygorii Strashko } 36093a76530SGrygorii Strashko 36193a76530SGrygorii Strashko writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP); 36293a76530SGrygorii Strashko writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 36393a76530SGrygorii Strashko } 36493a76530SGrygorii Strashko 36558356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common); 36658356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common); 36758356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port); 36858356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port); 36958356eb3SVignesh Raghavendra 3703342a10fSJian Shen static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common) 37193a76530SGrygorii Strashko { 37293a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 37393a76530SGrygorii Strashko int port_idx, i, ret; 37493a76530SGrygorii Strashko struct sk_buff *skb; 37593a76530SGrygorii Strashko u32 val, port_mask; 37693a76530SGrygorii Strashko 37793a76530SGrygorii Strashko if (common->usage_count) 37893a76530SGrygorii Strashko return 0; 37993a76530SGrygorii Strashko 38093a76530SGrygorii Strashko /* Control register */ 38193a76530SGrygorii Strashko writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE | 38293a76530SGrygorii Strashko AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD, 38393a76530SGrygorii Strashko common->cpsw_base + AM65_CPSW_REG_CTL); 38493a76530SGrygorii Strashko /* Max length register */ 38593a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 38693a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 38793a76530SGrygorii Strashko /* set base flow_id */ 38893a76530SGrygorii Strashko writel(common->rx_flow_id_base, 38993a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET); 39086e2eca4SGrygorii Strashko writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN | AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN, 39186e2eca4SGrygorii Strashko host_p->port_base + AM65_CPSW_P0_REG_CTL); 39293a76530SGrygorii Strashko 39393a76530SGrygorii Strashko am65_cpsw_nuss_set_p0_ptype(common); 39493a76530SGrygorii Strashko 39593a76530SGrygorii Strashko /* enable statistic */ 39693a76530SGrygorii Strashko val = BIT(HOST_PORT_NUM); 39793a76530SGrygorii Strashko for (port_idx = 0; port_idx < common->port_num; port_idx++) { 39893a76530SGrygorii Strashko struct am65_cpsw_port *port = &common->ports[port_idx]; 39993a76530SGrygorii Strashko 40093a76530SGrygorii Strashko if (!port->disabled) 40193a76530SGrygorii Strashko val |= BIT(port->port_id); 40293a76530SGrygorii Strashko } 40393a76530SGrygorii Strashko writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 40493a76530SGrygorii Strashko 40593a76530SGrygorii Strashko /* disable priority elevation */ 40693a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE); 40793a76530SGrygorii Strashko 40893a76530SGrygorii Strashko cpsw_ale_start(common->ale); 40993a76530SGrygorii Strashko 41093a76530SGrygorii Strashko /* limit to one RX flow only */ 41193a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 41293a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ID, 0); 41393a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 41493a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ENABLE, 1); 41593a76530SGrygorii Strashko /* switch to vlan unaware mode */ 41693a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1); 41793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 41893a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 41993a76530SGrygorii Strashko 42093a76530SGrygorii Strashko /* default vlan cfg: create mask based on enabled ports */ 42193a76530SGrygorii Strashko port_mask = GENMASK(common->port_num, 0) & 42293a76530SGrygorii Strashko ~common->disabled_ports_mask; 42393a76530SGrygorii Strashko 42493a76530SGrygorii Strashko cpsw_ale_add_vlan(common->ale, 0, port_mask, 42593a76530SGrygorii Strashko port_mask, port_mask, 42693a76530SGrygorii Strashko port_mask & ~ALE_PORT_HOST); 42793a76530SGrygorii Strashko 42858356eb3SVignesh Raghavendra if (common->is_emac_mode) 42958356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(common); 43058356eb3SVignesh Raghavendra else 43158356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(common); 43258356eb3SVignesh Raghavendra 4335c8560c4SGrygorii Strashko am65_cpsw_qos_tx_p0_rate_init(common); 4345c8560c4SGrygorii Strashko 43593a76530SGrygorii Strashko for (i = 0; i < common->rx_chns.descs_num; i++) { 43693a76530SGrygorii Strashko skb = __netdev_alloc_skb_ip_align(NULL, 43793a76530SGrygorii Strashko AM65_CPSW_MAX_PACKET_SIZE, 43893a76530SGrygorii Strashko GFP_KERNEL); 43993a76530SGrygorii Strashko if (!skb) { 44093a76530SGrygorii Strashko dev_err(common->dev, "cannot allocate skb\n"); 4411a014663SRoger Quadros return -ENOMEM; 44293a76530SGrygorii Strashko } 44393a76530SGrygorii Strashko 44493a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, skb); 44593a76530SGrygorii Strashko if (ret < 0) { 44693a76530SGrygorii Strashko dev_err(common->dev, 44793a76530SGrygorii Strashko "cannot submit skb to channel rx, error %d\n", 44893a76530SGrygorii Strashko ret); 44993a76530SGrygorii Strashko kfree_skb(skb); 4501a014663SRoger Quadros return ret; 45193a76530SGrygorii Strashko } 45293a76530SGrygorii Strashko kmemleak_not_leak(skb); 45393a76530SGrygorii Strashko } 45493a76530SGrygorii Strashko k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn); 45593a76530SGrygorii Strashko 45693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 45793a76530SGrygorii Strashko ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn); 45893a76530SGrygorii Strashko if (ret) 4591a014663SRoger Quadros return ret; 46093a76530SGrygorii Strashko napi_enable(&common->tx_chns[i].napi_tx); 46193a76530SGrygorii Strashko } 46293a76530SGrygorii Strashko 46393a76530SGrygorii Strashko napi_enable(&common->napi_rx); 46447bfc4d1SVignesh Raghavendra if (common->rx_irq_disabled) { 46547bfc4d1SVignesh Raghavendra common->rx_irq_disabled = false; 46647bfc4d1SVignesh Raghavendra enable_irq(common->rx_chns.irq); 46747bfc4d1SVignesh Raghavendra } 46893a76530SGrygorii Strashko 46993a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss started\n"); 47093a76530SGrygorii Strashko return 0; 47193a76530SGrygorii Strashko } 47293a76530SGrygorii Strashko 47393a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma); 47493a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma); 47593a76530SGrygorii Strashko 47693a76530SGrygorii Strashko static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common) 47793a76530SGrygorii Strashko { 47893a76530SGrygorii Strashko int i; 47993a76530SGrygorii Strashko 48093a76530SGrygorii Strashko if (common->usage_count != 1) 48193a76530SGrygorii Strashko return 0; 48293a76530SGrygorii Strashko 48393a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 48493a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 48593a76530SGrygorii Strashko 48693a76530SGrygorii Strashko /* shutdown tx channels */ 48793a76530SGrygorii Strashko atomic_set(&common->tdown_cnt, common->tx_ch_num); 48893a76530SGrygorii Strashko /* ensure new tdown_cnt value is visible */ 48993a76530SGrygorii Strashko smp_mb__after_atomic(); 49093a76530SGrygorii Strashko reinit_completion(&common->tdown_complete); 49193a76530SGrygorii Strashko 49293a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 49393a76530SGrygorii Strashko k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false); 49493a76530SGrygorii Strashko 49593a76530SGrygorii Strashko i = wait_for_completion_timeout(&common->tdown_complete, 49693a76530SGrygorii Strashko msecs_to_jiffies(1000)); 49793a76530SGrygorii Strashko if (!i) 49893a76530SGrygorii Strashko dev_err(common->dev, "tx timeout\n"); 49993a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 50093a76530SGrygorii Strashko napi_disable(&common->tx_chns[i].napi_tx); 50193a76530SGrygorii Strashko 50293a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 50393a76530SGrygorii Strashko k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn, 50493a76530SGrygorii Strashko &common->tx_chns[i], 50593a76530SGrygorii Strashko am65_cpsw_nuss_tx_cleanup); 50693a76530SGrygorii Strashko k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn); 50793a76530SGrygorii Strashko } 50893a76530SGrygorii Strashko 5090ed577e7SSiddharth Vadapalli reinit_completion(&common->tdown_complete); 51093a76530SGrygorii Strashko k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true); 5110ed577e7SSiddharth Vadapalli 5120ed577e7SSiddharth Vadapalli if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) { 5130ed577e7SSiddharth Vadapalli i = wait_for_completion_timeout(&common->tdown_complete, msecs_to_jiffies(1000)); 5140ed577e7SSiddharth Vadapalli if (!i) 5150ed577e7SSiddharth Vadapalli dev_err(common->dev, "rx teardown timeout\n"); 5160ed577e7SSiddharth Vadapalli } 5170ed577e7SSiddharth Vadapalli 51893a76530SGrygorii Strashko napi_disable(&common->napi_rx); 51993a76530SGrygorii Strashko 52093a76530SGrygorii Strashko for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++) 52193a76530SGrygorii Strashko k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i, 52293a76530SGrygorii Strashko &common->rx_chns, 52393a76530SGrygorii Strashko am65_cpsw_nuss_rx_cleanup, !!i); 52493a76530SGrygorii Strashko 52593a76530SGrygorii Strashko k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn); 52693a76530SGrygorii Strashko 52793a76530SGrygorii Strashko cpsw_ale_stop(common->ale); 52893a76530SGrygorii Strashko 52993a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_CTL); 53093a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 53193a76530SGrygorii Strashko 53293a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss stopped\n"); 53393a76530SGrygorii Strashko return 0; 53493a76530SGrygorii Strashko } 53593a76530SGrygorii Strashko 53693a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev) 53793a76530SGrygorii Strashko { 53893a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 53993a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 54093a76530SGrygorii Strashko int ret; 54193a76530SGrygorii Strashko 542e8609e69SSiddharth Vadapalli phylink_stop(port->slave.phylink); 54393a76530SGrygorii Strashko 54493a76530SGrygorii Strashko netif_tx_stop_all_queues(ndev); 54593a76530SGrygorii Strashko 546e8609e69SSiddharth Vadapalli phylink_disconnect_phy(port->slave.phylink); 54793a76530SGrygorii Strashko 54893a76530SGrygorii Strashko ret = am65_cpsw_nuss_common_stop(common); 54993a76530SGrygorii Strashko if (ret) 55093a76530SGrygorii Strashko return ret; 55193a76530SGrygorii Strashko 55293a76530SGrygorii Strashko common->usage_count--; 55393a76530SGrygorii Strashko pm_runtime_put(common->dev); 55493a76530SGrygorii Strashko return 0; 55593a76530SGrygorii Strashko } 55693a76530SGrygorii Strashko 5577bcffde0SGrygorii Strashko static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) 5587bcffde0SGrygorii Strashko { 5597bcffde0SGrygorii Strashko struct am65_cpsw_port *port = arg; 5607bcffde0SGrygorii Strashko 5617bcffde0SGrygorii Strashko if (!vdev) 5627bcffde0SGrygorii Strashko return 0; 5637bcffde0SGrygorii Strashko 5647bcffde0SGrygorii Strashko return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid); 5657bcffde0SGrygorii Strashko } 5667bcffde0SGrygorii Strashko 56793a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev) 56893a76530SGrygorii Strashko { 56993a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 57093a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 57193a76530SGrygorii Strashko int ret, i; 57224bc19b0SRoger Quadros u32 reg; 57393a76530SGrygorii Strashko 5742240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 5752240514cSMinghao Chi if (ret < 0) 57693a76530SGrygorii Strashko return ret; 57793a76530SGrygorii Strashko 57824bc19b0SRoger Quadros /* Idle MAC port */ 57924bc19b0SRoger Quadros cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 58024bc19b0SRoger Quadros cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 58124bc19b0SRoger Quadros cpsw_sl_ctl_reset(port->slave.mac_sl); 58224bc19b0SRoger Quadros 58324bc19b0SRoger Quadros /* soft reset MAC */ 58424bc19b0SRoger Quadros cpsw_sl_reg_write(port->slave.mac_sl, CPSW_SL_SOFT_RESET, 1); 58524bc19b0SRoger Quadros mdelay(1); 58624bc19b0SRoger Quadros reg = cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_SOFT_RESET); 58724bc19b0SRoger Quadros if (reg) { 58824bc19b0SRoger Quadros dev_err(common->dev, "soft RESET didn't complete\n"); 5895821504fSRoger Quadros ret = -ETIMEDOUT; 5905821504fSRoger Quadros goto runtime_put; 59124bc19b0SRoger Quadros } 59224bc19b0SRoger Quadros 59393a76530SGrygorii Strashko /* Notify the stack of the actual queue counts. */ 59493a76530SGrygorii Strashko ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num); 59593a76530SGrygorii Strashko if (ret) { 59693a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of tx queues\n"); 5975821504fSRoger Quadros goto runtime_put; 59893a76530SGrygorii Strashko } 59993a76530SGrygorii Strashko 60093a76530SGrygorii Strashko ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES); 60193a76530SGrygorii Strashko if (ret) { 60293a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of rx queues\n"); 6035821504fSRoger Quadros goto runtime_put; 60493a76530SGrygorii Strashko } 60593a76530SGrygorii Strashko 6065c8560c4SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 6075c8560c4SGrygorii Strashko struct netdev_queue *txq = netdev_get_tx_queue(ndev, i); 6085c8560c4SGrygorii Strashko 6095c8560c4SGrygorii Strashko netdev_tx_reset_queue(txq); 6105c8560c4SGrygorii Strashko txq->tx_maxrate = common->tx_chns[i].rate_mbps; 6115c8560c4SGrygorii Strashko } 61293a76530SGrygorii Strashko 6133342a10fSJian Shen ret = am65_cpsw_nuss_common_open(common); 61493a76530SGrygorii Strashko if (ret) 6155821504fSRoger Quadros goto runtime_put; 61693a76530SGrygorii Strashko 61793a76530SGrygorii Strashko common->usage_count++; 61893a76530SGrygorii Strashko 61993a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, ndev->dev_addr); 62093a76530SGrygorii Strashko 62158356eb3SVignesh Raghavendra if (common->is_emac_mode) 62258356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 62358356eb3SVignesh Raghavendra else 62458356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 62593a76530SGrygorii Strashko 62693a76530SGrygorii Strashko /* mac_sl should be configured via phy-link interface */ 62793a76530SGrygorii Strashko am65_cpsw_sl_ctl_reset(port); 62893a76530SGrygorii Strashko 629e8609e69SSiddharth Vadapalli ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0); 630e8609e69SSiddharth Vadapalli if (ret) 63193a76530SGrygorii Strashko goto error_cleanup; 63293a76530SGrygorii Strashko 6337bcffde0SGrygorii Strashko /* restore vlan configurations */ 6347bcffde0SGrygorii Strashko vlan_for_each(ndev, cpsw_restore_vlans, port); 6357bcffde0SGrygorii Strashko 636e8609e69SSiddharth Vadapalli phylink_start(port->slave.phylink); 63793a76530SGrygorii Strashko 63893a76530SGrygorii Strashko return 0; 63993a76530SGrygorii Strashko 64093a76530SGrygorii Strashko error_cleanup: 64193a76530SGrygorii Strashko am65_cpsw_nuss_ndo_slave_stop(ndev); 64293a76530SGrygorii Strashko return ret; 6435821504fSRoger Quadros 6445821504fSRoger Quadros runtime_put: 6455821504fSRoger Quadros pm_runtime_put(common->dev); 6465821504fSRoger Quadros return ret; 64793a76530SGrygorii Strashko } 64893a76530SGrygorii Strashko 64993a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma) 65093a76530SGrygorii Strashko { 65193a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = data; 65293a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 65393a76530SGrygorii Strashko struct sk_buff *skb; 65493a76530SGrygorii Strashko dma_addr_t buf_dma; 65593a76530SGrygorii Strashko u32 buf_dma_len; 65693a76530SGrygorii Strashko void **swdata; 65793a76530SGrygorii Strashko 65893a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 65993a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 66093a76530SGrygorii Strashko skb = *swdata; 66193a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 66239fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 66393a76530SGrygorii Strashko 664ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 66593a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 66693a76530SGrygorii Strashko 66793a76530SGrygorii Strashko dev_kfree_skb_any(skb); 66893a76530SGrygorii Strashko } 66993a76530SGrygorii Strashko 670b1f66a5bSGrygorii Strashko static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata) 671b1f66a5bSGrygorii Strashko { 672b1f66a5bSGrygorii Strashko struct skb_shared_hwtstamps *ssh; 673b1f66a5bSGrygorii Strashko u64 ns; 674b1f66a5bSGrygorii Strashko 675b1f66a5bSGrygorii Strashko ns = ((u64)psdata[1] << 32) | psdata[0]; 676b1f66a5bSGrygorii Strashko 677b1f66a5bSGrygorii Strashko ssh = skb_hwtstamps(skb); 678b1f66a5bSGrygorii Strashko memset(ssh, 0, sizeof(*ssh)); 679b1f66a5bSGrygorii Strashko ssh->hwtstamp = ns_to_ktime(ns); 680b1f66a5bSGrygorii Strashko } 681b1f66a5bSGrygorii Strashko 68293a76530SGrygorii Strashko /* RX psdata[2] word format - checksum information */ 68393a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0) 68493a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16) 68593a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17) 68693a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_TCP BIT(18) 68793a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19) 68893a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20) 68993a76530SGrygorii Strashko 69093a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info) 69193a76530SGrygorii Strashko { 69293a76530SGrygorii Strashko /* HW can verify IPv4/IPv6 TCP/UDP packets checksum 69393a76530SGrygorii Strashko * csum information provides in psdata[2] word: 69493a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error 69593a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID 69693a76530SGrygorii Strashko * bits - indicates IPv4/IPv6 packet 69793a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet 69893a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets 69993a76530SGrygorii Strashko * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR 70093a76530SGrygorii Strashko */ 70193a76530SGrygorii Strashko skb_checksum_none_assert(skb); 70293a76530SGrygorii Strashko 70393a76530SGrygorii Strashko if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) 70493a76530SGrygorii Strashko return; 70593a76530SGrygorii Strashko 70693a76530SGrygorii Strashko if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID | 70793a76530SGrygorii Strashko AM65_CPSW_RX_PSD_IPV4_VALID)) && 70893a76530SGrygorii Strashko !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) { 70993a76530SGrygorii Strashko /* csum for fragmented packets is unsupported */ 71093a76530SGrygorii Strashko if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT)) 71193a76530SGrygorii Strashko skb->ip_summed = CHECKSUM_UNNECESSARY; 71293a76530SGrygorii Strashko } 71393a76530SGrygorii Strashko } 71493a76530SGrygorii Strashko 71593a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common, 71693a76530SGrygorii Strashko u32 flow_idx) 71793a76530SGrygorii Strashko { 71893a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 71993a76530SGrygorii Strashko u32 buf_dma_len, pkt_len, port_id = 0, csum_info; 72093a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 72193a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 72293a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 72393a76530SGrygorii Strashko struct device *dev = common->dev; 72493a76530SGrygorii Strashko struct sk_buff *skb, *new_skb; 72593a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 72693a76530SGrygorii Strashko struct am65_cpsw_port *port; 72793a76530SGrygorii Strashko struct net_device *ndev; 72893a76530SGrygorii Strashko void **swdata; 72993a76530SGrygorii Strashko u32 *psdata; 73093a76530SGrygorii Strashko int ret = 0; 73193a76530SGrygorii Strashko 73293a76530SGrygorii Strashko ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma); 73393a76530SGrygorii Strashko if (ret) { 73493a76530SGrygorii Strashko if (ret != -ENODATA) 73593a76530SGrygorii Strashko dev_err(dev, "RX: pop chn fail %d\n", ret); 73693a76530SGrygorii Strashko return ret; 73793a76530SGrygorii Strashko } 73893a76530SGrygorii Strashko 7396a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 74093a76530SGrygorii Strashko dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx); 7410ed577e7SSiddharth Vadapalli if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) 7420ed577e7SSiddharth Vadapalli complete(&common->tdown_complete); 74393a76530SGrygorii Strashko return 0; 74493a76530SGrygorii Strashko } 74593a76530SGrygorii Strashko 74693a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 74793a76530SGrygorii Strashko dev_dbg(dev, "%s flow_idx: %u desc %pad\n", 74893a76530SGrygorii Strashko __func__, flow_idx, &desc_dma); 74993a76530SGrygorii Strashko 75093a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 75193a76530SGrygorii Strashko skb = *swdata; 75293a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 75339fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 75493a76530SGrygorii Strashko pkt_len = cppi5_hdesc_get_pktlen(desc_rx); 75593a76530SGrygorii Strashko cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL); 75693a76530SGrygorii Strashko dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id); 75793a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 75893a76530SGrygorii Strashko ndev = port->ndev; 75993a76530SGrygorii Strashko skb->dev = ndev; 76093a76530SGrygorii Strashko 76193a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(desc_rx); 762b1f66a5bSGrygorii Strashko /* add RX timestamp */ 763b1f66a5bSGrygorii Strashko if (port->rx_ts_enabled) 764b1f66a5bSGrygorii Strashko am65_cpsw_nuss_rx_ts(skb, psdata); 76593a76530SGrygorii Strashko csum_info = psdata[2]; 76693a76530SGrygorii Strashko dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info); 76793a76530SGrygorii Strashko 768ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 76993a76530SGrygorii Strashko 77093a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 77193a76530SGrygorii Strashko 77293a76530SGrygorii Strashko new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE); 77393a76530SGrygorii Strashko if (new_skb) { 77486e8b070SVignesh Raghavendra ndev_priv = netdev_priv(ndev); 77586e8b070SVignesh Raghavendra am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark); 77693a76530SGrygorii Strashko skb_put(skb, pkt_len); 77793a76530SGrygorii Strashko skb->protocol = eth_type_trans(skb, ndev); 77893a76530SGrygorii Strashko am65_cpsw_nuss_rx_csum(skb, csum_info); 77993a76530SGrygorii Strashko napi_gro_receive(&common->napi_rx, skb); 78093a76530SGrygorii Strashko 78193a76530SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 78293a76530SGrygorii Strashko 78393a76530SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 78493a76530SGrygorii Strashko stats->rx_packets++; 78593a76530SGrygorii Strashko stats->rx_bytes += pkt_len; 78693a76530SGrygorii Strashko u64_stats_update_end(&stats->syncp); 78793a76530SGrygorii Strashko kmemleak_not_leak(new_skb); 78893a76530SGrygorii Strashko } else { 78993a76530SGrygorii Strashko ndev->stats.rx_dropped++; 79093a76530SGrygorii Strashko new_skb = skb; 79193a76530SGrygorii Strashko } 79293a76530SGrygorii Strashko 79393a76530SGrygorii Strashko if (netif_dormant(ndev)) { 79493a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 79593a76530SGrygorii Strashko ndev->stats.rx_dropped++; 79693a76530SGrygorii Strashko return 0; 79793a76530SGrygorii Strashko } 79893a76530SGrygorii Strashko 79993a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, new_skb); 80093a76530SGrygorii Strashko if (WARN_ON(ret < 0)) { 80193a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 80293a76530SGrygorii Strashko ndev->stats.rx_errors++; 80393a76530SGrygorii Strashko ndev->stats.rx_dropped++; 80493a76530SGrygorii Strashko } 80593a76530SGrygorii Strashko 80693a76530SGrygorii Strashko return ret; 80793a76530SGrygorii Strashko } 80893a76530SGrygorii Strashko 80993a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget) 81093a76530SGrygorii Strashko { 81193a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx); 81293a76530SGrygorii Strashko int flow = AM65_CPSW_MAX_RX_FLOWS; 81393a76530SGrygorii Strashko int cur_budget, ret; 81493a76530SGrygorii Strashko int num_rx = 0; 81593a76530SGrygorii Strashko 81693a76530SGrygorii Strashko /* process every flow */ 81793a76530SGrygorii Strashko while (flow--) { 81893a76530SGrygorii Strashko cur_budget = budget - num_rx; 81993a76530SGrygorii Strashko 82093a76530SGrygorii Strashko while (cur_budget--) { 82193a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_packets(common, flow); 82293a76530SGrygorii Strashko if (ret) 82393a76530SGrygorii Strashko break; 82493a76530SGrygorii Strashko num_rx++; 82593a76530SGrygorii Strashko } 82693a76530SGrygorii Strashko 82793a76530SGrygorii Strashko if (num_rx >= budget) 82893a76530SGrygorii Strashko break; 82993a76530SGrygorii Strashko } 83093a76530SGrygorii Strashko 83193a76530SGrygorii Strashko dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget); 83293a76530SGrygorii Strashko 83347bfc4d1SVignesh Raghavendra if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) { 83447bfc4d1SVignesh Raghavendra if (common->rx_irq_disabled) { 83547bfc4d1SVignesh Raghavendra common->rx_irq_disabled = false; 83693a76530SGrygorii Strashko enable_irq(common->rx_chns.irq); 83747bfc4d1SVignesh Raghavendra } 83847bfc4d1SVignesh Raghavendra } 83993a76530SGrygorii Strashko 84093a76530SGrygorii Strashko return num_rx; 84193a76530SGrygorii Strashko } 84293a76530SGrygorii Strashko 84393a76530SGrygorii Strashko static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn, 84493a76530SGrygorii Strashko struct cppi5_host_desc_t *desc) 84593a76530SGrygorii Strashko { 84693a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc; 84793a76530SGrygorii Strashko dma_addr_t buf_dma, next_desc_dma; 84893a76530SGrygorii Strashko u32 buf_dma_len; 84993a76530SGrygorii Strashko 85093a76530SGrygorii Strashko first_desc = desc; 85193a76530SGrygorii Strashko next_desc = first_desc; 85293a76530SGrygorii Strashko 85393a76530SGrygorii Strashko cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len); 85439fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 85593a76530SGrygorii Strashko 856ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE); 85793a76530SGrygorii Strashko 85893a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc); 85939fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 86093a76530SGrygorii Strashko while (next_desc_dma) { 86193a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 86293a76530SGrygorii Strashko next_desc_dma); 86393a76530SGrygorii Strashko cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len); 86439fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 86593a76530SGrygorii Strashko 866ed569ed9SPeter Ujfalusi dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len, 86793a76530SGrygorii Strashko DMA_TO_DEVICE); 86893a76530SGrygorii Strashko 86993a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc); 87039fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 87193a76530SGrygorii Strashko 87293a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 87393a76530SGrygorii Strashko } 87493a76530SGrygorii Strashko 87593a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc); 87693a76530SGrygorii Strashko } 87793a76530SGrygorii Strashko 87893a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma) 87993a76530SGrygorii Strashko { 88093a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = data; 88193a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 88293a76530SGrygorii Strashko struct sk_buff *skb; 88393a76530SGrygorii Strashko void **swdata; 88493a76530SGrygorii Strashko 88593a76530SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma); 88693a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 88793a76530SGrygorii Strashko skb = *(swdata); 888ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 88993a76530SGrygorii Strashko 89093a76530SGrygorii Strashko dev_kfree_skb_any(skb); 89193a76530SGrygorii Strashko } 89293a76530SGrygorii Strashko 893a9e60cf0SGrygorii Strashko static struct sk_buff * 894a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn, 895a9e60cf0SGrygorii Strashko dma_addr_t desc_dma) 896a9e60cf0SGrygorii Strashko { 897a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 898a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 899a9e60cf0SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 900a9e60cf0SGrygorii Strashko struct net_device *ndev; 901a9e60cf0SGrygorii Strashko struct sk_buff *skb; 902a9e60cf0SGrygorii Strashko void **swdata; 903a9e60cf0SGrygorii Strashko 904a9e60cf0SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 905a9e60cf0SGrygorii Strashko desc_dma); 906a9e60cf0SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 907a9e60cf0SGrygorii Strashko skb = *(swdata); 908ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 909a9e60cf0SGrygorii Strashko 910a9e60cf0SGrygorii Strashko ndev = skb->dev; 911a9e60cf0SGrygorii Strashko 912a9e60cf0SGrygorii Strashko am65_cpts_tx_timestamp(tx_chn->common->cpts, skb); 913a9e60cf0SGrygorii Strashko 914a9e60cf0SGrygorii Strashko ndev_priv = netdev_priv(ndev); 915a9e60cf0SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 916a9e60cf0SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 917a9e60cf0SGrygorii Strashko stats->tx_packets++; 918a9e60cf0SGrygorii Strashko stats->tx_bytes += skb->len; 919a9e60cf0SGrygorii Strashko u64_stats_update_end(&stats->syncp); 920a9e60cf0SGrygorii Strashko 921a9e60cf0SGrygorii Strashko return skb; 922a9e60cf0SGrygorii Strashko } 923a9e60cf0SGrygorii Strashko 924a9e60cf0SGrygorii Strashko static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev, 925a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq) 926a9e60cf0SGrygorii Strashko { 927a9e60cf0SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 928a9e60cf0SGrygorii Strashko /* Check whether the queue is stopped due to stalled 929a9e60cf0SGrygorii Strashko * tx dma, if the queue is stopped then wake the queue 930a9e60cf0SGrygorii Strashko * as we have free desc for tx 931a9e60cf0SGrygorii Strashko */ 932a9e60cf0SGrygorii Strashko __netif_tx_lock(netif_txq, smp_processor_id()); 933a9e60cf0SGrygorii Strashko if (netif_running(ndev) && 934a9e60cf0SGrygorii Strashko (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS)) 935a9e60cf0SGrygorii Strashko netif_tx_wake_queue(netif_txq); 936a9e60cf0SGrygorii Strashko 937a9e60cf0SGrygorii Strashko __netif_tx_unlock(netif_txq); 938a9e60cf0SGrygorii Strashko } 939a9e60cf0SGrygorii Strashko } 940a9e60cf0SGrygorii Strashko 94193a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common, 94293a76530SGrygorii Strashko int chn, unsigned int budget) 94393a76530SGrygorii Strashko { 94493a76530SGrygorii Strashko struct device *dev = common->dev; 94593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 94693a76530SGrygorii Strashko struct netdev_queue *netif_txq; 94793a76530SGrygorii Strashko unsigned int total_bytes = 0; 94893a76530SGrygorii Strashko struct net_device *ndev; 94993a76530SGrygorii Strashko struct sk_buff *skb; 95093a76530SGrygorii Strashko dma_addr_t desc_dma; 95193a76530SGrygorii Strashko int res, num_tx = 0; 95293a76530SGrygorii Strashko 95393a76530SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 95493a76530SGrygorii Strashko 95593a76530SGrygorii Strashko while (true) { 956a9e60cf0SGrygorii Strashko spin_lock(&tx_chn->lock); 957a9e60cf0SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 958a9e60cf0SGrygorii Strashko spin_unlock(&tx_chn->lock); 959a9e60cf0SGrygorii Strashko if (res == -ENODATA) 960a9e60cf0SGrygorii Strashko break; 96193a76530SGrygorii Strashko 962a9e60cf0SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 963a9e60cf0SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 964a9e60cf0SGrygorii Strashko complete(&common->tdown_complete); 965a9e60cf0SGrygorii Strashko break; 966a9e60cf0SGrygorii Strashko } 967a9e60cf0SGrygorii Strashko 968a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 969a9e60cf0SGrygorii Strashko total_bytes = skb->len; 970a9e60cf0SGrygorii Strashko ndev = skb->dev; 971a9e60cf0SGrygorii Strashko napi_consume_skb(skb, budget); 972a9e60cf0SGrygorii Strashko num_tx++; 973a9e60cf0SGrygorii Strashko 974a9e60cf0SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 975a9e60cf0SGrygorii Strashko 976a9e60cf0SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 977a9e60cf0SGrygorii Strashko 978a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 979a9e60cf0SGrygorii Strashko } 980a9e60cf0SGrygorii Strashko 981a9e60cf0SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 982a9e60cf0SGrygorii Strashko 983a9e60cf0SGrygorii Strashko return num_tx; 984a9e60cf0SGrygorii Strashko } 985a9e60cf0SGrygorii Strashko 986a9e60cf0SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common, 987a9e60cf0SGrygorii Strashko int chn, unsigned int budget) 988a9e60cf0SGrygorii Strashko { 989a9e60cf0SGrygorii Strashko struct device *dev = common->dev; 990a9e60cf0SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 991a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq; 992a9e60cf0SGrygorii Strashko unsigned int total_bytes = 0; 993a9e60cf0SGrygorii Strashko struct net_device *ndev; 994a9e60cf0SGrygorii Strashko struct sk_buff *skb; 995a9e60cf0SGrygorii Strashko dma_addr_t desc_dma; 996a9e60cf0SGrygorii Strashko int res, num_tx = 0; 997a9e60cf0SGrygorii Strashko 998a9e60cf0SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 999a9e60cf0SGrygorii Strashko 1000a9e60cf0SGrygorii Strashko while (true) { 100193a76530SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 100293a76530SGrygorii Strashko if (res == -ENODATA) 100393a76530SGrygorii Strashko break; 100493a76530SGrygorii Strashko 10056a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 100693a76530SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 100793a76530SGrygorii Strashko complete(&common->tdown_complete); 100893a76530SGrygorii Strashko break; 100993a76530SGrygorii Strashko } 101093a76530SGrygorii Strashko 1011a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 101293a76530SGrygorii Strashko 101393a76530SGrygorii Strashko ndev = skb->dev; 101493a76530SGrygorii Strashko total_bytes += skb->len; 101593a76530SGrygorii Strashko napi_consume_skb(skb, budget); 101693a76530SGrygorii Strashko num_tx++; 101793a76530SGrygorii Strashko } 101893a76530SGrygorii Strashko 101993a76530SGrygorii Strashko if (!num_tx) 102093a76530SGrygorii Strashko return 0; 102193a76530SGrygorii Strashko 102293a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 102393a76530SGrygorii Strashko 102493a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 102593a76530SGrygorii Strashko 1026a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 102793a76530SGrygorii Strashko 102893a76530SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 102993a76530SGrygorii Strashko 103093a76530SGrygorii Strashko return num_tx; 103193a76530SGrygorii Strashko } 103293a76530SGrygorii Strashko 103393a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget) 103493a76530SGrygorii Strashko { 103593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx); 103693a76530SGrygorii Strashko int num_tx; 103793a76530SGrygorii Strashko 1038a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(tx_chn->common)) 1039a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget); 1040a9e60cf0SGrygorii Strashko else 1041a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget); 1042a9e60cf0SGrygorii Strashko 10433bacbe04SGrygorii Strashko if (num_tx >= budget) 10443bacbe04SGrygorii Strashko return budget; 104593a76530SGrygorii Strashko 10463bacbe04SGrygorii Strashko if (napi_complete_done(napi_tx, num_tx)) 10473bacbe04SGrygorii Strashko enable_irq(tx_chn->irq); 10483bacbe04SGrygorii Strashko 10493bacbe04SGrygorii Strashko return 0; 105093a76530SGrygorii Strashko } 105193a76530SGrygorii Strashko 105293a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id) 105393a76530SGrygorii Strashko { 105493a76530SGrygorii Strashko struct am65_cpsw_common *common = dev_id; 105593a76530SGrygorii Strashko 105647bfc4d1SVignesh Raghavendra common->rx_irq_disabled = true; 105793a76530SGrygorii Strashko disable_irq_nosync(irq); 105893a76530SGrygorii Strashko napi_schedule(&common->napi_rx); 105993a76530SGrygorii Strashko 106093a76530SGrygorii Strashko return IRQ_HANDLED; 106193a76530SGrygorii Strashko } 106293a76530SGrygorii Strashko 106393a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id) 106493a76530SGrygorii Strashko { 106593a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = dev_id; 106693a76530SGrygorii Strashko 106793a76530SGrygorii Strashko disable_irq_nosync(irq); 106893a76530SGrygorii Strashko napi_schedule(&tx_chn->napi_tx); 106993a76530SGrygorii Strashko 107093a76530SGrygorii Strashko return IRQ_HANDLED; 107193a76530SGrygorii Strashko } 107293a76530SGrygorii Strashko 107393a76530SGrygorii Strashko static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb, 107493a76530SGrygorii Strashko struct net_device *ndev) 107593a76530SGrygorii Strashko { 107693a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 107793a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc; 107893a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 107993a76530SGrygorii Strashko struct device *dev = common->dev; 108093a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 108193a76530SGrygorii Strashko struct netdev_queue *netif_txq; 108293a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 108393a76530SGrygorii Strashko int ret, q_idx, i; 108493a76530SGrygorii Strashko void **swdata; 108593a76530SGrygorii Strashko u32 *psdata; 108693a76530SGrygorii Strashko u32 pkt_len; 108793a76530SGrygorii Strashko 108893a76530SGrygorii Strashko /* padding enabled in hw */ 108993a76530SGrygorii Strashko pkt_len = skb_headlen(skb); 109093a76530SGrygorii Strashko 1091b1f66a5bSGrygorii Strashko /* SKB TX timestamp */ 1092b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1093b1f66a5bSGrygorii Strashko am65_cpts_prep_tx_timestamp(common->cpts, skb); 1094b1f66a5bSGrygorii Strashko 109593a76530SGrygorii Strashko q_idx = skb_get_queue_mapping(skb); 109693a76530SGrygorii Strashko dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx); 109793a76530SGrygorii Strashko 109893a76530SGrygorii Strashko tx_chn = &common->tx_chns[q_idx]; 109993a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, q_idx); 110093a76530SGrygorii Strashko 110193a76530SGrygorii Strashko /* Map the linear buffer */ 1102ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len, 110393a76530SGrygorii Strashko DMA_TO_DEVICE); 1104ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 110593a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb buffer\n"); 110693a76530SGrygorii Strashko ndev->stats.tx_errors++; 110793a76530SGrygorii Strashko goto err_free_skb; 110893a76530SGrygorii Strashko } 110993a76530SGrygorii Strashko 111093a76530SGrygorii Strashko first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 111193a76530SGrygorii Strashko if (!first_desc) { 111293a76530SGrygorii Strashko dev_dbg(dev, "Failed to allocate descriptor\n"); 1113ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len, 1114ed569ed9SPeter Ujfalusi DMA_TO_DEVICE); 111593a76530SGrygorii Strashko goto busy_stop_q; 111693a76530SGrygorii Strashko } 111793a76530SGrygorii Strashko 111893a76530SGrygorii Strashko cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT, 111993a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 112093a76530SGrygorii Strashko cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF); 112193a76530SGrygorii Strashko cppi5_hdesc_set_pkttype(first_desc, 0x7); 112293a76530SGrygorii Strashko cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id); 112393a76530SGrygorii Strashko 112439fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 112593a76530SGrygorii Strashko cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len); 112693a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(first_desc); 112793a76530SGrygorii Strashko *(swdata) = skb; 112893a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(first_desc); 112993a76530SGrygorii Strashko 113093a76530SGrygorii Strashko /* HW csum offload if enabled */ 113193a76530SGrygorii Strashko psdata[2] = 0; 113293a76530SGrygorii Strashko if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) { 113393a76530SGrygorii Strashko unsigned int cs_start, cs_offset; 113493a76530SGrygorii Strashko 113593a76530SGrygorii Strashko cs_start = skb_transport_offset(skb); 113693a76530SGrygorii Strashko cs_offset = cs_start + skb->csum_offset; 113793a76530SGrygorii Strashko /* HW numerates bytes starting from 1 */ 113893a76530SGrygorii Strashko psdata[2] = ((cs_offset + 1) << 24) | 113993a76530SGrygorii Strashko ((cs_start + 1) << 16) | (skb->len - cs_start); 114093a76530SGrygorii Strashko dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]); 114193a76530SGrygorii Strashko } 114293a76530SGrygorii Strashko 114393a76530SGrygorii Strashko if (!skb_is_nonlinear(skb)) 114493a76530SGrygorii Strashko goto done_tx; 114593a76530SGrygorii Strashko 114693a76530SGrygorii Strashko dev_dbg(dev, "fragmented SKB\n"); 114793a76530SGrygorii Strashko 114893a76530SGrygorii Strashko /* Handle the case where skb is fragmented in pages */ 114993a76530SGrygorii Strashko cur_desc = first_desc; 115093a76530SGrygorii Strashko for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 115193a76530SGrygorii Strashko skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 115293a76530SGrygorii Strashko u32 frag_size = skb_frag_size(frag); 115393a76530SGrygorii Strashko 115493a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 115593a76530SGrygorii Strashko if (!next_desc) { 115693a76530SGrygorii Strashko dev_err(dev, "Failed to allocate descriptor\n"); 115793a76530SGrygorii Strashko goto busy_free_descs; 115893a76530SGrygorii Strashko } 115993a76530SGrygorii Strashko 1160ed569ed9SPeter Ujfalusi buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size, 116193a76530SGrygorii Strashko DMA_TO_DEVICE); 1162ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 116393a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb page\n"); 116493a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 116593a76530SGrygorii Strashko ndev->stats.tx_errors++; 116693a76530SGrygorii Strashko goto err_free_descs; 116793a76530SGrygorii Strashko } 116893a76530SGrygorii Strashko 116993a76530SGrygorii Strashko cppi5_hdesc_reset_hbdesc(next_desc); 117039fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 117193a76530SGrygorii Strashko cppi5_hdesc_attach_buf(next_desc, 117293a76530SGrygorii Strashko buf_dma, frag_size, buf_dma, frag_size); 117393a76530SGrygorii Strashko 117493a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, 117593a76530SGrygorii Strashko next_desc); 117639fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma); 117793a76530SGrygorii Strashko cppi5_hdesc_link_hbdesc(cur_desc, desc_dma); 117893a76530SGrygorii Strashko 117993a76530SGrygorii Strashko pkt_len += frag_size; 118093a76530SGrygorii Strashko cur_desc = next_desc; 118193a76530SGrygorii Strashko } 118293a76530SGrygorii Strashko WARN_ON(pkt_len != skb->len); 118393a76530SGrygorii Strashko 118493a76530SGrygorii Strashko done_tx: 118593a76530SGrygorii Strashko skb_tx_timestamp(skb); 118693a76530SGrygorii Strashko 118793a76530SGrygorii Strashko /* report bql before sending packet */ 118893a76530SGrygorii Strashko netdev_tx_sent_queue(netif_txq, pkt_len); 118993a76530SGrygorii Strashko 119093a76530SGrygorii Strashko cppi5_hdesc_set_pktlen(first_desc, pkt_len); 119193a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc); 1192a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(common)) { 119393a76530SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1194a9e60cf0SGrygorii Strashko } else { 1195a9e60cf0SGrygorii Strashko spin_lock_bh(&tx_chn->lock); 1196a9e60cf0SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1197a9e60cf0SGrygorii Strashko spin_unlock_bh(&tx_chn->lock); 1198a9e60cf0SGrygorii Strashko } 119993a76530SGrygorii Strashko if (ret) { 120093a76530SGrygorii Strashko dev_err(dev, "can't push desc %d\n", ret); 120193a76530SGrygorii Strashko /* inform bql */ 120293a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, 1, pkt_len); 120393a76530SGrygorii Strashko ndev->stats.tx_errors++; 120493a76530SGrygorii Strashko goto err_free_descs; 120593a76530SGrygorii Strashko } 120693a76530SGrygorii Strashko 120793a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) { 120893a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 120993a76530SGrygorii Strashko /* Barrier, so that stop_queue visible to other cpus */ 121093a76530SGrygorii Strashko smp_mb__after_atomic(); 121193a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx); 121293a76530SGrygorii Strashko 121393a76530SGrygorii Strashko /* re-check for smp */ 121493a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= 121593a76530SGrygorii Strashko MAX_SKB_FRAGS) { 121693a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 121793a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx); 121893a76530SGrygorii Strashko } 121993a76530SGrygorii Strashko } 122093a76530SGrygorii Strashko 122193a76530SGrygorii Strashko return NETDEV_TX_OK; 122293a76530SGrygorii Strashko 122393a76530SGrygorii Strashko err_free_descs: 1224ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 122593a76530SGrygorii Strashko err_free_skb: 122693a76530SGrygorii Strashko ndev->stats.tx_dropped++; 122793a76530SGrygorii Strashko dev_kfree_skb_any(skb); 122893a76530SGrygorii Strashko return NETDEV_TX_OK; 122993a76530SGrygorii Strashko 123093a76530SGrygorii Strashko busy_free_descs: 1231ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 123293a76530SGrygorii Strashko busy_stop_q: 123393a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 123493a76530SGrygorii Strashko return NETDEV_TX_BUSY; 123593a76530SGrygorii Strashko } 123693a76530SGrygorii Strashko 123793a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev, 123893a76530SGrygorii Strashko void *addr) 123993a76530SGrygorii Strashko { 124093a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 124193a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 124293a76530SGrygorii Strashko struct sockaddr *sockaddr = (struct sockaddr *)addr; 124393a76530SGrygorii Strashko int ret; 124493a76530SGrygorii Strashko 124593a76530SGrygorii Strashko ret = eth_prepare_mac_addr_change(ndev, addr); 124693a76530SGrygorii Strashko if (ret < 0) 124793a76530SGrygorii Strashko return ret; 124893a76530SGrygorii Strashko 12492240514cSMinghao Chi ret = pm_runtime_resume_and_get(common->dev); 12502240514cSMinghao Chi if (ret < 0) 125193a76530SGrygorii Strashko return ret; 125293a76530SGrygorii Strashko 125393a76530SGrygorii Strashko cpsw_ale_del_ucast(common->ale, ndev->dev_addr, 125493a76530SGrygorii Strashko HOST_PORT_NUM, 0, 0); 125593a76530SGrygorii Strashko cpsw_ale_add_ucast(common->ale, sockaddr->sa_data, 125693a76530SGrygorii Strashko HOST_PORT_NUM, ALE_SECURE, 0); 125793a76530SGrygorii Strashko 125893a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, addr); 125993a76530SGrygorii Strashko eth_commit_mac_addr_change(ndev, sockaddr); 126093a76530SGrygorii Strashko 126193a76530SGrygorii Strashko pm_runtime_put(common->dev); 126293a76530SGrygorii Strashko 126393a76530SGrygorii Strashko return 0; 126493a76530SGrygorii Strashko } 126593a76530SGrygorii Strashko 1266b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev, 1267b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1268b1f66a5bSGrygorii Strashko { 1269b1f66a5bSGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1270b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1271b1f66a5bSGrygorii Strashko u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype; 1272b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1273b1f66a5bSGrygorii Strashko 1274b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1275b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1276b1f66a5bSGrygorii Strashko 1277b1f66a5bSGrygorii Strashko if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1278b1f66a5bSGrygorii Strashko return -EFAULT; 1279b1f66a5bSGrygorii Strashko 1280b1f66a5bSGrygorii Strashko /* TX HW timestamp */ 1281b1f66a5bSGrygorii Strashko switch (cfg.tx_type) { 1282b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_OFF: 1283b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_ON: 1284b1f66a5bSGrygorii Strashko break; 1285b1f66a5bSGrygorii Strashko default: 1286b1f66a5bSGrygorii Strashko return -ERANGE; 1287b1f66a5bSGrygorii Strashko } 1288b1f66a5bSGrygorii Strashko 1289b1f66a5bSGrygorii Strashko switch (cfg.rx_filter) { 1290b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NONE: 1291b1f66a5bSGrygorii Strashko port->rx_ts_enabled = false; 1292b1f66a5bSGrygorii Strashko break; 1293b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_ALL: 1294b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_SOME: 1295b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1296b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1297b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1298b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1299b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1300b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1301b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1302b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1303b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1304b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_EVENT: 1305b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_SYNC: 1306b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1307b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NTP_ALL: 1308b1f66a5bSGrygorii Strashko port->rx_ts_enabled = true; 1309b1f66a5bSGrygorii Strashko cfg.rx_filter = HWTSTAMP_FILTER_ALL; 1310b1f66a5bSGrygorii Strashko break; 1311b1f66a5bSGrygorii Strashko default: 1312b1f66a5bSGrygorii Strashko return -ERANGE; 1313b1f66a5bSGrygorii Strashko } 1314b1f66a5bSGrygorii Strashko 1315b1f66a5bSGrygorii Strashko port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON); 1316b1f66a5bSGrygorii Strashko 1317b1f66a5bSGrygorii Strashko /* cfg TX timestamp */ 1318b1f66a5bSGrygorii Strashko seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET << 1319b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588; 1320b1f66a5bSGrygorii Strashko 1321b1f66a5bSGrygorii Strashko ts_vlan_ltype = ETH_P_8021Q; 1322b1f66a5bSGrygorii Strashko 1323b1f66a5bSGrygorii Strashko ts_ctrl_ltype2 = ETH_P_1588 | 1324b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 | 1325b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 | 1326b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 | 1327b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 | 1328b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 | 1329b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 | 1330b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 | 1331b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO; 1332b1f66a5bSGrygorii Strashko 1333b1f66a5bSGrygorii Strashko ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS << 1334b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT; 1335b1f66a5bSGrygorii Strashko 1336b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1337b1f66a5bSGrygorii Strashko ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN | 1338b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN; 1339b1f66a5bSGrygorii Strashko 1340b1f66a5bSGrygorii Strashko writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG); 1341b1f66a5bSGrygorii Strashko writel(ts_vlan_ltype, port->port_base + 1342b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG); 1343b1f66a5bSGrygorii Strashko writel(ts_ctrl_ltype2, port->port_base + 1344b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2); 1345b1f66a5bSGrygorii Strashko writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL); 1346b1f66a5bSGrygorii Strashko 1347b1f66a5bSGrygorii Strashko /* en/dis RX timestamp */ 1348b1f66a5bSGrygorii Strashko am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled); 1349b1f66a5bSGrygorii Strashko 1350b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1351b1f66a5bSGrygorii Strashko } 1352b1f66a5bSGrygorii Strashko 1353b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev, 1354b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1355b1f66a5bSGrygorii Strashko { 1356b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1357b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1358b1f66a5bSGrygorii Strashko 1359b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1360b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1361b1f66a5bSGrygorii Strashko 1362b1f66a5bSGrygorii Strashko cfg.flags = 0; 1363b1f66a5bSGrygorii Strashko cfg.tx_type = port->tx_ts_enabled ? 1364b1f66a5bSGrygorii Strashko HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1365b1f66a5bSGrygorii Strashko cfg.rx_filter = port->rx_ts_enabled ? 1366b1f66a5bSGrygorii Strashko HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 1367b1f66a5bSGrygorii Strashko 1368b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1369b1f66a5bSGrygorii Strashko } 1370b1f66a5bSGrygorii Strashko 137193a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev, 137293a76530SGrygorii Strashko struct ifreq *req, int cmd) 137393a76530SGrygorii Strashko { 137493a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 137593a76530SGrygorii Strashko 137693a76530SGrygorii Strashko if (!netif_running(ndev)) 137793a76530SGrygorii Strashko return -EINVAL; 137893a76530SGrygorii Strashko 1379b1f66a5bSGrygorii Strashko switch (cmd) { 1380b1f66a5bSGrygorii Strashko case SIOCSHWTSTAMP: 1381b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_set(ndev, req); 1382b1f66a5bSGrygorii Strashko case SIOCGHWTSTAMP: 1383b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_get(ndev, req); 1384b1f66a5bSGrygorii Strashko } 1385b1f66a5bSGrygorii Strashko 1386e8609e69SSiddharth Vadapalli return phylink_mii_ioctl(port->slave.phylink, req, cmd); 138793a76530SGrygorii Strashko } 138893a76530SGrygorii Strashko 138993a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev, 139093a76530SGrygorii Strashko struct rtnl_link_stats64 *stats) 139193a76530SGrygorii Strashko { 139293a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev); 139393a76530SGrygorii Strashko unsigned int start; 139493a76530SGrygorii Strashko int cpu; 139593a76530SGrygorii Strashko 139693a76530SGrygorii Strashko for_each_possible_cpu(cpu) { 139793a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *cpu_stats; 139893a76530SGrygorii Strashko u64 rx_packets; 139993a76530SGrygorii Strashko u64 rx_bytes; 140093a76530SGrygorii Strashko u64 tx_packets; 140193a76530SGrygorii Strashko u64 tx_bytes; 140293a76530SGrygorii Strashko 140393a76530SGrygorii Strashko cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu); 140493a76530SGrygorii Strashko do { 1405068c38adSThomas Gleixner start = u64_stats_fetch_begin(&cpu_stats->syncp); 140693a76530SGrygorii Strashko rx_packets = cpu_stats->rx_packets; 140793a76530SGrygorii Strashko rx_bytes = cpu_stats->rx_bytes; 140893a76530SGrygorii Strashko tx_packets = cpu_stats->tx_packets; 140993a76530SGrygorii Strashko tx_bytes = cpu_stats->tx_bytes; 1410068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&cpu_stats->syncp, start)); 141193a76530SGrygorii Strashko 141293a76530SGrygorii Strashko stats->rx_packets += rx_packets; 141393a76530SGrygorii Strashko stats->rx_bytes += rx_bytes; 141493a76530SGrygorii Strashko stats->tx_packets += tx_packets; 141593a76530SGrygorii Strashko stats->tx_bytes += tx_bytes; 141693a76530SGrygorii Strashko } 141793a76530SGrygorii Strashko 141893a76530SGrygorii Strashko stats->rx_errors = dev->stats.rx_errors; 141993a76530SGrygorii Strashko stats->rx_dropped = dev->stats.rx_dropped; 142093a76530SGrygorii Strashko stats->tx_dropped = dev->stats.tx_dropped; 142193a76530SGrygorii Strashko } 142293a76530SGrygorii Strashko 142384b4aa49SGrygorii Strashko static const struct net_device_ops am65_cpsw_nuss_netdev_ops = { 142493a76530SGrygorii Strashko .ndo_open = am65_cpsw_nuss_ndo_slave_open, 142593a76530SGrygorii Strashko .ndo_stop = am65_cpsw_nuss_ndo_slave_stop, 142693a76530SGrygorii Strashko .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit, 142793a76530SGrygorii Strashko .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode, 142893a76530SGrygorii Strashko .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats, 142993a76530SGrygorii Strashko .ndo_validate_addr = eth_validate_addr, 143093a76530SGrygorii Strashko .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address, 143193a76530SGrygorii Strashko .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout, 143293a76530SGrygorii Strashko .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid, 143393a76530SGrygorii Strashko .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid, 1434a7605370SArnd Bergmann .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl, 14358127224cSIvan Khoronzhuk .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc, 14365c8560c4SGrygorii Strashko .ndo_set_tx_maxrate = am65_cpsw_qos_ndo_tx_p0_set_maxrate, 143793a76530SGrygorii Strashko }; 143893a76530SGrygorii Strashko 1439dab2b265SSiddharth Vadapalli static void am65_cpsw_disable_phy(struct phy *phy) 1440dab2b265SSiddharth Vadapalli { 1441dab2b265SSiddharth Vadapalli phy_power_off(phy); 1442dab2b265SSiddharth Vadapalli phy_exit(phy); 1443dab2b265SSiddharth Vadapalli } 1444dab2b265SSiddharth Vadapalli 1445dab2b265SSiddharth Vadapalli static int am65_cpsw_enable_phy(struct phy *phy) 1446dab2b265SSiddharth Vadapalli { 1447dab2b265SSiddharth Vadapalli int ret; 1448dab2b265SSiddharth Vadapalli 1449dab2b265SSiddharth Vadapalli ret = phy_init(phy); 1450dab2b265SSiddharth Vadapalli if (ret < 0) 1451dab2b265SSiddharth Vadapalli return ret; 1452dab2b265SSiddharth Vadapalli 1453dab2b265SSiddharth Vadapalli ret = phy_power_on(phy); 1454dab2b265SSiddharth Vadapalli if (ret < 0) { 1455dab2b265SSiddharth Vadapalli phy_exit(phy); 1456dab2b265SSiddharth Vadapalli return ret; 1457dab2b265SSiddharth Vadapalli } 1458dab2b265SSiddharth Vadapalli 1459dab2b265SSiddharth Vadapalli return 0; 1460dab2b265SSiddharth Vadapalli } 1461dab2b265SSiddharth Vadapalli 1462dab2b265SSiddharth Vadapalli static void am65_cpsw_disable_serdes_phy(struct am65_cpsw_common *common) 1463dab2b265SSiddharth Vadapalli { 1464dab2b265SSiddharth Vadapalli struct am65_cpsw_port *port; 1465dab2b265SSiddharth Vadapalli struct phy *phy; 1466dab2b265SSiddharth Vadapalli int i; 1467dab2b265SSiddharth Vadapalli 1468dab2b265SSiddharth Vadapalli for (i = 0; i < common->port_num; i++) { 1469dab2b265SSiddharth Vadapalli port = &common->ports[i]; 1470dab2b265SSiddharth Vadapalli phy = port->slave.serdes_phy; 1471dab2b265SSiddharth Vadapalli if (phy) 1472dab2b265SSiddharth Vadapalli am65_cpsw_disable_phy(phy); 1473dab2b265SSiddharth Vadapalli } 1474dab2b265SSiddharth Vadapalli } 1475dab2b265SSiddharth Vadapalli 1476dab2b265SSiddharth Vadapalli static int am65_cpsw_init_serdes_phy(struct device *dev, struct device_node *port_np, 1477dab2b265SSiddharth Vadapalli struct am65_cpsw_port *port) 1478dab2b265SSiddharth Vadapalli { 1479bca93b20SSiddharth Vadapalli const char *name = "serdes"; 1480dab2b265SSiddharth Vadapalli struct phy *phy; 1481dab2b265SSiddharth Vadapalli int ret; 1482dab2b265SSiddharth Vadapalli 1483b3a8df9fSGeert Uytterhoeven phy = devm_of_phy_optional_get(dev, port_np, name); 1484b3a8df9fSGeert Uytterhoeven if (IS_ERR_OR_NULL(phy)) 1485b3a8df9fSGeert Uytterhoeven return PTR_ERR_OR_ZERO(phy); 1486dab2b265SSiddharth Vadapalli 1487dab2b265SSiddharth Vadapalli /* Serdes PHY exists. Store it. */ 1488dab2b265SSiddharth Vadapalli port->slave.serdes_phy = phy; 1489dab2b265SSiddharth Vadapalli 1490dab2b265SSiddharth Vadapalli ret = am65_cpsw_enable_phy(phy); 1491dab2b265SSiddharth Vadapalli if (ret < 0) 1492dab2b265SSiddharth Vadapalli goto err_phy; 1493dab2b265SSiddharth Vadapalli 1494dab2b265SSiddharth Vadapalli return 0; 1495dab2b265SSiddharth Vadapalli 1496dab2b265SSiddharth Vadapalli err_phy: 1497dab2b265SSiddharth Vadapalli devm_phy_put(dev, phy); 1498dab2b265SSiddharth Vadapalli return ret; 1499dab2b265SSiddharth Vadapalli } 1500dab2b265SSiddharth Vadapalli 1501e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode, 1502e8609e69SSiddharth Vadapalli const struct phylink_link_state *state) 1503e8609e69SSiddharth Vadapalli { 150437184fc1SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 150537184fc1SSiddharth Vadapalli phylink_config); 150637184fc1SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 150737184fc1SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 150837184fc1SSiddharth Vadapalli 1509e0f72db3SSiddharth Vadapalli if (common->pdata.extra_modes & BIT(state->interface)) { 1510ce639b76SSiddharth Vadapalli if (state->interface == PHY_INTERFACE_MODE_SGMII) { 1511e0f72db3SSiddharth Vadapalli writel(ADVERTISE_SGMII, 1512e0f72db3SSiddharth Vadapalli port->sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG); 1513ce639b76SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN); 1514ce639b76SSiddharth Vadapalli } else { 1515ce639b76SSiddharth Vadapalli cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN); 1516ce639b76SSiddharth Vadapalli } 1517e0f72db3SSiddharth Vadapalli 15188e672b56SSiddharth Vadapalli if (state->interface == PHY_INTERFACE_MODE_USXGMII) { 15198e672b56SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, 15208e672b56SSiddharth Vadapalli CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN); 15218e672b56SSiddharth Vadapalli } else { 15228e672b56SSiddharth Vadapalli cpsw_sl_ctl_clr(port->slave.mac_sl, 15238e672b56SSiddharth Vadapalli CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN); 15248e672b56SSiddharth Vadapalli } 15258e672b56SSiddharth Vadapalli 152637184fc1SSiddharth Vadapalli writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE, 152737184fc1SSiddharth Vadapalli port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG); 1528e8609e69SSiddharth Vadapalli } 1529e0f72db3SSiddharth Vadapalli } 1530e8609e69SSiddharth Vadapalli 1531e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode, 1532e8609e69SSiddharth Vadapalli phy_interface_t interface) 1533e8609e69SSiddharth Vadapalli { 1534e8609e69SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1535e8609e69SSiddharth Vadapalli phylink_config); 1536e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1537e8609e69SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 1538e8609e69SSiddharth Vadapalli struct net_device *ndev = port->ndev; 1539ce639b76SSiddharth Vadapalli u32 mac_control; 1540e8609e69SSiddharth Vadapalli int tmo; 1541e8609e69SSiddharth Vadapalli 1542e8609e69SSiddharth Vadapalli /* disable forwarding */ 1543e8609e69SSiddharth Vadapalli cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1544e8609e69SSiddharth Vadapalli 1545e8609e69SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 1546e8609e69SSiddharth Vadapalli 1547e8609e69SSiddharth Vadapalli tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 1548e8609e69SSiddharth Vadapalli dev_dbg(common->dev, "down msc_sl %08x tmo %d\n", 1549e8609e69SSiddharth Vadapalli cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo); 1550e8609e69SSiddharth Vadapalli 1551ce639b76SSiddharth Vadapalli /* All the bits that am65_cpsw_nuss_mac_link_up() can possibly set */ 1552ce639b76SSiddharth Vadapalli mac_control = CPSW_SL_CTL_GMII_EN | CPSW_SL_CTL_GIG | CPSW_SL_CTL_IFCTL_A | 1553ce639b76SSiddharth Vadapalli CPSW_SL_CTL_FULLDUPLEX | CPSW_SL_CTL_RX_FLOW_EN | CPSW_SL_CTL_TX_FLOW_EN; 1554ce639b76SSiddharth Vadapalli /* If interface mode is RGMII, CPSW_SL_CTL_EXT_EN might have been set for 10 Mbps */ 1555ce639b76SSiddharth Vadapalli if (phy_interface_mode_is_rgmii(interface)) 1556ce639b76SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_EXT_EN; 1557ce639b76SSiddharth Vadapalli /* Only clear those bits that can be set by am65_cpsw_nuss_mac_link_up() */ 1558ce639b76SSiddharth Vadapalli cpsw_sl_ctl_clr(port->slave.mac_sl, mac_control); 1559e8609e69SSiddharth Vadapalli 1560e8609e69SSiddharth Vadapalli am65_cpsw_qos_link_down(ndev); 1561e8609e69SSiddharth Vadapalli netif_tx_stop_all_queues(ndev); 1562e8609e69SSiddharth Vadapalli } 1563e8609e69SSiddharth Vadapalli 1564e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy, 1565e8609e69SSiddharth Vadapalli unsigned int mode, phy_interface_t interface, int speed, 1566e8609e69SSiddharth Vadapalli int duplex, bool tx_pause, bool rx_pause) 1567e8609e69SSiddharth Vadapalli { 1568e8609e69SSiddharth Vadapalli struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1569e8609e69SSiddharth Vadapalli phylink_config); 1570e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1571e8609e69SSiddharth Vadapalli struct am65_cpsw_common *common = port->common; 1572e8609e69SSiddharth Vadapalli u32 mac_control = CPSW_SL_CTL_GMII_EN; 1573e8609e69SSiddharth Vadapalli struct net_device *ndev = port->ndev; 1574e8609e69SSiddharth Vadapalli 1575ce639b76SSiddharth Vadapalli /* Bring the port out of idle state */ 1576ce639b76SSiddharth Vadapalli cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 1577ce639b76SSiddharth Vadapalli 1578e8609e69SSiddharth Vadapalli if (speed == SPEED_1000) 1579e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_GIG; 1580ce639b76SSiddharth Vadapalli /* TODO: Verify whether in-band is necessary for 10 Mbps RGMII */ 15816c681f89SSiddharth Vadapalli if (speed == SPEED_10 && phy_interface_mode_is_rgmii(interface)) 1582e8609e69SSiddharth Vadapalli /* Can be used with in band mode only */ 1583e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_EXT_EN; 1584e8609e69SSiddharth Vadapalli if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII) 1585e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_IFCTL_A; 1586e8609e69SSiddharth Vadapalli if (duplex) 1587e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_FULLDUPLEX; 1588e8609e69SSiddharth Vadapalli 1589e8609e69SSiddharth Vadapalli /* rx_pause/tx_pause */ 1590e8609e69SSiddharth Vadapalli if (rx_pause) 1591e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_RX_FLOW_EN; 1592e8609e69SSiddharth Vadapalli 1593e8609e69SSiddharth Vadapalli if (tx_pause) 1594e8609e69SSiddharth Vadapalli mac_control |= CPSW_SL_CTL_TX_FLOW_EN; 1595e8609e69SSiddharth Vadapalli 1596e8609e69SSiddharth Vadapalli cpsw_sl_ctl_set(port->slave.mac_sl, mac_control); 1597e8609e69SSiddharth Vadapalli 1598e8609e69SSiddharth Vadapalli /* enable forwarding */ 1599e8609e69SSiddharth Vadapalli cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1600e8609e69SSiddharth Vadapalli 1601e8609e69SSiddharth Vadapalli am65_cpsw_qos_link_up(ndev, speed); 1602e8609e69SSiddharth Vadapalli netif_tx_wake_all_queues(ndev); 1603e8609e69SSiddharth Vadapalli } 1604e8609e69SSiddharth Vadapalli 1605e8609e69SSiddharth Vadapalli static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = { 1606e8609e69SSiddharth Vadapalli .mac_config = am65_cpsw_nuss_mac_config, 1607e8609e69SSiddharth Vadapalli .mac_link_down = am65_cpsw_nuss_mac_link_down, 1608e8609e69SSiddharth Vadapalli .mac_link_up = am65_cpsw_nuss_mac_link_up, 1609e8609e69SSiddharth Vadapalli }; 1610e8609e69SSiddharth Vadapalli 161193a76530SGrygorii Strashko static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port) 161293a76530SGrygorii Strashko { 161393a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 161493a76530SGrygorii Strashko 161593a76530SGrygorii Strashko if (!port->disabled) 161693a76530SGrygorii Strashko return; 161793a76530SGrygorii Strashko 161893a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 161993a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 162093a76530SGrygorii Strashko 162193a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 162293a76530SGrygorii Strashko cpsw_sl_ctl_reset(port->slave.mac_sl); 162393a76530SGrygorii Strashko } 162493a76530SGrygorii Strashko 16251a014663SRoger Quadros static void am65_cpsw_nuss_free_tx_chns(void *data) 162693a76530SGrygorii Strashko { 16271a014663SRoger Quadros struct am65_cpsw_common *common = data; 162893a76530SGrygorii Strashko int i; 162993a76530SGrygorii Strashko 163093a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 163193a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 163293a76530SGrygorii Strashko 163393a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 163493a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 163593a76530SGrygorii Strashko 1636ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1637ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1638ce8eb4c7SVignesh Raghavendra 16391a014663SRoger Quadros memset(tx_chn, 0, sizeof(*tx_chn)); 164093a76530SGrygorii Strashko } 164193a76530SGrygorii Strashko } 164293a76530SGrygorii Strashko 164393a76530SGrygorii Strashko void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common) 164493a76530SGrygorii Strashko { 164593a76530SGrygorii Strashko struct device *dev = common->dev; 164693a76530SGrygorii Strashko int i; 164793a76530SGrygorii Strashko 16481a014663SRoger Quadros devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common); 16491a014663SRoger Quadros 16505c8560c4SGrygorii Strashko common->tx_ch_rate_msk = 0; 165193a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 165293a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 165393a76530SGrygorii Strashko 16541a014663SRoger Quadros if (tx_chn->irq) 165593a76530SGrygorii Strashko devm_free_irq(dev, tx_chn->irq, tx_chn); 165693a76530SGrygorii Strashko 165793a76530SGrygorii Strashko netif_napi_del(&tx_chn->napi_tx); 165893a76530SGrygorii Strashko 165993a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 166093a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 166193a76530SGrygorii Strashko 1662ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1663ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1664ce8eb4c7SVignesh Raghavendra 166593a76530SGrygorii Strashko memset(tx_chn, 0, sizeof(*tx_chn)); 166693a76530SGrygorii Strashko } 166793a76530SGrygorii Strashko } 166893a76530SGrygorii Strashko 166924bc19b0SRoger Quadros static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common) 167024bc19b0SRoger Quadros { 167124bc19b0SRoger Quadros struct device *dev = common->dev; 167224bc19b0SRoger Quadros int i, ret = 0; 167324bc19b0SRoger Quadros 167424bc19b0SRoger Quadros for (i = 0; i < common->tx_ch_num; i++) { 167524bc19b0SRoger Quadros struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 167624bc19b0SRoger Quadros 167724bc19b0SRoger Quadros netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx, 167824bc19b0SRoger Quadros am65_cpsw_nuss_tx_poll); 167924bc19b0SRoger Quadros 168024bc19b0SRoger Quadros ret = devm_request_irq(dev, tx_chn->irq, 168124bc19b0SRoger Quadros am65_cpsw_nuss_tx_irq, 168224bc19b0SRoger Quadros IRQF_TRIGGER_HIGH, 168324bc19b0SRoger Quadros tx_chn->tx_chn_name, tx_chn); 168424bc19b0SRoger Quadros if (ret) { 168524bc19b0SRoger Quadros dev_err(dev, "failure requesting tx%u irq %u, %d\n", 168624bc19b0SRoger Quadros tx_chn->id, tx_chn->irq, ret); 168724bc19b0SRoger Quadros goto err; 168824bc19b0SRoger Quadros } 168924bc19b0SRoger Quadros } 169024bc19b0SRoger Quadros 169124bc19b0SRoger Quadros err: 169224bc19b0SRoger Quadros return ret; 169324bc19b0SRoger Quadros } 169424bc19b0SRoger Quadros 169593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common) 169693a76530SGrygorii Strashko { 169793a76530SGrygorii Strashko u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS); 169893a76530SGrygorii Strashko struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 }; 169993a76530SGrygorii Strashko struct device *dev = common->dev; 170093a76530SGrygorii Strashko struct k3_ring_cfg ring_cfg = { 170193a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 170293a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 170393a76530SGrygorii Strashko .flags = 0 170493a76530SGrygorii Strashko }; 170593a76530SGrygorii Strashko u32 hdesc_size; 170693a76530SGrygorii Strashko int i, ret = 0; 170793a76530SGrygorii Strashko 170893a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 170993a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 171093a76530SGrygorii Strashko 171193a76530SGrygorii Strashko tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 171293a76530SGrygorii Strashko tx_cfg.tx_cfg = ring_cfg; 171393a76530SGrygorii Strashko tx_cfg.txcq_cfg = ring_cfg; 171493a76530SGrygorii Strashko tx_cfg.tx_cfg.size = max_desc_num; 171593a76530SGrygorii Strashko tx_cfg.txcq_cfg.size = max_desc_num; 171693a76530SGrygorii Strashko 171793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 171893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 171993a76530SGrygorii Strashko 172093a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 172193a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "tx%d", i); 172293a76530SGrygorii Strashko 1723a9e60cf0SGrygorii Strashko spin_lock_init(&tx_chn->lock); 172493a76530SGrygorii Strashko tx_chn->common = common; 172593a76530SGrygorii Strashko tx_chn->id = i; 172693a76530SGrygorii Strashko tx_chn->descs_num = max_desc_num; 172793a76530SGrygorii Strashko 172893a76530SGrygorii Strashko tx_chn->tx_chn = 172993a76530SGrygorii Strashko k3_udma_glue_request_tx_chn(dev, 173093a76530SGrygorii Strashko tx_chn->tx_chn_name, 173193a76530SGrygorii Strashko &tx_cfg); 173293a76530SGrygorii Strashko if (IS_ERR(tx_chn->tx_chn)) { 17338fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn), 17348fbc2f9eSGrygorii Strashko "Failed to request tx dma channel\n"); 173593a76530SGrygorii Strashko goto err; 173693a76530SGrygorii Strashko } 1737ed569ed9SPeter Ujfalusi tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn); 1738ed569ed9SPeter Ujfalusi 1739ed569ed9SPeter Ujfalusi tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev, 1740ed569ed9SPeter Ujfalusi tx_chn->descs_num, 1741ed569ed9SPeter Ujfalusi hdesc_size, 1742ed569ed9SPeter Ujfalusi tx_chn->tx_chn_name); 1743ed569ed9SPeter Ujfalusi if (IS_ERR(tx_chn->desc_pool)) { 1744ed569ed9SPeter Ujfalusi ret = PTR_ERR(tx_chn->desc_pool); 1745ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create poll %d\n", ret); 1746ed569ed9SPeter Ujfalusi goto err; 1747ed569ed9SPeter Ujfalusi } 174893a76530SGrygorii Strashko 174993a76530SGrygorii Strashko tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn); 1750*f9a1d321SDan Carpenter if (tx_chn->irq < 0) { 175193a76530SGrygorii Strashko dev_err(dev, "Failed to get tx dma irq %d\n", 175293a76530SGrygorii Strashko tx_chn->irq); 1753*f9a1d321SDan Carpenter ret = tx_chn->irq; 175493a76530SGrygorii Strashko goto err; 175593a76530SGrygorii Strashko } 175693a76530SGrygorii Strashko 175793a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 175893a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "%s-tx%d", 175993a76530SGrygorii Strashko dev_name(dev), tx_chn->id); 1760fd23df72SRoger Quadros } 1761fd23df72SRoger Quadros 176224bc19b0SRoger Quadros ret = am65_cpsw_nuss_ndev_add_tx_napi(common); 176324bc19b0SRoger Quadros if (ret) { 176424bc19b0SRoger Quadros dev_err(dev, "Failed to add tx NAPI %d\n", ret); 176524bc19b0SRoger Quadros goto err; 176624bc19b0SRoger Quadros } 176724bc19b0SRoger Quadros 176893a76530SGrygorii Strashko err: 17691a014663SRoger Quadros i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common); 17701a014663SRoger Quadros if (i) { 17711a014663SRoger Quadros dev_err(dev, "Failed to add free_tx_chns action %d\n", i); 17721a014663SRoger Quadros return i; 17731a014663SRoger Quadros } 177493a76530SGrygorii Strashko 177593a76530SGrygorii Strashko return ret; 177693a76530SGrygorii Strashko } 177793a76530SGrygorii Strashko 17781a014663SRoger Quadros static void am65_cpsw_nuss_free_rx_chns(void *data) 177993a76530SGrygorii Strashko { 17801a014663SRoger Quadros struct am65_cpsw_common *common = data; 178193a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn; 178293a76530SGrygorii Strashko 178393a76530SGrygorii Strashko rx_chn = &common->rx_chns; 178493a76530SGrygorii Strashko 178593a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 178693a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 1787ce8eb4c7SVignesh Raghavendra 1788ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 1789ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 179093a76530SGrygorii Strashko } 179193a76530SGrygorii Strashko 179224bc19b0SRoger Quadros static void am65_cpsw_nuss_remove_rx_chns(void *data) 179324bc19b0SRoger Quadros { 179424bc19b0SRoger Quadros struct am65_cpsw_common *common = data; 179524bc19b0SRoger Quadros struct am65_cpsw_rx_chn *rx_chn; 179624bc19b0SRoger Quadros struct device *dev = common->dev; 179724bc19b0SRoger Quadros 179824bc19b0SRoger Quadros rx_chn = &common->rx_chns; 179924bc19b0SRoger Quadros devm_remove_action(dev, am65_cpsw_nuss_free_rx_chns, common); 180024bc19b0SRoger Quadros 180124bc19b0SRoger Quadros if (!(rx_chn->irq < 0)) 180224bc19b0SRoger Quadros devm_free_irq(dev, rx_chn->irq, common); 180324bc19b0SRoger Quadros 180424bc19b0SRoger Quadros netif_napi_del(&common->napi_rx); 180524bc19b0SRoger Quadros 180624bc19b0SRoger Quadros if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 180724bc19b0SRoger Quadros k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 180824bc19b0SRoger Quadros 180924bc19b0SRoger Quadros if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 181024bc19b0SRoger Quadros k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 181124bc19b0SRoger Quadros 181224bc19b0SRoger Quadros common->rx_flow_id_base = -1; 181324bc19b0SRoger Quadros } 181424bc19b0SRoger Quadros 181593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common) 181693a76530SGrygorii Strashko { 181793a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 181893a76530SGrygorii Strashko struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 }; 181993a76530SGrygorii Strashko u32 max_desc_num = AM65_CPSW_MAX_RX_DESC; 182093a76530SGrygorii Strashko struct device *dev = common->dev; 182193a76530SGrygorii Strashko u32 hdesc_size; 182293a76530SGrygorii Strashko u32 fdqring_id; 182393a76530SGrygorii Strashko int i, ret = 0; 182493a76530SGrygorii Strashko 182593a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 182693a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 182793a76530SGrygorii Strashko 182893a76530SGrygorii Strashko rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 182993a76530SGrygorii Strashko rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS; 18301a014663SRoger Quadros rx_cfg.flow_id_base = common->rx_flow_id_base; 183193a76530SGrygorii Strashko 183293a76530SGrygorii Strashko /* init all flows */ 183393a76530SGrygorii Strashko rx_chn->dev = dev; 183493a76530SGrygorii Strashko rx_chn->descs_num = max_desc_num; 183593a76530SGrygorii Strashko 183693a76530SGrygorii Strashko rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg); 183793a76530SGrygorii Strashko if (IS_ERR(rx_chn->rx_chn)) { 18388fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn), 18398fbc2f9eSGrygorii Strashko "Failed to request rx dma channel\n"); 184093a76530SGrygorii Strashko goto err; 184193a76530SGrygorii Strashko } 1842ed569ed9SPeter Ujfalusi rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn); 1843ed569ed9SPeter Ujfalusi 1844ed569ed9SPeter Ujfalusi rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev, 1845ed569ed9SPeter Ujfalusi rx_chn->descs_num, 1846ed569ed9SPeter Ujfalusi hdesc_size, "rx"); 1847ed569ed9SPeter Ujfalusi if (IS_ERR(rx_chn->desc_pool)) { 1848ed569ed9SPeter Ujfalusi ret = PTR_ERR(rx_chn->desc_pool); 1849ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create rx poll %d\n", ret); 1850ed569ed9SPeter Ujfalusi goto err; 1851ed569ed9SPeter Ujfalusi } 185293a76530SGrygorii Strashko 185393a76530SGrygorii Strashko common->rx_flow_id_base = 185493a76530SGrygorii Strashko k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn); 185593a76530SGrygorii Strashko dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base); 185693a76530SGrygorii Strashko 185793a76530SGrygorii Strashko fdqring_id = K3_RINGACC_RING_ID_ANY; 185893a76530SGrygorii Strashko for (i = 0; i < rx_cfg.flow_id_num; i++) { 185993a76530SGrygorii Strashko struct k3_ring_cfg rxring_cfg = { 186093a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 186193a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 186293a76530SGrygorii Strashko .flags = 0, 186393a76530SGrygorii Strashko }; 186493a76530SGrygorii Strashko struct k3_ring_cfg fdqring_cfg = { 186593a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 186693a76530SGrygorii Strashko .flags = K3_RINGACC_RING_SHARED, 186793a76530SGrygorii Strashko }; 186893a76530SGrygorii Strashko struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = { 186993a76530SGrygorii Strashko .rx_cfg = rxring_cfg, 187093a76530SGrygorii Strashko .rxfdq_cfg = fdqring_cfg, 187193a76530SGrygorii Strashko .ring_rxq_id = K3_RINGACC_RING_ID_ANY, 187293a76530SGrygorii Strashko .src_tag_lo_sel = 187393a76530SGrygorii Strashko K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG, 187493a76530SGrygorii Strashko }; 187593a76530SGrygorii Strashko 187693a76530SGrygorii Strashko rx_flow_cfg.ring_rxfdq0_id = fdqring_id; 187793a76530SGrygorii Strashko rx_flow_cfg.rx_cfg.size = max_desc_num; 187893a76530SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.size = max_desc_num; 1879c6275c02SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode; 188093a76530SGrygorii Strashko 188193a76530SGrygorii Strashko ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn, 188293a76530SGrygorii Strashko i, &rx_flow_cfg); 188393a76530SGrygorii Strashko if (ret) { 188493a76530SGrygorii Strashko dev_err(dev, "Failed to init rx flow%d %d\n", i, ret); 188593a76530SGrygorii Strashko goto err; 188693a76530SGrygorii Strashko } 188793a76530SGrygorii Strashko if (!i) 188893a76530SGrygorii Strashko fdqring_id = 188993a76530SGrygorii Strashko k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn, 189093a76530SGrygorii Strashko i); 189193a76530SGrygorii Strashko 189293a76530SGrygorii Strashko rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i); 189393a76530SGrygorii Strashko 189493a76530SGrygorii Strashko if (rx_chn->irq <= 0) { 189593a76530SGrygorii Strashko dev_err(dev, "Failed to get rx dma irq %d\n", 189693a76530SGrygorii Strashko rx_chn->irq); 189793a76530SGrygorii Strashko ret = -ENXIO; 189893a76530SGrygorii Strashko goto err; 189993a76530SGrygorii Strashko } 190093a76530SGrygorii Strashko } 190193a76530SGrygorii Strashko 190224bc19b0SRoger Quadros netif_napi_add(common->dma_ndev, &common->napi_rx, 190324bc19b0SRoger Quadros am65_cpsw_nuss_rx_poll); 190424bc19b0SRoger Quadros 190524bc19b0SRoger Quadros ret = devm_request_irq(dev, rx_chn->irq, 190624bc19b0SRoger Quadros am65_cpsw_nuss_rx_irq, 190724bc19b0SRoger Quadros IRQF_TRIGGER_HIGH, dev_name(dev), common); 190824bc19b0SRoger Quadros if (ret) { 190924bc19b0SRoger Quadros dev_err(dev, "failure requesting rx irq %u, %d\n", 191024bc19b0SRoger Quadros rx_chn->irq, ret); 191124bc19b0SRoger Quadros goto err; 191224bc19b0SRoger Quadros } 191324bc19b0SRoger Quadros 1914fd23df72SRoger Quadros err: 19151a014663SRoger Quadros i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common); 19161a014663SRoger Quadros if (i) { 19171a014663SRoger Quadros dev_err(dev, "Failed to add free_rx_chns action %d\n", i); 19181a014663SRoger Quadros return i; 19191a014663SRoger Quadros } 1920fd23df72SRoger Quadros 192193a76530SGrygorii Strashko return ret; 192293a76530SGrygorii Strashko } 192393a76530SGrygorii Strashko 192493a76530SGrygorii Strashko static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common) 192593a76530SGrygorii Strashko { 192693a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 192793a76530SGrygorii Strashko 192893a76530SGrygorii Strashko host_p->common = common; 192993a76530SGrygorii Strashko host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE; 193093a76530SGrygorii Strashko host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE; 193193a76530SGrygorii Strashko 193293a76530SGrygorii Strashko return 0; 193393a76530SGrygorii Strashko } 193493a76530SGrygorii Strashko 193593a76530SGrygorii Strashko static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node, 193693a76530SGrygorii Strashko int slave, u8 *mac_addr) 193793a76530SGrygorii Strashko { 193893a76530SGrygorii Strashko u32 mac_lo, mac_hi, offset; 193993a76530SGrygorii Strashko struct regmap *syscon; 194093a76530SGrygorii Strashko int ret; 194193a76530SGrygorii Strashko 194293a76530SGrygorii Strashko syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse"); 194393a76530SGrygorii Strashko if (IS_ERR(syscon)) { 194493a76530SGrygorii Strashko if (PTR_ERR(syscon) == -ENODEV) 194593a76530SGrygorii Strashko return 0; 194693a76530SGrygorii Strashko return PTR_ERR(syscon); 194793a76530SGrygorii Strashko } 194893a76530SGrygorii Strashko 194993a76530SGrygorii Strashko ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1, 195093a76530SGrygorii Strashko &offset); 195193a76530SGrygorii Strashko if (ret) 195293a76530SGrygorii Strashko return ret; 195393a76530SGrygorii Strashko 195493a76530SGrygorii Strashko regmap_read(syscon, offset, &mac_lo); 195593a76530SGrygorii Strashko regmap_read(syscon, offset + 4, &mac_hi); 195693a76530SGrygorii Strashko 195793a76530SGrygorii Strashko mac_addr[0] = (mac_hi >> 8) & 0xff; 195893a76530SGrygorii Strashko mac_addr[1] = mac_hi & 0xff; 195993a76530SGrygorii Strashko mac_addr[2] = (mac_lo >> 24) & 0xff; 196093a76530SGrygorii Strashko mac_addr[3] = (mac_lo >> 16) & 0xff; 196193a76530SGrygorii Strashko mac_addr[4] = (mac_lo >> 8) & 0xff; 196293a76530SGrygorii Strashko mac_addr[5] = mac_lo & 0xff; 196393a76530SGrygorii Strashko 196493a76530SGrygorii Strashko return 0; 196593a76530SGrygorii Strashko } 196693a76530SGrygorii Strashko 1967b1f66a5bSGrygorii Strashko static int am65_cpsw_init_cpts(struct am65_cpsw_common *common) 1968b1f66a5bSGrygorii Strashko { 1969b1f66a5bSGrygorii Strashko struct device *dev = common->dev; 1970b1f66a5bSGrygorii Strashko struct device_node *node; 1971b1f66a5bSGrygorii Strashko struct am65_cpts *cpts; 1972b1f66a5bSGrygorii Strashko void __iomem *reg_base; 1973b1f66a5bSGrygorii Strashko 1974b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1975b1f66a5bSGrygorii Strashko return 0; 1976b1f66a5bSGrygorii Strashko 1977b1f66a5bSGrygorii Strashko node = of_get_child_by_name(dev->of_node, "cpts"); 1978b1f66a5bSGrygorii Strashko if (!node) { 1979b1f66a5bSGrygorii Strashko dev_err(dev, "%s cpts not found\n", __func__); 1980b1f66a5bSGrygorii Strashko return -ENOENT; 1981b1f66a5bSGrygorii Strashko } 1982b1f66a5bSGrygorii Strashko 1983b1f66a5bSGrygorii Strashko reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE; 1984b1f66a5bSGrygorii Strashko cpts = am65_cpts_create(dev, reg_base, node); 1985b1f66a5bSGrygorii Strashko if (IS_ERR(cpts)) { 1986b1f66a5bSGrygorii Strashko int ret = PTR_ERR(cpts); 1987b1f66a5bSGrygorii Strashko 19885dd89d2fSMiaoqian Lin of_node_put(node); 1989b1f66a5bSGrygorii Strashko dev_err(dev, "cpts create err %d\n", ret); 1990b1f66a5bSGrygorii Strashko return ret; 1991b1f66a5bSGrygorii Strashko } 1992b1f66a5bSGrygorii Strashko common->cpts = cpts; 1993a9c74700SGrygorii Strashko /* Forbid PM runtime if CPTS is running. 1994a9c74700SGrygorii Strashko * K3 CPSWxG modules may completely lose context during ON->OFF 1995a9c74700SGrygorii Strashko * transitions depending on integration. 1996a9c74700SGrygorii Strashko * AM65x/J721E MCU CPSW2G: false 1997a9c74700SGrygorii Strashko * J721E MAIN_CPSW9G: true 1998a9c74700SGrygorii Strashko */ 1999a9c74700SGrygorii Strashko pm_runtime_forbid(dev); 2000b1f66a5bSGrygorii Strashko 2001b1f66a5bSGrygorii Strashko return 0; 2002b1f66a5bSGrygorii Strashko } 2003b1f66a5bSGrygorii Strashko 200493a76530SGrygorii Strashko static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common) 200593a76530SGrygorii Strashko { 200693a76530SGrygorii Strashko struct device_node *node, *port_np; 200793a76530SGrygorii Strashko struct device *dev = common->dev; 200893a76530SGrygorii Strashko int ret; 200993a76530SGrygorii Strashko 201093a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 201193a76530SGrygorii Strashko if (!node) 201293a76530SGrygorii Strashko return -ENOENT; 201393a76530SGrygorii Strashko 201493a76530SGrygorii Strashko for_each_child_of_node(node, port_np) { 201593a76530SGrygorii Strashko struct am65_cpsw_port *port; 201693a76530SGrygorii Strashko u32 port_id; 201793a76530SGrygorii Strashko 201893a76530SGrygorii Strashko /* it is not a slave port node, continue */ 201993a76530SGrygorii Strashko if (strcmp(port_np->name, "port")) 202093a76530SGrygorii Strashko continue; 202193a76530SGrygorii Strashko 202293a76530SGrygorii Strashko ret = of_property_read_u32(port_np, "reg", &port_id); 202393a76530SGrygorii Strashko if (ret < 0) { 202493a76530SGrygorii Strashko dev_err(dev, "%pOF error reading port_id %d\n", 202593a76530SGrygorii Strashko port_np, ret); 2026be565ec7SWang Qing goto of_node_put; 202793a76530SGrygorii Strashko } 202893a76530SGrygorii Strashko 202993a76530SGrygorii Strashko if (!port_id || port_id > common->port_num) { 203093a76530SGrygorii Strashko dev_err(dev, "%pOF has invalid port_id %u %s\n", 203193a76530SGrygorii Strashko port_np, port_id, port_np->name); 2032be565ec7SWang Qing ret = -EINVAL; 2033be565ec7SWang Qing goto of_node_put; 203493a76530SGrygorii Strashko } 203593a76530SGrygorii Strashko 203693a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 203793a76530SGrygorii Strashko port->port_id = port_id; 203893a76530SGrygorii Strashko port->common = common; 203993a76530SGrygorii Strashko port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE + 204093a76530SGrygorii Strashko AM65_CPSW_NU_PORTS_OFFSET * (port_id); 204137184fc1SSiddharth Vadapalli if (common->pdata.extra_modes) 204237184fc1SSiddharth Vadapalli port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id); 204393a76530SGrygorii Strashko port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE + 204493a76530SGrygorii Strashko (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id); 204593a76530SGrygorii Strashko port->name = of_get_property(port_np, "label", NULL); 20468127224cSIvan Khoronzhuk port->fetch_ram_base = 20478127224cSIvan Khoronzhuk common->cpsw_base + AM65_CPSW_NU_FRAM_BASE + 20488127224cSIvan Khoronzhuk (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1)); 204993a76530SGrygorii Strashko 2050d6d0aeafSGrygorii Strashko port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base); 2051be565ec7SWang Qing if (IS_ERR(port->slave.mac_sl)) { 2052be565ec7SWang Qing ret = PTR_ERR(port->slave.mac_sl); 2053be565ec7SWang Qing goto of_node_put; 2054be565ec7SWang Qing } 2055d6d0aeafSGrygorii Strashko 205693a76530SGrygorii Strashko port->disabled = !of_device_is_available(port_np); 205784b4aa49SGrygorii Strashko if (port->disabled) { 205884b4aa49SGrygorii Strashko common->disabled_ports_mask |= BIT(port->port_id); 205993a76530SGrygorii Strashko continue; 206084b4aa49SGrygorii Strashko } 206193a76530SGrygorii Strashko 206293a76530SGrygorii Strashko port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL); 206393a76530SGrygorii Strashko if (IS_ERR(port->slave.ifphy)) { 206493a76530SGrygorii Strashko ret = PTR_ERR(port->slave.ifphy); 206593a76530SGrygorii Strashko dev_err(dev, "%pOF error retrieving port phy: %d\n", 206693a76530SGrygorii Strashko port_np, ret); 2067be565ec7SWang Qing goto of_node_put; 206893a76530SGrygorii Strashko } 206993a76530SGrygorii Strashko 2070dab2b265SSiddharth Vadapalli /* Initialize the Serdes PHY for the port */ 2071dab2b265SSiddharth Vadapalli ret = am65_cpsw_init_serdes_phy(dev, port_np, port); 2072dab2b265SSiddharth Vadapalli if (ret) 2073374283a1SDan Carpenter goto of_node_put; 2074dab2b265SSiddharth Vadapalli 207593a76530SGrygorii Strashko port->slave.mac_only = 207693a76530SGrygorii Strashko of_property_read_bool(port_np, "ti,mac-only"); 207793a76530SGrygorii Strashko 207893a76530SGrygorii Strashko /* get phy/link info */ 2079e8609e69SSiddharth Vadapalli port->slave.phy_node = port_np; 208093a76530SGrygorii Strashko ret = of_get_phy_mode(port_np, &port->slave.phy_if); 208193a76530SGrygorii Strashko if (ret) { 208293a76530SGrygorii Strashko dev_err(dev, "%pOF read phy-mode err %d\n", 208393a76530SGrygorii Strashko port_np, ret); 2084be565ec7SWang Qing goto of_node_put; 208593a76530SGrygorii Strashko } 208693a76530SGrygorii Strashko 2087763015a7SSiddharth Vadapalli ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if); 2088763015a7SSiddharth Vadapalli if (ret) 2089763015a7SSiddharth Vadapalli goto of_node_put; 2090763015a7SSiddharth Vadapalli 209183216e39SMichael Walle ret = of_get_mac_address(port_np, port->slave.mac_addr); 209283216e39SMichael Walle if (ret) { 209383216e39SMichael Walle am65_cpsw_am654_get_efuse_macid(port_np, 209493a76530SGrygorii Strashko port->port_id, 209583216e39SMichael Walle port->slave.mac_addr); 209683216e39SMichael Walle if (!is_valid_ether_addr(port->slave.mac_addr)) { 2097ba530feaSJakub Kicinski eth_random_addr(port->slave.mac_addr); 209876dcbd23SColin Ian King dev_err(dev, "Use random MAC address\n"); 209993a76530SGrygorii Strashko } 210093a76530SGrygorii Strashko } 210183216e39SMichael Walle } 210293a76530SGrygorii Strashko of_node_put(node); 210393a76530SGrygorii Strashko 210484b4aa49SGrygorii Strashko /* is there at least one ext.port */ 210584b4aa49SGrygorii Strashko if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) { 210684b4aa49SGrygorii Strashko dev_err(dev, "No Ext. port are available\n"); 210784b4aa49SGrygorii Strashko return -ENODEV; 210884b4aa49SGrygorii Strashko } 210984b4aa49SGrygorii Strashko 211093a76530SGrygorii Strashko return 0; 2111be565ec7SWang Qing 2112be565ec7SWang Qing of_node_put: 2113be565ec7SWang Qing of_node_put(port_np); 2114be565ec7SWang Qing of_node_put(node); 2115be565ec7SWang Qing return ret; 211693a76530SGrygorii Strashko } 211793a76530SGrygorii Strashko 211893a76530SGrygorii Strashko static void am65_cpsw_pcpu_stats_free(void *data) 211993a76530SGrygorii Strashko { 212093a76530SGrygorii Strashko struct am65_cpsw_ndev_stats __percpu *stats = data; 212193a76530SGrygorii Strashko 212293a76530SGrygorii Strashko free_percpu(stats); 212393a76530SGrygorii Strashko } 212493a76530SGrygorii Strashko 2125e8609e69SSiddharth Vadapalli static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common) 2126e8609e69SSiddharth Vadapalli { 2127e8609e69SSiddharth Vadapalli struct am65_cpsw_port *port; 2128e8609e69SSiddharth Vadapalli int i; 2129e8609e69SSiddharth Vadapalli 2130e8609e69SSiddharth Vadapalli for (i = 0; i < common->port_num; i++) { 2131e8609e69SSiddharth Vadapalli port = &common->ports[i]; 2132e8609e69SSiddharth Vadapalli if (port->slave.phylink) 2133e8609e69SSiddharth Vadapalli phylink_destroy(port->slave.phylink); 2134e8609e69SSiddharth Vadapalli } 2135e8609e69SSiddharth Vadapalli } 2136e8609e69SSiddharth Vadapalli 213784b4aa49SGrygorii Strashko static int 213884b4aa49SGrygorii Strashko am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx) 213993a76530SGrygorii Strashko { 214093a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 214193a76530SGrygorii Strashko struct device *dev = common->dev; 214293a76530SGrygorii Strashko struct am65_cpsw_port *port; 2143e8609e69SSiddharth Vadapalli struct phylink *phylink; 214493a76530SGrygorii Strashko int ret; 214593a76530SGrygorii Strashko 214684b4aa49SGrygorii Strashko port = &common->ports[port_idx]; 214784b4aa49SGrygorii Strashko 214884b4aa49SGrygorii Strashko if (port->disabled) 214984b4aa49SGrygorii Strashko return 0; 215093a76530SGrygorii Strashko 215193a76530SGrygorii Strashko /* alloc netdev */ 215293a76530SGrygorii Strashko port->ndev = devm_alloc_etherdev_mqs(common->dev, 215393a76530SGrygorii Strashko sizeof(struct am65_cpsw_ndev_priv), 215493a76530SGrygorii Strashko AM65_CPSW_MAX_TX_QUEUES, 215593a76530SGrygorii Strashko AM65_CPSW_MAX_RX_QUEUES); 215693a76530SGrygorii Strashko if (!port->ndev) { 215793a76530SGrygorii Strashko dev_err(dev, "error allocating slave net_device %u\n", 215893a76530SGrygorii Strashko port->port_id); 215993a76530SGrygorii Strashko return -ENOMEM; 216093a76530SGrygorii Strashko } 216193a76530SGrygorii Strashko 216293a76530SGrygorii Strashko ndev_priv = netdev_priv(port->ndev); 216393a76530SGrygorii Strashko ndev_priv->port = port; 216493a76530SGrygorii Strashko ndev_priv->msg_enable = AM65_CPSW_DEBUG; 216593a76530SGrygorii Strashko SET_NETDEV_DEV(port->ndev, dev); 216693a76530SGrygorii Strashko 2167f3956ebbSJakub Kicinski eth_hw_addr_set(port->ndev, port->slave.mac_addr); 216893a76530SGrygorii Strashko 216993a76530SGrygorii Strashko port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE; 217093a76530SGrygorii Strashko port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE; 217193a76530SGrygorii Strashko port->ndev->hw_features = NETIF_F_SG | 217293a76530SGrygorii Strashko NETIF_F_RXCSUM | 21732c4dc314SMurali Karicheri NETIF_F_HW_CSUM | 21742c4dc314SMurali Karicheri NETIF_F_HW_TC; 217593a76530SGrygorii Strashko port->ndev->features = port->ndev->hw_features | 217693a76530SGrygorii Strashko NETIF_F_HW_VLAN_CTAG_FILTER; 217793a76530SGrygorii Strashko port->ndev->vlan_features |= NETIF_F_SG; 217884b4aa49SGrygorii Strashko port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops; 217993a76530SGrygorii Strashko port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave; 218093a76530SGrygorii Strashko 2181e8609e69SSiddharth Vadapalli /* Configuring Phylink */ 2182e8609e69SSiddharth Vadapalli port->slave.phylink_config.dev = &port->ndev->dev; 2183e8609e69SSiddharth Vadapalli port->slave.phylink_config.type = PHYLINK_NETDEV; 21848e672b56SSiddharth Vadapalli port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | 21858e672b56SSiddharth Vadapalli MAC_1000FD | MAC_5000FD; 218624bc19b0SRoger Quadros port->slave.phylink_config.mac_managed_pm = true; /* MAC does PM */ 2187e8609e69SSiddharth Vadapalli 2188a2935a1cSSiddharth Vadapalli switch (port->slave.phy_if) { 2189a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_RGMII: 2190a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_RGMII_ID: 2191a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_RGMII_RXID: 2192a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_RGMII_TXID: 2193e8609e69SSiddharth Vadapalli phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces); 2194a2935a1cSSiddharth Vadapalli break; 2195a2935a1cSSiddharth Vadapalli 2196a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_RMII: 219737184fc1SSiddharth Vadapalli __set_bit(PHY_INTERFACE_MODE_RMII, 219837184fc1SSiddharth Vadapalli port->slave.phylink_config.supported_interfaces); 2199a2935a1cSSiddharth Vadapalli break; 2200a2935a1cSSiddharth Vadapalli 2201a2935a1cSSiddharth Vadapalli case PHY_INTERFACE_MODE_QSGMII: 2202e0f72db3SSiddharth Vadapalli case PHY_INTERFACE_MODE_SGMII: 22038e672b56SSiddharth Vadapalli case PHY_INTERFACE_MODE_USXGMII: 2204a2935a1cSSiddharth Vadapalli if (common->pdata.extra_modes & BIT(port->slave.phy_if)) { 2205a2935a1cSSiddharth Vadapalli __set_bit(port->slave.phy_if, 220637184fc1SSiddharth Vadapalli port->slave.phylink_config.supported_interfaces); 220737184fc1SSiddharth Vadapalli } else { 220837184fc1SSiddharth Vadapalli dev_err(dev, "selected phy-mode is not supported\n"); 220937184fc1SSiddharth Vadapalli return -EOPNOTSUPP; 221037184fc1SSiddharth Vadapalli } 2211a2935a1cSSiddharth Vadapalli break; 2212a2935a1cSSiddharth Vadapalli 2213a2935a1cSSiddharth Vadapalli default: 2214a2935a1cSSiddharth Vadapalli dev_err(dev, "selected phy-mode is not supported\n"); 2215a2935a1cSSiddharth Vadapalli return -EOPNOTSUPP; 2216a2935a1cSSiddharth Vadapalli } 2217e8609e69SSiddharth Vadapalli 22180b718007SSiddharth Vadapalli phylink = phylink_create(&port->slave.phylink_config, 22190b718007SSiddharth Vadapalli of_node_to_fwnode(port->slave.phy_node), 22200b718007SSiddharth Vadapalli port->slave.phy_if, 2221e8609e69SSiddharth Vadapalli &am65_cpsw_phylink_mac_ops); 2222e8609e69SSiddharth Vadapalli if (IS_ERR(phylink)) 2223e8609e69SSiddharth Vadapalli return PTR_ERR(phylink); 2224e8609e69SSiddharth Vadapalli 2225e8609e69SSiddharth Vadapalli port->slave.phylink = phylink; 2226e8609e69SSiddharth Vadapalli 222793a76530SGrygorii Strashko /* Disable TX checksum offload by default due to HW bug */ 222838389aa6SGrygorii Strashko if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM) 222993a76530SGrygorii Strashko port->ndev->features &= ~NETIF_F_HW_CSUM; 223093a76530SGrygorii Strashko 223193a76530SGrygorii Strashko ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats); 223293a76530SGrygorii Strashko if (!ndev_priv->stats) 223393a76530SGrygorii Strashko return -ENOMEM; 223493a76530SGrygorii Strashko 223593a76530SGrygorii Strashko ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free, 223693a76530SGrygorii Strashko ndev_priv->stats); 223784b4aa49SGrygorii Strashko if (ret) 223884b4aa49SGrygorii Strashko dev_err(dev, "failed to add percpu stat free action %d\n", ret); 223984b4aa49SGrygorii Strashko 224084b4aa49SGrygorii Strashko if (!common->dma_ndev) 224184b4aa49SGrygorii Strashko common->dma_ndev = port->ndev; 224284b4aa49SGrygorii Strashko 224393a76530SGrygorii Strashko return ret; 224493a76530SGrygorii Strashko } 224593a76530SGrygorii Strashko 224684b4aa49SGrygorii Strashko static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common) 224784b4aa49SGrygorii Strashko { 224884b4aa49SGrygorii Strashko int ret; 224984b4aa49SGrygorii Strashko int i; 225084b4aa49SGrygorii Strashko 225184b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 225284b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_port_ndev(common, i); 225384b4aa49SGrygorii Strashko if (ret) 225484b4aa49SGrygorii Strashko return ret; 225584b4aa49SGrygorii Strashko } 225684b4aa49SGrygorii Strashko 22571a014663SRoger Quadros return ret; 225893a76530SGrygorii Strashko } 225993a76530SGrygorii Strashko 226084b4aa49SGrygorii Strashko static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common) 226184b4aa49SGrygorii Strashko { 226284b4aa49SGrygorii Strashko struct am65_cpsw_port *port; 226384b4aa49SGrygorii Strashko int i; 226484b4aa49SGrygorii Strashko 226584b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 226684b4aa49SGrygorii Strashko port = &common->ports[i]; 226746fb6512SZhang Changzhong if (port->ndev && port->ndev->reg_state == NETREG_REGISTERED) 226884b4aa49SGrygorii Strashko unregister_netdev(port->ndev); 226984b4aa49SGrygorii Strashko } 227084b4aa49SGrygorii Strashko } 227184b4aa49SGrygorii Strashko 22722934db9bSVignesh Raghavendra static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common) 22732934db9bSVignesh Raghavendra { 22742934db9bSVignesh Raghavendra int set_val = 0; 22752934db9bSVignesh Raghavendra int i; 22762934db9bSVignesh Raghavendra 22772934db9bSVignesh Raghavendra if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask)) 22782934db9bSVignesh Raghavendra set_val = 1; 22792934db9bSVignesh Raghavendra 22802934db9bSVignesh Raghavendra dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val); 22812934db9bSVignesh Raghavendra 22822934db9bSVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 22832934db9bSVignesh Raghavendra struct am65_cpsw_port *port = am65_common_get_port(common, i); 2284ae03d189SGrygorii Strashko struct am65_cpsw_ndev_priv *priv; 22852934db9bSVignesh Raghavendra 2286ae03d189SGrygorii Strashko if (!port->ndev) 2287ae03d189SGrygorii Strashko continue; 2288ae03d189SGrygorii Strashko 2289ae03d189SGrygorii Strashko priv = am65_ndev_to_priv(port->ndev); 22902934db9bSVignesh Raghavendra priv->offload_fwd_mark = set_val; 22912934db9bSVignesh Raghavendra } 22922934db9bSVignesh Raghavendra } 22932934db9bSVignesh Raghavendra 22942934db9bSVignesh Raghavendra bool am65_cpsw_port_dev_check(const struct net_device *ndev) 22952934db9bSVignesh Raghavendra { 22962934db9bSVignesh Raghavendra if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) { 22972934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 22982934db9bSVignesh Raghavendra 22992934db9bSVignesh Raghavendra return !common->is_emac_mode; 23002934db9bSVignesh Raghavendra } 23012934db9bSVignesh Raghavendra 23022934db9bSVignesh Raghavendra return false; 23032934db9bSVignesh Raghavendra } 23042934db9bSVignesh Raghavendra 23052f5dc00fSVladimir Oltean static int am65_cpsw_netdevice_port_link(struct net_device *ndev, 23062f5dc00fSVladimir Oltean struct net_device *br_ndev, 23072f5dc00fSVladimir Oltean struct netlink_ext_ack *extack) 23082934db9bSVignesh Raghavendra { 23092934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 23102934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 23112f5dc00fSVladimir Oltean int err; 23122934db9bSVignesh Raghavendra 23132934db9bSVignesh Raghavendra if (!common->br_members) { 23142934db9bSVignesh Raghavendra common->hw_bridge_dev = br_ndev; 23152934db9bSVignesh Raghavendra } else { 23162934db9bSVignesh Raghavendra /* This is adding the port to a second bridge, this is 23172934db9bSVignesh Raghavendra * unsupported 23182934db9bSVignesh Raghavendra */ 23192934db9bSVignesh Raghavendra if (common->hw_bridge_dev != br_ndev) 23202934db9bSVignesh Raghavendra return -EOPNOTSUPP; 23212934db9bSVignesh Raghavendra } 23222934db9bSVignesh Raghavendra 23234e51bf44SVladimir Oltean err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL, 232447211192STobias Waldekranz false, extack); 23252f5dc00fSVladimir Oltean if (err) 23262f5dc00fSVladimir Oltean return err; 23272f5dc00fSVladimir Oltean 23282934db9bSVignesh Raghavendra common->br_members |= BIT(priv->port->port_id); 23292934db9bSVignesh Raghavendra 23302934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 23312934db9bSVignesh Raghavendra 23322934db9bSVignesh Raghavendra return NOTIFY_DONE; 23332934db9bSVignesh Raghavendra } 23342934db9bSVignesh Raghavendra 23352934db9bSVignesh Raghavendra static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev) 23362934db9bSVignesh Raghavendra { 23372934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 23382934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 23392934db9bSVignesh Raghavendra 23404e51bf44SVladimir Oltean switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL); 23412f5dc00fSVladimir Oltean 23422934db9bSVignesh Raghavendra common->br_members &= ~BIT(priv->port->port_id); 23432934db9bSVignesh Raghavendra 23442934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 23452934db9bSVignesh Raghavendra 23462934db9bSVignesh Raghavendra if (!common->br_members) 23472934db9bSVignesh Raghavendra common->hw_bridge_dev = NULL; 23482934db9bSVignesh Raghavendra } 23492934db9bSVignesh Raghavendra 23502934db9bSVignesh Raghavendra /* netdev notifier */ 23512934db9bSVignesh Raghavendra static int am65_cpsw_netdevice_event(struct notifier_block *unused, 23522934db9bSVignesh Raghavendra unsigned long event, void *ptr) 23532934db9bSVignesh Raghavendra { 23542f5dc00fSVladimir Oltean struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 23552934db9bSVignesh Raghavendra struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 23562934db9bSVignesh Raghavendra struct netdev_notifier_changeupper_info *info; 23572934db9bSVignesh Raghavendra int ret = NOTIFY_DONE; 23582934db9bSVignesh Raghavendra 23592934db9bSVignesh Raghavendra if (!am65_cpsw_port_dev_check(ndev)) 23602934db9bSVignesh Raghavendra return NOTIFY_DONE; 23612934db9bSVignesh Raghavendra 23622934db9bSVignesh Raghavendra switch (event) { 23632934db9bSVignesh Raghavendra case NETDEV_CHANGEUPPER: 23642934db9bSVignesh Raghavendra info = ptr; 23652934db9bSVignesh Raghavendra 23662934db9bSVignesh Raghavendra if (netif_is_bridge_master(info->upper_dev)) { 23672934db9bSVignesh Raghavendra if (info->linking) 23682f5dc00fSVladimir Oltean ret = am65_cpsw_netdevice_port_link(ndev, 23692f5dc00fSVladimir Oltean info->upper_dev, 23702f5dc00fSVladimir Oltean extack); 23712934db9bSVignesh Raghavendra else 23722934db9bSVignesh Raghavendra am65_cpsw_netdevice_port_unlink(ndev); 23732934db9bSVignesh Raghavendra } 23742934db9bSVignesh Raghavendra break; 23752934db9bSVignesh Raghavendra default: 23762934db9bSVignesh Raghavendra return NOTIFY_DONE; 23772934db9bSVignesh Raghavendra } 23782934db9bSVignesh Raghavendra 23792934db9bSVignesh Raghavendra return notifier_from_errno(ret); 23802934db9bSVignesh Raghavendra } 23812934db9bSVignesh Raghavendra 23822934db9bSVignesh Raghavendra static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw) 23832934db9bSVignesh Raghavendra { 23842934db9bSVignesh Raghavendra int ret = 0; 23852934db9bSVignesh Raghavendra 23862934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 23872934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 23882934db9bSVignesh Raghavendra return 0; 23892934db9bSVignesh Raghavendra 23902934db9bSVignesh Raghavendra cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event; 23912934db9bSVignesh Raghavendra ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 23922934db9bSVignesh Raghavendra if (ret) { 23932934db9bSVignesh Raghavendra dev_err(cpsw->dev, "can't register netdevice notifier\n"); 23942934db9bSVignesh Raghavendra return ret; 23952934db9bSVignesh Raghavendra } 23962934db9bSVignesh Raghavendra 239786e8b070SVignesh Raghavendra ret = am65_cpsw_switchdev_register_notifiers(cpsw); 239886e8b070SVignesh Raghavendra if (ret) 239986e8b070SVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 240086e8b070SVignesh Raghavendra 24012934db9bSVignesh Raghavendra return ret; 24022934db9bSVignesh Raghavendra } 24032934db9bSVignesh Raghavendra 24042934db9bSVignesh Raghavendra static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw) 24052934db9bSVignesh Raghavendra { 24062934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 24072934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 24082934db9bSVignesh Raghavendra return; 24092934db9bSVignesh Raghavendra 241086e8b070SVignesh Raghavendra am65_cpsw_switchdev_unregister_notifiers(cpsw); 24112934db9bSVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 24122934db9bSVignesh Raghavendra } 24132934db9bSVignesh Raghavendra 241458356eb3SVignesh Raghavendra static const struct devlink_ops am65_cpsw_devlink_ops = {}; 241558356eb3SVignesh Raghavendra 241658356eb3SVignesh Raghavendra static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw) 241758356eb3SVignesh Raghavendra { 241858356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0, 241958356eb3SVignesh Raghavendra ALE_MCAST_BLOCK_LEARN_FWD); 242058356eb3SVignesh Raghavendra } 242158356eb3SVignesh Raghavendra 242258356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common) 242358356eb3SVignesh Raghavendra { 242458356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 242558356eb3SVignesh Raghavendra 242658356eb3SVignesh Raghavendra writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 242758356eb3SVignesh Raghavendra 242858356eb3SVignesh Raghavendra am65_cpsw_init_stp_ale_entry(common); 242958356eb3SVignesh Raghavendra 243058356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1); 243158356eb3SVignesh Raghavendra dev_dbg(common->dev, "Set P0_UNI_FLOOD\n"); 243258356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0); 243358356eb3SVignesh Raghavendra } 243458356eb3SVignesh Raghavendra 243558356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common) 243658356eb3SVignesh Raghavendra { 243758356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 243858356eb3SVignesh Raghavendra 243958356eb3SVignesh Raghavendra writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 244058356eb3SVignesh Raghavendra 244158356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0); 244258356eb3SVignesh Raghavendra dev_dbg(common->dev, "unset P0_UNI_FLOOD\n"); 244358356eb3SVignesh Raghavendra 244458356eb3SVignesh Raghavendra /* learning make no sense in multi-mac mode */ 244558356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1); 244658356eb3SVignesh Raghavendra } 244758356eb3SVignesh Raghavendra 244858356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id, 244958356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 245058356eb3SVignesh Raghavendra { 245158356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 245258356eb3SVignesh Raghavendra struct am65_cpsw_common *common = dl_priv->common; 245358356eb3SVignesh Raghavendra 245458356eb3SVignesh Raghavendra dev_dbg(common->dev, "%s id:%u\n", __func__, id); 245558356eb3SVignesh Raghavendra 245658356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 245758356eb3SVignesh Raghavendra return -EOPNOTSUPP; 245858356eb3SVignesh Raghavendra 245958356eb3SVignesh Raghavendra ctx->val.vbool = !common->is_emac_mode; 246058356eb3SVignesh Raghavendra 246158356eb3SVignesh Raghavendra return 0; 246258356eb3SVignesh Raghavendra } 246358356eb3SVignesh Raghavendra 246458356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port) 246558356eb3SVignesh Raghavendra { 246658356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 246758356eb3SVignesh Raghavendra struct am65_cpsw_common *common = port->common; 246858356eb3SVignesh Raghavendra u32 port_mask; 246958356eb3SVignesh Raghavendra 247058356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 247158356eb3SVignesh Raghavendra 247258356eb3SVignesh Raghavendra if (slave->mac_only) 247358356eb3SVignesh Raghavendra /* enable mac-only mode on port */ 247458356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, 247558356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 1); 247658356eb3SVignesh Raghavendra 247758356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1); 247858356eb3SVignesh Raghavendra 247958356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 248058356eb3SVignesh Raghavendra 248158356eb3SVignesh Raghavendra cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr, 248258356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE, slave->port_vlan); 248358356eb3SVignesh Raghavendra cpsw_ale_add_mcast(common->ale, port->ndev->broadcast, 248458356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2); 248558356eb3SVignesh Raghavendra } 248658356eb3SVignesh Raghavendra 248758356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port) 248858356eb3SVignesh Raghavendra { 248958356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 249058356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = port->common; 249158356eb3SVignesh Raghavendra u32 port_mask; 249258356eb3SVignesh Raghavendra 249358356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 249458356eb3SVignesh Raghavendra ALE_PORT_NOLEARN, 0); 249558356eb3SVignesh Raghavendra 249658356eb3SVignesh Raghavendra cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr, 249758356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN, 249858356eb3SVignesh Raghavendra slave->port_vlan); 249958356eb3SVignesh Raghavendra 250058356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 250158356eb3SVignesh Raghavendra 250258356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast, 250358356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, 250458356eb3SVignesh Raghavendra ALE_MCAST_FWD_2); 250558356eb3SVignesh Raghavendra 250658356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 250758356eb3SVignesh Raghavendra 250858356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 250958356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 0); 251058356eb3SVignesh Raghavendra } 251158356eb3SVignesh Raghavendra 251258356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id, 251358356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 251458356eb3SVignesh Raghavendra { 251558356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 251658356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = dl_priv->common; 251758356eb3SVignesh Raghavendra bool switch_en = ctx->val.vbool; 251858356eb3SVignesh Raghavendra bool if_running = false; 251958356eb3SVignesh Raghavendra int i; 252058356eb3SVignesh Raghavendra 252158356eb3SVignesh Raghavendra dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 252258356eb3SVignesh Raghavendra 252358356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 252458356eb3SVignesh Raghavendra return -EOPNOTSUPP; 252558356eb3SVignesh Raghavendra 252658356eb3SVignesh Raghavendra if (switch_en == !cpsw->is_emac_mode) 252758356eb3SVignesh Raghavendra return 0; 252858356eb3SVignesh Raghavendra 252958356eb3SVignesh Raghavendra if (!switch_en && cpsw->br_members) { 253058356eb3SVignesh Raghavendra dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n"); 253158356eb3SVignesh Raghavendra return -EINVAL; 253258356eb3SVignesh Raghavendra } 253358356eb3SVignesh Raghavendra 253458356eb3SVignesh Raghavendra rtnl_lock(); 253558356eb3SVignesh Raghavendra 253658356eb3SVignesh Raghavendra cpsw->is_emac_mode = !switch_en; 253758356eb3SVignesh Raghavendra 253858356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 253958356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 254058356eb3SVignesh Raghavendra 254158356eb3SVignesh Raghavendra if (!sl_ndev || !netif_running(sl_ndev)) 254258356eb3SVignesh Raghavendra continue; 254358356eb3SVignesh Raghavendra 254458356eb3SVignesh Raghavendra if_running = true; 254558356eb3SVignesh Raghavendra } 254658356eb3SVignesh Raghavendra 254758356eb3SVignesh Raghavendra if (!if_running) { 254858356eb3SVignesh Raghavendra /* all ndevs are down */ 254958356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 255058356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 255158356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 255258356eb3SVignesh Raghavendra 255358356eb3SVignesh Raghavendra if (!sl_ndev) 255458356eb3SVignesh Raghavendra continue; 255558356eb3SVignesh Raghavendra 255658356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 255758356eb3SVignesh Raghavendra if (switch_en) 255858356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 255958356eb3SVignesh Raghavendra else 256058356eb3SVignesh Raghavendra slave->port_vlan = 0; 256158356eb3SVignesh Raghavendra } 256258356eb3SVignesh Raghavendra 256358356eb3SVignesh Raghavendra goto exit; 256458356eb3SVignesh Raghavendra } 256558356eb3SVignesh Raghavendra 256658356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 256758356eb3SVignesh Raghavendra /* clean up ALE table */ 256858356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1); 256958356eb3SVignesh Raghavendra cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT); 257058356eb3SVignesh Raghavendra 257158356eb3SVignesh Raghavendra if (switch_en) { 257258356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Enable switch mode\n"); 257358356eb3SVignesh Raghavendra 257458356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(cpsw); 257558356eb3SVignesh Raghavendra 257658356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 257758356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 257858356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 257958356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 258058356eb3SVignesh Raghavendra 258158356eb3SVignesh Raghavendra if (!sl_ndev) 258258356eb3SVignesh Raghavendra continue; 258358356eb3SVignesh Raghavendra 258458356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 258558356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 258658356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 258758356eb3SVignesh Raghavendra 258858356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 258958356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 259058356eb3SVignesh Raghavendra } 259158356eb3SVignesh Raghavendra 259258356eb3SVignesh Raghavendra } else { 259358356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Disable switch mode\n"); 259458356eb3SVignesh Raghavendra 259558356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(cpsw); 259658356eb3SVignesh Raghavendra 259758356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 259858356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 259958356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 260058356eb3SVignesh Raghavendra 260158356eb3SVignesh Raghavendra if (!sl_ndev) 260258356eb3SVignesh Raghavendra continue; 260358356eb3SVignesh Raghavendra 260458356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 260558356eb3SVignesh Raghavendra port->slave.port_vlan = 0; 260658356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 260758356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 260858356eb3SVignesh Raghavendra } 260958356eb3SVignesh Raghavendra } 261058356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0); 261158356eb3SVignesh Raghavendra exit: 261258356eb3SVignesh Raghavendra rtnl_unlock(); 261358356eb3SVignesh Raghavendra 261458356eb3SVignesh Raghavendra return 0; 261558356eb3SVignesh Raghavendra } 261658356eb3SVignesh Raghavendra 261758356eb3SVignesh Raghavendra static const struct devlink_param am65_cpsw_devlink_params[] = { 261858356eb3SVignesh Raghavendra DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode", 261958356eb3SVignesh Raghavendra DEVLINK_PARAM_TYPE_BOOL, 262058356eb3SVignesh Raghavendra BIT(DEVLINK_PARAM_CMODE_RUNTIME), 262158356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_get, 262258356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_set, NULL), 262358356eb3SVignesh Raghavendra }; 262458356eb3SVignesh Raghavendra 262558356eb3SVignesh Raghavendra static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common) 262658356eb3SVignesh Raghavendra { 262758356eb3SVignesh Raghavendra struct devlink_port_attrs attrs = {}; 262858356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv; 262958356eb3SVignesh Raghavendra struct device *dev = common->dev; 263058356eb3SVignesh Raghavendra struct devlink_port *dl_port; 263158356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 263258356eb3SVignesh Raghavendra int ret = 0; 263358356eb3SVignesh Raghavendra int i; 263458356eb3SVignesh Raghavendra 263558356eb3SVignesh Raghavendra common->devlink = 2636919d13a7SLeon Romanovsky devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev); 263758356eb3SVignesh Raghavendra if (!common->devlink) 263858356eb3SVignesh Raghavendra return -ENOMEM; 263958356eb3SVignesh Raghavendra 264058356eb3SVignesh Raghavendra dl_priv = devlink_priv(common->devlink); 264158356eb3SVignesh Raghavendra dl_priv->common = common; 264258356eb3SVignesh Raghavendra 264358356eb3SVignesh Raghavendra /* Provide devlink hook to switch mode when multiple external ports 264458356eb3SVignesh Raghavendra * are present NUSS switchdev driver is enabled. 264558356eb3SVignesh Raghavendra */ 264658356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 264758356eb3SVignesh Raghavendra IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) { 264858356eb3SVignesh Raghavendra ret = devlink_params_register(common->devlink, 264958356eb3SVignesh Raghavendra am65_cpsw_devlink_params, 265058356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 265158356eb3SVignesh Raghavendra if (ret) { 265258356eb3SVignesh Raghavendra dev_err(dev, "devlink params reg fail ret:%d\n", ret); 265358356eb3SVignesh Raghavendra goto dl_unreg; 265458356eb3SVignesh Raghavendra } 265558356eb3SVignesh Raghavendra } 265658356eb3SVignesh Raghavendra 265758356eb3SVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 265858356eb3SVignesh Raghavendra port = am65_common_get_port(common, i); 265958356eb3SVignesh Raghavendra dl_port = &port->devlink_port; 266058356eb3SVignesh Raghavendra 26617e777b1bSMatthias Schiffer if (port->ndev) 266258356eb3SVignesh Raghavendra attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 26637e777b1bSMatthias Schiffer else 26647e777b1bSMatthias Schiffer attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; 266558356eb3SVignesh Raghavendra attrs.phys.port_number = port->port_id; 266658356eb3SVignesh Raghavendra attrs.switch_id.id_len = sizeof(resource_size_t); 266758356eb3SVignesh Raghavendra memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len); 266858356eb3SVignesh Raghavendra devlink_port_attrs_set(dl_port, &attrs); 266958356eb3SVignesh Raghavendra 267058356eb3SVignesh Raghavendra ret = devlink_port_register(common->devlink, dl_port, port->port_id); 267158356eb3SVignesh Raghavendra if (ret) { 267258356eb3SVignesh Raghavendra dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n", 267358356eb3SVignesh Raghavendra port->port_id, ret); 267458356eb3SVignesh Raghavendra goto dl_port_unreg; 267558356eb3SVignesh Raghavendra } 267658356eb3SVignesh Raghavendra } 26770d98ff22SLeon Romanovsky devlink_register(common->devlink); 267858356eb3SVignesh Raghavendra return ret; 267958356eb3SVignesh Raghavendra 268058356eb3SVignesh Raghavendra dl_port_unreg: 2681acf34954SLeon Romanovsky for (i = i - 1; i >= 1; i--) { 2682acf34954SLeon Romanovsky port = am65_common_get_port(common, i); 2683acf34954SLeon Romanovsky dl_port = &port->devlink_port; 2684acf34954SLeon Romanovsky 2685acf34954SLeon Romanovsky devlink_port_unregister(dl_port); 2686acf34954SLeon Romanovsky } 268758356eb3SVignesh Raghavendra dl_unreg: 268858356eb3SVignesh Raghavendra devlink_free(common->devlink); 268958356eb3SVignesh Raghavendra return ret; 269058356eb3SVignesh Raghavendra } 269158356eb3SVignesh Raghavendra 269258356eb3SVignesh Raghavendra static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common) 269358356eb3SVignesh Raghavendra { 2694acf34954SLeon Romanovsky struct devlink_port *dl_port; 2695acf34954SLeon Romanovsky struct am65_cpsw_port *port; 2696acf34954SLeon Romanovsky int i; 2697acf34954SLeon Romanovsky 26980d98ff22SLeon Romanovsky devlink_unregister(common->devlink); 26990d98ff22SLeon Romanovsky 2700acf34954SLeon Romanovsky for (i = 1; i <= common->port_num; i++) { 2701acf34954SLeon Romanovsky port = am65_common_get_port(common, i); 2702acf34954SLeon Romanovsky dl_port = &port->devlink_port; 2703acf34954SLeon Romanovsky 2704acf34954SLeon Romanovsky devlink_port_unregister(dl_port); 2705acf34954SLeon Romanovsky } 2706acf34954SLeon Romanovsky 270758356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 27080d98ff22SLeon Romanovsky IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 27090d98ff22SLeon Romanovsky devlink_params_unregister(common->devlink, 27100d98ff22SLeon Romanovsky am65_cpsw_devlink_params, 271158356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 271258356eb3SVignesh Raghavendra 271358356eb3SVignesh Raghavendra devlink_free(common->devlink); 271458356eb3SVignesh Raghavendra } 271558356eb3SVignesh Raghavendra 271684b4aa49SGrygorii Strashko static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common) 271793a76530SGrygorii Strashko { 271893a76530SGrygorii Strashko struct device *dev = common->dev; 271993a76530SGrygorii Strashko struct am65_cpsw_port *port; 272084b4aa49SGrygorii Strashko int ret = 0, i; 272193a76530SGrygorii Strashko 272224bc19b0SRoger Quadros /* init tx channels */ 272324bc19b0SRoger Quadros ret = am65_cpsw_nuss_init_tx_chns(common); 272493a76530SGrygorii Strashko if (ret) 272584b4aa49SGrygorii Strashko return ret; 272624bc19b0SRoger Quadros ret = am65_cpsw_nuss_init_rx_chns(common); 272724bc19b0SRoger Quadros if (ret) 27281a014663SRoger Quadros return ret; 27291a014663SRoger Quadros 27300680e20aSSiddharth Vadapalli ret = am65_cpsw_nuss_register_devlink(common); 27310680e20aSSiddharth Vadapalli if (ret) 27320680e20aSSiddharth Vadapalli return ret; 27330680e20aSSiddharth Vadapalli 273484b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 273584b4aa49SGrygorii Strashko port = &common->ports[i]; 273684b4aa49SGrygorii Strashko 273784b4aa49SGrygorii Strashko if (!port->ndev) 273884b4aa49SGrygorii Strashko continue; 273984b4aa49SGrygorii Strashko 2740ac73d4bfSJiri Pirko SET_NETDEV_DEVLINK_PORT(port->ndev, &port->devlink_port); 2741ac73d4bfSJiri Pirko 274293a76530SGrygorii Strashko ret = register_netdev(port->ndev); 274384b4aa49SGrygorii Strashko if (ret) { 274484b4aa49SGrygorii Strashko dev_err(dev, "error registering slave net device%i %d\n", 274584b4aa49SGrygorii Strashko i, ret); 274684b4aa49SGrygorii Strashko goto err_cleanup_ndev; 274784b4aa49SGrygorii Strashko } 274884b4aa49SGrygorii Strashko } 274984b4aa49SGrygorii Strashko 27502934db9bSVignesh Raghavendra ret = am65_cpsw_register_notifiers(common); 275158356eb3SVignesh Raghavendra if (ret) 275258356eb3SVignesh Raghavendra goto err_cleanup_ndev; 275393a76530SGrygorii Strashko 275493a76530SGrygorii Strashko /* can't auto unregister ndev using devm_add_action() due to 275593a76530SGrygorii Strashko * devres release sequence in DD core for DMA 275693a76530SGrygorii Strashko */ 275784b4aa49SGrygorii Strashko 275858356eb3SVignesh Raghavendra return 0; 27590680e20aSSiddharth Vadapalli 276084b4aa49SGrygorii Strashko err_cleanup_ndev: 276184b4aa49SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 27620680e20aSSiddharth Vadapalli am65_cpsw_unregister_devlink(common); 27632934db9bSVignesh Raghavendra 276493a76530SGrygorii Strashko return ret; 276593a76530SGrygorii Strashko } 276693a76530SGrygorii Strashko 276793a76530SGrygorii Strashko int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx) 276893a76530SGrygorii Strashko { 276993a76530SGrygorii Strashko int ret; 277093a76530SGrygorii Strashko 277193a76530SGrygorii Strashko common->tx_ch_num = num_tx; 277293a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_tx_chns(common); 277393a76530SGrygorii Strashko 277424bc19b0SRoger Quadros return ret; 277593a76530SGrygorii Strashko } 277693a76530SGrygorii Strashko 277738389aa6SGrygorii Strashko struct am65_cpsw_soc_pdata { 277838389aa6SGrygorii Strashko u32 quirks_dis; 277938389aa6SGrygorii Strashko }; 278038389aa6SGrygorii Strashko 278138389aa6SGrygorii Strashko static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = { 278238389aa6SGrygorii Strashko .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 278338389aa6SGrygorii Strashko }; 278438389aa6SGrygorii Strashko 278538389aa6SGrygorii Strashko static const struct soc_device_attribute am65_cpsw_socinfo[] = { 278638389aa6SGrygorii Strashko { .family = "AM65X", 278738389aa6SGrygorii Strashko .revision = "SR2.0", 278838389aa6SGrygorii Strashko .data = &am65x_soc_sr2_0 278938389aa6SGrygorii Strashko }, 279038389aa6SGrygorii Strashko {/* sentinel */} 279138389aa6SGrygorii Strashko }; 279238389aa6SGrygorii Strashko 279393a76530SGrygorii Strashko static const struct am65_cpsw_pdata am65x_sr1_0 = { 279493a76530SGrygorii Strashko .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 27957747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2796c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 279793a76530SGrygorii Strashko }; 279893a76530SGrygorii Strashko 279938389aa6SGrygorii Strashko static const struct am65_cpsw_pdata j721e_pdata = { 280093a76530SGrygorii Strashko .quirks = 0, 28017747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2802c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 280393a76530SGrygorii Strashko }; 280493a76530SGrygorii Strashko 28054f7cce27SVignesh Raghavendra static const struct am65_cpsw_pdata am64x_cpswxg_pdata = { 28060ed577e7SSiddharth Vadapalli .quirks = AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ, 28074f7cce27SVignesh Raghavendra .ale_dev_id = "am64-cpswxg", 28084f7cce27SVignesh Raghavendra .fdqring_mode = K3_RINGACC_RING_MODE_RING, 28094f7cce27SVignesh Raghavendra }; 28104f7cce27SVignesh Raghavendra 281137184fc1SSiddharth Vadapalli static const struct am65_cpsw_pdata j7200_cpswxg_pdata = { 281237184fc1SSiddharth Vadapalli .quirks = 0, 281337184fc1SSiddharth Vadapalli .ale_dev_id = "am64-cpswxg", 281437184fc1SSiddharth Vadapalli .fdqring_mode = K3_RINGACC_RING_MODE_RING, 28152e20e764SSiddharth Vadapalli .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII), 281637184fc1SSiddharth Vadapalli }; 281737184fc1SSiddharth Vadapalli 2818944131faSSiddharth Vadapalli static const struct am65_cpsw_pdata j721e_cpswxg_pdata = { 2819944131faSSiddharth Vadapalli .quirks = 0, 2820944131faSSiddharth Vadapalli .ale_dev_id = "am64-cpswxg", 2821944131faSSiddharth Vadapalli .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 2822186016daSSiddharth Vadapalli .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII), 2823944131faSSiddharth Vadapalli }; 2824944131faSSiddharth Vadapalli 28254e003d61SSiddharth Vadapalli static const struct am65_cpsw_pdata j784s4_cpswxg_pdata = { 28264e003d61SSiddharth Vadapalli .quirks = 0, 28274e003d61SSiddharth Vadapalli .ale_dev_id = "am64-cpswxg", 28284e003d61SSiddharth Vadapalli .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 28298e672b56SSiddharth Vadapalli .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_USXGMII), 28304e003d61SSiddharth Vadapalli }; 28314e003d61SSiddharth Vadapalli 283293a76530SGrygorii Strashko static const struct of_device_id am65_cpsw_nuss_of_mtable[] = { 283393a76530SGrygorii Strashko { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0}, 283438389aa6SGrygorii Strashko { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata}, 28354f7cce27SVignesh Raghavendra { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata}, 283637184fc1SSiddharth Vadapalli { .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata}, 2837944131faSSiddharth Vadapalli { .compatible = "ti,j721e-cpswxg-nuss", .data = &j721e_cpswxg_pdata}, 28384e003d61SSiddharth Vadapalli { .compatible = "ti,j784s4-cpswxg-nuss", .data = &j784s4_cpswxg_pdata}, 283993a76530SGrygorii Strashko { /* sentinel */ }, 284093a76530SGrygorii Strashko }; 284193a76530SGrygorii Strashko MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable); 284293a76530SGrygorii Strashko 284338389aa6SGrygorii Strashko static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common) 284438389aa6SGrygorii Strashko { 284538389aa6SGrygorii Strashko const struct soc_device_attribute *soc; 284638389aa6SGrygorii Strashko 284738389aa6SGrygorii Strashko soc = soc_device_match(am65_cpsw_socinfo); 284838389aa6SGrygorii Strashko if (soc && soc->data) { 284938389aa6SGrygorii Strashko const struct am65_cpsw_soc_pdata *socdata = soc->data; 285038389aa6SGrygorii Strashko 285138389aa6SGrygorii Strashko /* disable quirks */ 285238389aa6SGrygorii Strashko common->pdata.quirks &= ~socdata->quirks_dis; 285338389aa6SGrygorii Strashko } 285438389aa6SGrygorii Strashko } 285538389aa6SGrygorii Strashko 285693a76530SGrygorii Strashko static int am65_cpsw_nuss_probe(struct platform_device *pdev) 285793a76530SGrygorii Strashko { 28582074f9eaSGrygorii Strashko struct cpsw_ale_params ale_params = { 0 }; 285993a76530SGrygorii Strashko const struct of_device_id *of_id; 286093a76530SGrygorii Strashko struct device *dev = &pdev->dev; 286193a76530SGrygorii Strashko struct am65_cpsw_common *common; 286293a76530SGrygorii Strashko struct device_node *node; 286393a76530SGrygorii Strashko struct resource *res; 286423015ff1SGrygorii Strashko struct clk *clk; 286558356eb3SVignesh Raghavendra u64 id_temp; 286693a76530SGrygorii Strashko int ret, i; 2867020b232fSRoger Quadros int ale_entries; 286893a76530SGrygorii Strashko 286993a76530SGrygorii Strashko common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL); 287093a76530SGrygorii Strashko if (!common) 287193a76530SGrygorii Strashko return -ENOMEM; 287293a76530SGrygorii Strashko common->dev = dev; 287393a76530SGrygorii Strashko 287493a76530SGrygorii Strashko of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev); 287593a76530SGrygorii Strashko if (!of_id) 287693a76530SGrygorii Strashko return -EINVAL; 287738389aa6SGrygorii Strashko common->pdata = *(const struct am65_cpsw_pdata *)of_id->data; 287838389aa6SGrygorii Strashko 287938389aa6SGrygorii Strashko am65_cpsw_nuss_apply_socinfo(common); 288093a76530SGrygorii Strashko 288193a76530SGrygorii Strashko res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss"); 288293a76530SGrygorii Strashko common->ss_base = devm_ioremap_resource(&pdev->dev, res); 288393a76530SGrygorii Strashko if (IS_ERR(common->ss_base)) 288493a76530SGrygorii Strashko return PTR_ERR(common->ss_base); 288593a76530SGrygorii Strashko common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE; 288658356eb3SVignesh Raghavendra /* Use device's physical base address as switch id */ 288758356eb3SVignesh Raghavendra id_temp = cpu_to_be64(res->start); 288858356eb3SVignesh Raghavendra memcpy(common->switch_id, &id_temp, sizeof(res->start)); 288993a76530SGrygorii Strashko 289093a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 289193a76530SGrygorii Strashko if (!node) 289293a76530SGrygorii Strashko return -ENOENT; 289393a76530SGrygorii Strashko common->port_num = of_get_child_count(node); 28945dd89d2fSMiaoqian Lin of_node_put(node); 289593a76530SGrygorii Strashko if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS) 289693a76530SGrygorii Strashko return -ENOENT; 289793a76530SGrygorii Strashko 28981a014663SRoger Quadros common->rx_flow_id_base = -1; 289993a76530SGrygorii Strashko init_completion(&common->tdown_complete); 290093a76530SGrygorii Strashko common->tx_ch_num = 1; 290151824048SGrygorii Strashko common->pf_p0_rx_ptype_rrobin = false; 290258356eb3SVignesh Raghavendra common->default_vlan = 1; 290393a76530SGrygorii Strashko 290493a76530SGrygorii Strashko common->ports = devm_kcalloc(dev, common->port_num, 290593a76530SGrygorii Strashko sizeof(*common->ports), 290693a76530SGrygorii Strashko GFP_KERNEL); 290793a76530SGrygorii Strashko if (!common->ports) 290893a76530SGrygorii Strashko return -ENOMEM; 290993a76530SGrygorii Strashko 291023015ff1SGrygorii Strashko clk = devm_clk_get(dev, "fck"); 29118fbc2f9eSGrygorii Strashko if (IS_ERR(clk)) 29128fbc2f9eSGrygorii Strashko return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n"); 291323015ff1SGrygorii Strashko common->bus_freq = clk_get_rate(clk); 291423015ff1SGrygorii Strashko 291593a76530SGrygorii Strashko pm_runtime_enable(dev); 29162240514cSMinghao Chi ret = pm_runtime_resume_and_get(dev); 291793a76530SGrygorii Strashko if (ret < 0) { 291893a76530SGrygorii Strashko pm_runtime_disable(dev); 291993a76530SGrygorii Strashko return ret; 292093a76530SGrygorii Strashko } 292193a76530SGrygorii Strashko 2922a45cfcc6SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "mdio"); 2923a45cfcc6SGrygorii Strashko if (!node) { 2924a45cfcc6SGrygorii Strashko dev_warn(dev, "MDIO node not found\n"); 2925a45cfcc6SGrygorii Strashko } else if (of_device_is_available(node)) { 2926a45cfcc6SGrygorii Strashko struct platform_device *mdio_pdev; 2927a45cfcc6SGrygorii Strashko 2928a45cfcc6SGrygorii Strashko mdio_pdev = of_platform_device_create(node, NULL, dev); 2929a45cfcc6SGrygorii Strashko if (!mdio_pdev) { 2930a45cfcc6SGrygorii Strashko ret = -ENODEV; 2931a45cfcc6SGrygorii Strashko goto err_pm_clear; 2932a45cfcc6SGrygorii Strashko } 2933a45cfcc6SGrygorii Strashko 2934a45cfcc6SGrygorii Strashko common->mdio_dev = &mdio_pdev->dev; 2935a45cfcc6SGrygorii Strashko } 2936a45cfcc6SGrygorii Strashko of_node_put(node); 293793a76530SGrygorii Strashko 293893a76530SGrygorii Strashko am65_cpsw_nuss_get_ver(common); 293993a76530SGrygorii Strashko 294093a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_host_p(common); 294193a76530SGrygorii Strashko if (ret) 294293a76530SGrygorii Strashko goto err_of_clear; 294393a76530SGrygorii Strashko 294493a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_slave_ports(common); 294593a76530SGrygorii Strashko if (ret) 294693a76530SGrygorii Strashko goto err_of_clear; 294793a76530SGrygorii Strashko 294893a76530SGrygorii Strashko /* init common data */ 294993a76530SGrygorii Strashko ale_params.dev = dev; 295093a76530SGrygorii Strashko ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT; 295193a76530SGrygorii Strashko ale_params.ale_ports = common->port_num + 1; 295293a76530SGrygorii Strashko ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE; 29537747d4b7SGrygorii Strashko ale_params.dev_id = common->pdata.ale_dev_id; 295423015ff1SGrygorii Strashko ale_params.bus_freq = common->bus_freq; 295593a76530SGrygorii Strashko 295693a76530SGrygorii Strashko common->ale = cpsw_ale_create(&ale_params); 29571401cf60SWei Yongjun if (IS_ERR(common->ale)) { 295893a76530SGrygorii Strashko dev_err(dev, "error initializing ale engine\n"); 29591401cf60SWei Yongjun ret = PTR_ERR(common->ale); 296093a76530SGrygorii Strashko goto err_of_clear; 296193a76530SGrygorii Strashko } 296293a76530SGrygorii Strashko 2963020b232fSRoger Quadros ale_entries = common->ale->params.ale_entries; 2964020b232fSRoger Quadros common->ale_context = devm_kzalloc(dev, 2965020b232fSRoger Quadros ale_entries * ALE_ENTRY_WORDS * sizeof(u32), 2966020b232fSRoger Quadros GFP_KERNEL); 2967b1f66a5bSGrygorii Strashko ret = am65_cpsw_init_cpts(common); 2968b1f66a5bSGrygorii Strashko if (ret) 2969b1f66a5bSGrygorii Strashko goto err_of_clear; 2970b1f66a5bSGrygorii Strashko 297193a76530SGrygorii Strashko /* init ports */ 297293a76530SGrygorii Strashko for (i = 0; i < common->port_num; i++) 297393a76530SGrygorii Strashko am65_cpsw_nuss_slave_disable_unused(&common->ports[i]); 297493a76530SGrygorii Strashko 297593a76530SGrygorii Strashko dev_set_drvdata(dev, common); 297693a76530SGrygorii Strashko 297758356eb3SVignesh Raghavendra common->is_emac_mode = true; 297858356eb3SVignesh Raghavendra 297984b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_ndevs(common); 298093a76530SGrygorii Strashko if (ret) 2981e8609e69SSiddharth Vadapalli goto err_free_phylink; 298293a76530SGrygorii Strashko 298384b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_register_ndevs(common); 298493a76530SGrygorii Strashko if (ret) 2985e8609e69SSiddharth Vadapalli goto err_free_phylink; 298693a76530SGrygorii Strashko 298793a76530SGrygorii Strashko pm_runtime_put(dev); 298893a76530SGrygorii Strashko return 0; 298993a76530SGrygorii Strashko 2990e8609e69SSiddharth Vadapalli err_free_phylink: 2991e8609e69SSiddharth Vadapalli am65_cpsw_nuss_phylink_cleanup(common); 29924ad8766cSSiddharth Vadapalli am65_cpts_release(common->cpts); 299393a76530SGrygorii Strashko err_of_clear: 2994c6b486fbSSiddharth Vadapalli if (common->mdio_dev) 2995a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 2996a45cfcc6SGrygorii Strashko err_pm_clear: 299793a76530SGrygorii Strashko pm_runtime_put_sync(dev); 299893a76530SGrygorii Strashko pm_runtime_disable(dev); 299993a76530SGrygorii Strashko return ret; 300093a76530SGrygorii Strashko } 300193a76530SGrygorii Strashko 300293a76530SGrygorii Strashko static int am65_cpsw_nuss_remove(struct platform_device *pdev) 300393a76530SGrygorii Strashko { 300493a76530SGrygorii Strashko struct device *dev = &pdev->dev; 300593a76530SGrygorii Strashko struct am65_cpsw_common *common; 300693a76530SGrygorii Strashko int ret; 300793a76530SGrygorii Strashko 300893a76530SGrygorii Strashko common = dev_get_drvdata(dev); 300993a76530SGrygorii Strashko 30102240514cSMinghao Chi ret = pm_runtime_resume_and_get(&pdev->dev); 30112240514cSMinghao Chi if (ret < 0) 301293a76530SGrygorii Strashko return ret; 301393a76530SGrygorii Strashko 301458356eb3SVignesh Raghavendra am65_cpsw_unregister_devlink(common); 30152934db9bSVignesh Raghavendra am65_cpsw_unregister_notifiers(common); 301658356eb3SVignesh Raghavendra 301793a76530SGrygorii Strashko /* must unregister ndevs here because DD release_driver routine calls 301893a76530SGrygorii Strashko * dma_deconfigure(dev) before devres_release_all(dev) 301993a76530SGrygorii Strashko */ 302093a76530SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 30211a0c016aSRoger Quadros am65_cpsw_nuss_phylink_cleanup(common); 30224ad8766cSSiddharth Vadapalli am65_cpts_release(common->cpts); 3023dab2b265SSiddharth Vadapalli am65_cpsw_disable_serdes_phy(common); 302493a76530SGrygorii Strashko 3025c6b486fbSSiddharth Vadapalli if (common->mdio_dev) 3026a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 302793a76530SGrygorii Strashko 302893a76530SGrygorii Strashko pm_runtime_put_sync(&pdev->dev); 302993a76530SGrygorii Strashko pm_runtime_disable(&pdev->dev); 303093a76530SGrygorii Strashko return 0; 303193a76530SGrygorii Strashko } 303293a76530SGrygorii Strashko 303324bc19b0SRoger Quadros static int am65_cpsw_nuss_suspend(struct device *dev) 303424bc19b0SRoger Quadros { 303524bc19b0SRoger Quadros struct am65_cpsw_common *common = dev_get_drvdata(dev); 30361581cd8bSRoger Quadros struct am65_cpsw_host *host_p = am65_common_get_host(common); 303724bc19b0SRoger Quadros struct am65_cpsw_port *port; 303824bc19b0SRoger Quadros struct net_device *ndev; 303924bc19b0SRoger Quadros int i, ret; 304024bc19b0SRoger Quadros 3041020b232fSRoger Quadros cpsw_ale_dump(common->ale, common->ale_context); 30421581cd8bSRoger Quadros host_p->vid_context = readl(host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 304324bc19b0SRoger Quadros for (i = 0; i < common->port_num; i++) { 304424bc19b0SRoger Quadros port = &common->ports[i]; 304524bc19b0SRoger Quadros ndev = port->ndev; 304624bc19b0SRoger Quadros 304724bc19b0SRoger Quadros if (!ndev) 304824bc19b0SRoger Quadros continue; 304924bc19b0SRoger Quadros 30501581cd8bSRoger Quadros port->vid_context = readl(port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 305124bc19b0SRoger Quadros netif_device_detach(ndev); 305224bc19b0SRoger Quadros if (netif_running(ndev)) { 305324bc19b0SRoger Quadros rtnl_lock(); 305424bc19b0SRoger Quadros ret = am65_cpsw_nuss_ndo_slave_stop(ndev); 305524bc19b0SRoger Quadros rtnl_unlock(); 305624bc19b0SRoger Quadros if (ret < 0) { 305724bc19b0SRoger Quadros netdev_err(ndev, "failed to stop: %d", ret); 305824bc19b0SRoger Quadros return ret; 305924bc19b0SRoger Quadros } 306024bc19b0SRoger Quadros } 306124bc19b0SRoger Quadros } 306224bc19b0SRoger Quadros 306324bc19b0SRoger Quadros am65_cpts_suspend(common->cpts); 306424bc19b0SRoger Quadros 306524bc19b0SRoger Quadros am65_cpsw_nuss_remove_rx_chns(common); 306624bc19b0SRoger Quadros am65_cpsw_nuss_remove_tx_chns(common); 306724bc19b0SRoger Quadros 306824bc19b0SRoger Quadros return 0; 306924bc19b0SRoger Quadros } 307024bc19b0SRoger Quadros 307124bc19b0SRoger Quadros static int am65_cpsw_nuss_resume(struct device *dev) 307224bc19b0SRoger Quadros { 307324bc19b0SRoger Quadros struct am65_cpsw_common *common = dev_get_drvdata(dev); 307424bc19b0SRoger Quadros struct am65_cpsw_port *port; 307524bc19b0SRoger Quadros struct net_device *ndev; 307624bc19b0SRoger Quadros int i, ret; 30771581cd8bSRoger Quadros struct am65_cpsw_host *host_p = am65_common_get_host(common); 307824bc19b0SRoger Quadros 307924bc19b0SRoger Quadros ret = am65_cpsw_nuss_init_tx_chns(common); 308024bc19b0SRoger Quadros if (ret) 308124bc19b0SRoger Quadros return ret; 308224bc19b0SRoger Quadros ret = am65_cpsw_nuss_init_rx_chns(common); 308324bc19b0SRoger Quadros if (ret) 308424bc19b0SRoger Quadros return ret; 308524bc19b0SRoger Quadros 308624bc19b0SRoger Quadros /* If RX IRQ was disabled before suspend, keep it disabled */ 308724bc19b0SRoger Quadros if (common->rx_irq_disabled) 308824bc19b0SRoger Quadros disable_irq(common->rx_chns.irq); 308924bc19b0SRoger Quadros 309024bc19b0SRoger Quadros am65_cpts_resume(common->cpts); 309124bc19b0SRoger Quadros 309224bc19b0SRoger Quadros for (i = 0; i < common->port_num; i++) { 309324bc19b0SRoger Quadros port = &common->ports[i]; 309424bc19b0SRoger Quadros ndev = port->ndev; 309524bc19b0SRoger Quadros 309624bc19b0SRoger Quadros if (!ndev) 309724bc19b0SRoger Quadros continue; 309824bc19b0SRoger Quadros 309924bc19b0SRoger Quadros if (netif_running(ndev)) { 310024bc19b0SRoger Quadros rtnl_lock(); 310124bc19b0SRoger Quadros ret = am65_cpsw_nuss_ndo_slave_open(ndev); 310224bc19b0SRoger Quadros rtnl_unlock(); 310324bc19b0SRoger Quadros if (ret < 0) { 310424bc19b0SRoger Quadros netdev_err(ndev, "failed to start: %d", ret); 310524bc19b0SRoger Quadros return ret; 310624bc19b0SRoger Quadros } 310724bc19b0SRoger Quadros } 310824bc19b0SRoger Quadros 310924bc19b0SRoger Quadros netif_device_attach(ndev); 31101581cd8bSRoger Quadros writel(port->vid_context, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 311124bc19b0SRoger Quadros } 311224bc19b0SRoger Quadros 31131581cd8bSRoger Quadros writel(host_p->vid_context, host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 3114020b232fSRoger Quadros cpsw_ale_restore(common->ale, common->ale_context); 31151581cd8bSRoger Quadros 311624bc19b0SRoger Quadros return 0; 311724bc19b0SRoger Quadros } 311824bc19b0SRoger Quadros 311924bc19b0SRoger Quadros static const struct dev_pm_ops am65_cpsw_nuss_dev_pm_ops = { 3120078838f5SArnd Bergmann SYSTEM_SLEEP_PM_OPS(am65_cpsw_nuss_suspend, am65_cpsw_nuss_resume) 312124bc19b0SRoger Quadros }; 312224bc19b0SRoger Quadros 312393a76530SGrygorii Strashko static struct platform_driver am65_cpsw_nuss_driver = { 312493a76530SGrygorii Strashko .driver = { 312593a76530SGrygorii Strashko .name = AM65_CPSW_DRV_NAME, 312693a76530SGrygorii Strashko .of_match_table = am65_cpsw_nuss_of_mtable, 312724bc19b0SRoger Quadros .pm = &am65_cpsw_nuss_dev_pm_ops, 312893a76530SGrygorii Strashko }, 312993a76530SGrygorii Strashko .probe = am65_cpsw_nuss_probe, 313093a76530SGrygorii Strashko .remove = am65_cpsw_nuss_remove, 313193a76530SGrygorii Strashko }; 313293a76530SGrygorii Strashko 313393a76530SGrygorii Strashko module_platform_driver(am65_cpsw_nuss_driver); 313493a76530SGrygorii Strashko 313593a76530SGrygorii Strashko MODULE_LICENSE("GPL v2"); 313693a76530SGrygorii Strashko MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 313793a76530SGrygorii Strashko MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver"); 3138