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> 102f5dc00fSVladimir Oltean #include <linux/if_bridge.h> 1193a76530SGrygorii Strashko #include <linux/if_vlan.h> 1293a76530SGrygorii Strashko #include <linux/interrupt.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> 2293a76530SGrygorii Strashko #include <linux/phy.h> 2393a76530SGrygorii Strashko #include <linux/phy/phy.h> 2493a76530SGrygorii Strashko #include <linux/platform_device.h> 2593a76530SGrygorii Strashko #include <linux/pm_runtime.h> 2693a76530SGrygorii Strashko #include <linux/regmap.h> 2793a76530SGrygorii Strashko #include <linux/mfd/syscon.h> 2838389aa6SGrygorii Strashko #include <linux/sys_soc.h> 2993a76530SGrygorii Strashko #include <linux/dma/ti-cppi5.h> 3093a76530SGrygorii Strashko #include <linux/dma/k3-udma-glue.h> 3193a76530SGrygorii Strashko 3293a76530SGrygorii Strashko #include "cpsw_ale.h" 3393a76530SGrygorii Strashko #include "cpsw_sl.h" 3493a76530SGrygorii Strashko #include "am65-cpsw-nuss.h" 3586e8b070SVignesh Raghavendra #include "am65-cpsw-switchdev.h" 3693a76530SGrygorii Strashko #include "k3-cppi-desc-pool.h" 37b1f66a5bSGrygorii Strashko #include "am65-cpts.h" 3893a76530SGrygorii Strashko 3993a76530SGrygorii Strashko #define AM65_CPSW_SS_BASE 0x0 4093a76530SGrygorii Strashko #define AM65_CPSW_SGMII_BASE 0x100 4193a76530SGrygorii Strashko #define AM65_CPSW_XGMII_BASE 0x2100 4293a76530SGrygorii Strashko #define AM65_CPSW_CPSW_NU_BASE 0x20000 4393a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_BASE 0x1000 448127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_BASE 0x12000 4593a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_BASE 0x1a000 4693a76530SGrygorii Strashko #define AM65_CPSW_NU_ALE_BASE 0x1e000 4793a76530SGrygorii Strashko #define AM65_CPSW_NU_CPTS_BASE 0x1d000 4893a76530SGrygorii Strashko 4993a76530SGrygorii Strashko #define AM65_CPSW_NU_PORTS_OFFSET 0x1000 5093a76530SGrygorii Strashko #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200 518127224cSIvan Khoronzhuk #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200 5293a76530SGrygorii Strashko 5393a76530SGrygorii Strashko #define AM65_CPSW_MAX_PORTS 8 5493a76530SGrygorii Strashko 5593a76530SGrygorii Strashko #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN 5693a76530SGrygorii Strashko #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 5793a76530SGrygorii Strashko 5893a76530SGrygorii Strashko #define AM65_CPSW_REG_CTL 0x004 5993a76530SGrygorii Strashko #define AM65_CPSW_REG_STAT_PORT_EN 0x014 6093a76530SGrygorii Strashko #define AM65_CPSW_REG_PTYPE 0x018 6193a76530SGrygorii Strashko 6293a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL 0x004 6393a76530SGrygorii Strashko #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008 6493a76530SGrygorii Strashko 6593a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c 6693a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020 6793a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024 6893a76530SGrygorii Strashko 6993a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_L 0x308 7093a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_SA_H 0x30c 7193a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL 0x310 7293a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314 7393a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318 7493a76530SGrygorii Strashko #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C 7593a76530SGrygorii Strashko 7693a76530SGrygorii Strashko #define AM65_CPSW_CTL_VLAN_AWARE BIT(1) 7793a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_ENABLE BIT(2) 7893a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13) 7993a76530SGrygorii Strashko #define AM65_CPSW_CTL_P0_RX_PAD BIT(14) 8093a76530SGrygorii Strashko 8193a76530SGrygorii Strashko /* AM65_CPSW_P0_REG_CTL */ 8293a76530SGrygorii Strashko #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0) 8393a76530SGrygorii Strashko 8493a76530SGrygorii Strashko /* AM65_CPSW_PORT_REG_PRI_CTL */ 8593a76530SGrygorii Strashko #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8) 8693a76530SGrygorii Strashko 8793a76530SGrygorii Strashko /* AM65_CPSW_PN_TS_CTL register fields */ 8893a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4) 8993a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5) 9093a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6) 9193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7) 9293a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10) 9393a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11) 9493a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16 9593a76530SGrygorii Strashko 9693a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */ 9793a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16 9893a76530SGrygorii Strashko 9993a76530SGrygorii Strashko /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */ 10093a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16) 10193a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17) 10293a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18) 10393a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19) 10493a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20) 10593a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21) 10693a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22) 10793a76530SGrygorii Strashko #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23) 10893a76530SGrygorii Strashko 10993a76530SGrygorii Strashko /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 11093a76530SGrygorii Strashko #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 11193a76530SGrygorii Strashko 11293a76530SGrygorii Strashko #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e) 11393a76530SGrygorii Strashko 11493a76530SGrygorii Strashko #define AM65_CPSW_TS_TX_ANX_ALL_EN \ 11593a76530SGrygorii Strashko (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \ 11693a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \ 11793a76530SGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN) 11893a76530SGrygorii Strashko 11993a76530SGrygorii Strashko #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30 12093a76530SGrygorii Strashko /* Number of TX/RX descriptors */ 12193a76530SGrygorii Strashko #define AM65_CPSW_MAX_TX_DESC 500 12293a76530SGrygorii Strashko #define AM65_CPSW_MAX_RX_DESC 500 12393a76530SGrygorii Strashko 12493a76530SGrygorii Strashko #define AM65_CPSW_NAV_PS_DATA_SIZE 16 12593a76530SGrygorii Strashko #define AM65_CPSW_NAV_SW_DATA_SIZE 16 12693a76530SGrygorii Strashko 12793a76530SGrygorii Strashko #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \ 12893a76530SGrygorii Strashko NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \ 12993a76530SGrygorii Strashko NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR) 13093a76530SGrygorii Strashko 13193a76530SGrygorii Strashko static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave, 13293a76530SGrygorii Strashko const u8 *dev_addr) 13393a76530SGrygorii Strashko { 13493a76530SGrygorii Strashko u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) | 13593a76530SGrygorii Strashko (dev_addr[2] << 16) | (dev_addr[3] << 24); 13693a76530SGrygorii Strashko u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8); 13793a76530SGrygorii Strashko 13893a76530SGrygorii Strashko writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H); 13993a76530SGrygorii Strashko writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L); 14093a76530SGrygorii Strashko } 14193a76530SGrygorii Strashko 14293a76530SGrygorii Strashko static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port) 14393a76530SGrygorii Strashko { 14493a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 14593a76530SGrygorii Strashko /* Max length register has to be restored after MAC SL reset */ 14693a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 14793a76530SGrygorii Strashko port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 14893a76530SGrygorii Strashko } 14993a76530SGrygorii Strashko 15093a76530SGrygorii Strashko static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common) 15193a76530SGrygorii Strashko { 15293a76530SGrygorii Strashko common->nuss_ver = readl(common->ss_base); 15393a76530SGrygorii Strashko common->cpsw_ver = readl(common->cpsw_base); 15493a76530SGrygorii Strashko dev_info(common->dev, 15538389aa6SGrygorii Strashko "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n", 15693a76530SGrygorii Strashko common->nuss_ver, 15793a76530SGrygorii Strashko common->cpsw_ver, 15838389aa6SGrygorii Strashko common->port_num + 1, 15938389aa6SGrygorii Strashko common->pdata.quirks); 16093a76530SGrygorii Strashko } 16193a76530SGrygorii Strashko 16293a76530SGrygorii Strashko void am65_cpsw_nuss_adjust_link(struct net_device *ndev) 16393a76530SGrygorii Strashko { 16493a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 16593a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 16693a76530SGrygorii Strashko struct phy_device *phy = port->slave.phy; 16793a76530SGrygorii Strashko u32 mac_control = 0; 16893a76530SGrygorii Strashko 16993a76530SGrygorii Strashko if (!phy) 17093a76530SGrygorii Strashko return; 17193a76530SGrygorii Strashko 17293a76530SGrygorii Strashko if (phy->link) { 17393a76530SGrygorii Strashko mac_control = CPSW_SL_CTL_GMII_EN; 17493a76530SGrygorii Strashko 17593a76530SGrygorii Strashko if (phy->speed == 1000) 17693a76530SGrygorii Strashko mac_control |= CPSW_SL_CTL_GIG; 17793a76530SGrygorii Strashko if (phy->speed == 10 && phy_interface_is_rgmii(phy)) 17893a76530SGrygorii Strashko /* Can be used with in band mode only */ 17993a76530SGrygorii Strashko mac_control |= CPSW_SL_CTL_EXT_EN; 180c2f89219SGrygorii Strashko if (phy->speed == 100 && phy->interface == PHY_INTERFACE_MODE_RMII) 181c2f89219SGrygorii Strashko mac_control |= CPSW_SL_CTL_IFCTL_A; 18293a76530SGrygorii Strashko if (phy->duplex) 18393a76530SGrygorii Strashko mac_control |= CPSW_SL_CTL_FULLDUPLEX; 18493a76530SGrygorii Strashko 18593a76530SGrygorii Strashko /* RGMII speed is 100M if !CPSW_SL_CTL_GIG*/ 18693a76530SGrygorii Strashko 18793a76530SGrygorii Strashko /* rx_pause/tx_pause */ 18893a76530SGrygorii Strashko if (port->slave.rx_pause) 18993a76530SGrygorii Strashko mac_control |= CPSW_SL_CTL_RX_FLOW_EN; 19093a76530SGrygorii Strashko 19193a76530SGrygorii Strashko if (port->slave.tx_pause) 19293a76530SGrygorii Strashko mac_control |= CPSW_SL_CTL_TX_FLOW_EN; 19393a76530SGrygorii Strashko 19493a76530SGrygorii Strashko cpsw_sl_ctl_set(port->slave.mac_sl, mac_control); 19593a76530SGrygorii Strashko 19693a76530SGrygorii Strashko /* enable forwarding */ 19793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 19893a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 19993a76530SGrygorii Strashko 2008127224cSIvan Khoronzhuk am65_cpsw_qos_link_up(ndev, phy->speed); 20193a76530SGrygorii Strashko netif_tx_wake_all_queues(ndev); 20293a76530SGrygorii Strashko } else { 20393a76530SGrygorii Strashko int tmo; 2048127224cSIvan Khoronzhuk 20593a76530SGrygorii Strashko /* disable forwarding */ 20693a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 20793a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 20893a76530SGrygorii Strashko 20993a76530SGrygorii Strashko cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 21093a76530SGrygorii Strashko 21193a76530SGrygorii Strashko tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 21293a76530SGrygorii Strashko dev_dbg(common->dev, "donw msc_sl %08x tmo %d\n", 21393a76530SGrygorii Strashko cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), 21493a76530SGrygorii Strashko tmo); 21593a76530SGrygorii Strashko 21693a76530SGrygorii Strashko cpsw_sl_ctl_reset(port->slave.mac_sl); 21793a76530SGrygorii Strashko 2188127224cSIvan Khoronzhuk am65_cpsw_qos_link_down(ndev); 21993a76530SGrygorii Strashko netif_tx_stop_all_queues(ndev); 22093a76530SGrygorii Strashko } 22193a76530SGrygorii Strashko 22293a76530SGrygorii Strashko phy_print_status(phy); 22393a76530SGrygorii Strashko } 22493a76530SGrygorii Strashko 22593a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev, 22693a76530SGrygorii Strashko __be16 proto, u16 vid) 22793a76530SGrygorii Strashko { 22893a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 22993a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 23093a76530SGrygorii Strashko u32 port_mask, unreg_mcast = 0; 23193a76530SGrygorii Strashko int ret; 23293a76530SGrygorii Strashko 23386e8b070SVignesh Raghavendra if (!common->is_emac_mode) 23486e8b070SVignesh Raghavendra return 0; 23586e8b070SVignesh Raghavendra 2367bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 2377bcffde0SGrygorii Strashko return 0; 2387bcffde0SGrygorii Strashko 23993a76530SGrygorii Strashko ret = pm_runtime_get_sync(common->dev); 24093a76530SGrygorii Strashko if (ret < 0) { 24193a76530SGrygorii Strashko pm_runtime_put_noidle(common->dev); 24293a76530SGrygorii Strashko return ret; 24393a76530SGrygorii Strashko } 24493a76530SGrygorii Strashko 24593a76530SGrygorii Strashko port_mask = BIT(port->port_id) | ALE_PORT_HOST; 24693a76530SGrygorii Strashko if (!vid) 24793a76530SGrygorii Strashko unreg_mcast = port_mask; 24893a76530SGrygorii Strashko dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid); 2492d64a034SGrygorii Strashko ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask, 25093a76530SGrygorii Strashko unreg_mcast, port_mask, 0); 25193a76530SGrygorii Strashko 25293a76530SGrygorii Strashko pm_runtime_put(common->dev); 25393a76530SGrygorii Strashko return ret; 25493a76530SGrygorii Strashko } 25593a76530SGrygorii Strashko 25693a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev, 25793a76530SGrygorii Strashko __be16 proto, u16 vid) 25893a76530SGrygorii Strashko { 25993a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 2602d64a034SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 26193a76530SGrygorii Strashko int ret; 26293a76530SGrygorii Strashko 26386e8b070SVignesh Raghavendra if (!common->is_emac_mode) 26486e8b070SVignesh Raghavendra return 0; 26586e8b070SVignesh Raghavendra 2667bcffde0SGrygorii Strashko if (!netif_running(ndev) || !vid) 2677bcffde0SGrygorii Strashko return 0; 2687bcffde0SGrygorii Strashko 26993a76530SGrygorii Strashko ret = pm_runtime_get_sync(common->dev); 27093a76530SGrygorii Strashko if (ret < 0) { 27193a76530SGrygorii Strashko pm_runtime_put_noidle(common->dev); 27293a76530SGrygorii Strashko return ret; 27393a76530SGrygorii Strashko } 27493a76530SGrygorii Strashko 27593a76530SGrygorii Strashko dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid); 2762d64a034SGrygorii Strashko ret = cpsw_ale_del_vlan(common->ale, vid, 2772d64a034SGrygorii Strashko BIT(port->port_id) | ALE_PORT_HOST); 27893a76530SGrygorii Strashko 27993a76530SGrygorii Strashko pm_runtime_put(common->dev); 28093a76530SGrygorii Strashko return ret; 28193a76530SGrygorii Strashko } 28293a76530SGrygorii Strashko 28384b4aa49SGrygorii Strashko static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port, 28493a76530SGrygorii Strashko bool promisc) 28593a76530SGrygorii Strashko { 28693a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 28793a76530SGrygorii Strashko 28886e8b070SVignesh Raghavendra if (promisc && !common->is_emac_mode) { 28986e8b070SVignesh Raghavendra dev_dbg(common->dev, "promisc mode requested in switch mode"); 29086e8b070SVignesh Raghavendra return; 29186e8b070SVignesh Raghavendra } 29286e8b070SVignesh Raghavendra 29393a76530SGrygorii Strashko if (promisc) { 29493a76530SGrygorii Strashko /* Enable promiscuous mode */ 29593a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 29693a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 1); 29793a76530SGrygorii Strashko dev_dbg(common->dev, "promisc enabled\n"); 29893a76530SGrygorii Strashko } else { 29993a76530SGrygorii Strashko /* Disable promiscuous mode */ 30093a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 30193a76530SGrygorii Strashko ALE_PORT_MACONLY_CAF, 0); 30293a76530SGrygorii Strashko dev_dbg(common->dev, "promisc disabled\n"); 30393a76530SGrygorii Strashko } 30493a76530SGrygorii Strashko } 30593a76530SGrygorii Strashko 30693a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev) 30793a76530SGrygorii Strashko { 30893a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 30993a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 31093a76530SGrygorii Strashko u32 port_mask; 31193a76530SGrygorii Strashko bool promisc; 31293a76530SGrygorii Strashko 31393a76530SGrygorii Strashko promisc = !!(ndev->flags & IFF_PROMISC); 31484b4aa49SGrygorii Strashko am65_cpsw_slave_set_promisc(port, promisc); 31593a76530SGrygorii Strashko 31693a76530SGrygorii Strashko if (promisc) 31793a76530SGrygorii Strashko return; 31893a76530SGrygorii Strashko 31993a76530SGrygorii Strashko /* Restore allmulti on vlans if necessary */ 32093a76530SGrygorii Strashko cpsw_ale_set_allmulti(common->ale, 32193a76530SGrygorii Strashko ndev->flags & IFF_ALLMULTI, port->port_id); 32293a76530SGrygorii Strashko 32393a76530SGrygorii Strashko port_mask = ALE_PORT_HOST; 32493a76530SGrygorii Strashko /* Clear all mcast from ALE */ 32593a76530SGrygorii Strashko cpsw_ale_flush_multicast(common->ale, port_mask, -1); 32693a76530SGrygorii Strashko 32793a76530SGrygorii Strashko if (!netdev_mc_empty(ndev)) { 32893a76530SGrygorii Strashko struct netdev_hw_addr *ha; 32993a76530SGrygorii Strashko 33093a76530SGrygorii Strashko /* program multicast address list into ALE register */ 33193a76530SGrygorii Strashko netdev_for_each_mc_addr(ha, ndev) { 33293a76530SGrygorii Strashko cpsw_ale_add_mcast(common->ale, ha->addr, 33393a76530SGrygorii Strashko port_mask, 0, 0, 0); 33493a76530SGrygorii Strashko } 33593a76530SGrygorii Strashko } 33693a76530SGrygorii Strashko } 33793a76530SGrygorii Strashko 33893a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev, 33993a76530SGrygorii Strashko unsigned int txqueue) 34093a76530SGrygorii Strashko { 34193a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 34293a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 34393a76530SGrygorii Strashko struct netdev_queue *netif_txq; 34493a76530SGrygorii Strashko unsigned long trans_start; 34593a76530SGrygorii Strashko 34693a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, txqueue); 34793a76530SGrygorii Strashko tx_chn = &common->tx_chns[txqueue]; 34893a76530SGrygorii Strashko trans_start = netif_txq->trans_start; 34993a76530SGrygorii Strashko 35093a76530SGrygorii Strashko netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n", 35193a76530SGrygorii Strashko txqueue, 35293a76530SGrygorii Strashko netif_tx_queue_stopped(netif_txq), 35393a76530SGrygorii Strashko jiffies_to_msecs(jiffies - trans_start), 35493a76530SGrygorii Strashko dql_avail(&netif_txq->dql), 35593a76530SGrygorii Strashko k3_cppi_desc_pool_avail(tx_chn->desc_pool)); 35693a76530SGrygorii Strashko 35793a76530SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 35893a76530SGrygorii Strashko /* try recover if stopped by us */ 35993a76530SGrygorii Strashko txq_trans_update(netif_txq); 36093a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 36193a76530SGrygorii Strashko } 36293a76530SGrygorii Strashko } 36393a76530SGrygorii Strashko 36493a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common, 36593a76530SGrygorii Strashko struct sk_buff *skb) 36693a76530SGrygorii Strashko { 36793a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 36893a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 36993a76530SGrygorii Strashko struct device *dev = common->dev; 37093a76530SGrygorii Strashko u32 pkt_len = skb_tailroom(skb); 37193a76530SGrygorii Strashko dma_addr_t desc_dma; 37293a76530SGrygorii Strashko dma_addr_t buf_dma; 37393a76530SGrygorii Strashko void *swdata; 37493a76530SGrygorii Strashko 37593a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool); 37693a76530SGrygorii Strashko if (!desc_rx) { 37793a76530SGrygorii Strashko dev_err(dev, "Failed to allocate RXFDQ descriptor\n"); 37893a76530SGrygorii Strashko return -ENOMEM; 37993a76530SGrygorii Strashko } 38093a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx); 38193a76530SGrygorii Strashko 382ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len, 383ed569ed9SPeter Ujfalusi DMA_FROM_DEVICE); 384ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) { 38593a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 38693a76530SGrygorii Strashko dev_err(dev, "Failed to map rx skb buffer\n"); 38793a76530SGrygorii Strashko return -EINVAL; 38893a76530SGrygorii Strashko } 38993a76530SGrygorii Strashko 39093a76530SGrygorii Strashko cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT, 39193a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 39239fd0547SPeter Ujfalusi k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma); 393a9e60cf0SGrygorii Strashko cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb)); 39493a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 39593a76530SGrygorii Strashko *((void **)swdata) = skb; 39693a76530SGrygorii Strashko 39793a76530SGrygorii Strashko return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma); 39893a76530SGrygorii Strashko } 39993a76530SGrygorii Strashko 40093a76530SGrygorii Strashko void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common) 40193a76530SGrygorii Strashko { 40293a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 40393a76530SGrygorii Strashko u32 val, pri_map; 40493a76530SGrygorii Strashko 40593a76530SGrygorii Strashko /* P0 set Receive Priority Type */ 40693a76530SGrygorii Strashko val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 40793a76530SGrygorii Strashko 40893a76530SGrygorii Strashko if (common->pf_p0_rx_ptype_rrobin) { 40993a76530SGrygorii Strashko val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 41093a76530SGrygorii Strashko /* Enet Ports fifos works in fixed priority mode only, so 41193a76530SGrygorii Strashko * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0 41293a76530SGrygorii Strashko */ 41393a76530SGrygorii Strashko pri_map = 0x0; 41493a76530SGrygorii Strashko } else { 41593a76530SGrygorii Strashko val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 41693a76530SGrygorii Strashko /* restore P0_Rx_Pri_Map */ 41793a76530SGrygorii Strashko pri_map = 0x76543210; 41893a76530SGrygorii Strashko } 41993a76530SGrygorii Strashko 42093a76530SGrygorii Strashko writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP); 42193a76530SGrygorii Strashko writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 42293a76530SGrygorii Strashko } 42393a76530SGrygorii Strashko 42458356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common); 42558356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common); 42658356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port); 42758356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port); 42858356eb3SVignesh Raghavendra 42993a76530SGrygorii Strashko static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common, 43093a76530SGrygorii Strashko netdev_features_t features) 43193a76530SGrygorii Strashko { 43293a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 43393a76530SGrygorii Strashko int port_idx, i, ret; 43493a76530SGrygorii Strashko struct sk_buff *skb; 43593a76530SGrygorii Strashko u32 val, port_mask; 43693a76530SGrygorii Strashko 43793a76530SGrygorii Strashko if (common->usage_count) 43893a76530SGrygorii Strashko return 0; 43993a76530SGrygorii Strashko 44093a76530SGrygorii Strashko /* Control register */ 44193a76530SGrygorii Strashko writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE | 44293a76530SGrygorii Strashko AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD, 44393a76530SGrygorii Strashko common->cpsw_base + AM65_CPSW_REG_CTL); 44493a76530SGrygorii Strashko /* Max length register */ 44593a76530SGrygorii Strashko writel(AM65_CPSW_MAX_PACKET_SIZE, 44693a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 44793a76530SGrygorii Strashko /* set base flow_id */ 44893a76530SGrygorii Strashko writel(common->rx_flow_id_base, 44993a76530SGrygorii Strashko host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET); 45093a76530SGrygorii Strashko /* en tx crc offload */ 45197067aafSGrygorii Strashko writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN, host_p->port_base + AM65_CPSW_P0_REG_CTL); 45293a76530SGrygorii Strashko 45393a76530SGrygorii Strashko am65_cpsw_nuss_set_p0_ptype(common); 45493a76530SGrygorii Strashko 45593a76530SGrygorii Strashko /* enable statistic */ 45693a76530SGrygorii Strashko val = BIT(HOST_PORT_NUM); 45793a76530SGrygorii Strashko for (port_idx = 0; port_idx < common->port_num; port_idx++) { 45893a76530SGrygorii Strashko struct am65_cpsw_port *port = &common->ports[port_idx]; 45993a76530SGrygorii Strashko 46093a76530SGrygorii Strashko if (!port->disabled) 46193a76530SGrygorii Strashko val |= BIT(port->port_id); 46293a76530SGrygorii Strashko } 46393a76530SGrygorii Strashko writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 46493a76530SGrygorii Strashko 46593a76530SGrygorii Strashko /* disable priority elevation */ 46693a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE); 46793a76530SGrygorii Strashko 46893a76530SGrygorii Strashko cpsw_ale_start(common->ale); 46993a76530SGrygorii Strashko 47093a76530SGrygorii Strashko /* limit to one RX flow only */ 47193a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 47293a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ID, 0); 47393a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 47493a76530SGrygorii Strashko ALE_DEFAULT_THREAD_ENABLE, 1); 47593a76530SGrygorii Strashko /* switch to vlan unaware mode */ 47693a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1); 47793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 47893a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 47993a76530SGrygorii Strashko 48093a76530SGrygorii Strashko /* default vlan cfg: create mask based on enabled ports */ 48193a76530SGrygorii Strashko port_mask = GENMASK(common->port_num, 0) & 48293a76530SGrygorii Strashko ~common->disabled_ports_mask; 48393a76530SGrygorii Strashko 48493a76530SGrygorii Strashko cpsw_ale_add_vlan(common->ale, 0, port_mask, 48593a76530SGrygorii Strashko port_mask, port_mask, 48693a76530SGrygorii Strashko port_mask & ~ALE_PORT_HOST); 48793a76530SGrygorii Strashko 48858356eb3SVignesh Raghavendra if (common->is_emac_mode) 48958356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(common); 49058356eb3SVignesh Raghavendra else 49158356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(common); 49258356eb3SVignesh Raghavendra 49393a76530SGrygorii Strashko for (i = 0; i < common->rx_chns.descs_num; i++) { 49493a76530SGrygorii Strashko skb = __netdev_alloc_skb_ip_align(NULL, 49593a76530SGrygorii Strashko AM65_CPSW_MAX_PACKET_SIZE, 49693a76530SGrygorii Strashko GFP_KERNEL); 49793a76530SGrygorii Strashko if (!skb) { 49893a76530SGrygorii Strashko dev_err(common->dev, "cannot allocate skb\n"); 49993a76530SGrygorii Strashko return -ENOMEM; 50093a76530SGrygorii Strashko } 50193a76530SGrygorii Strashko 50293a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, skb); 50393a76530SGrygorii Strashko if (ret < 0) { 50493a76530SGrygorii Strashko dev_err(common->dev, 50593a76530SGrygorii Strashko "cannot submit skb to channel rx, error %d\n", 50693a76530SGrygorii Strashko ret); 50793a76530SGrygorii Strashko kfree_skb(skb); 50893a76530SGrygorii Strashko return ret; 50993a76530SGrygorii Strashko } 51093a76530SGrygorii Strashko kmemleak_not_leak(skb); 51193a76530SGrygorii Strashko } 51293a76530SGrygorii Strashko k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn); 51393a76530SGrygorii Strashko 51493a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 51593a76530SGrygorii Strashko ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn); 51693a76530SGrygorii Strashko if (ret) 51793a76530SGrygorii Strashko return ret; 51893a76530SGrygorii Strashko napi_enable(&common->tx_chns[i].napi_tx); 51993a76530SGrygorii Strashko } 52093a76530SGrygorii Strashko 52193a76530SGrygorii Strashko napi_enable(&common->napi_rx); 52293a76530SGrygorii Strashko 52393a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss started\n"); 52493a76530SGrygorii Strashko return 0; 52593a76530SGrygorii Strashko } 52693a76530SGrygorii Strashko 52793a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma); 52893a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma); 52993a76530SGrygorii Strashko 53093a76530SGrygorii Strashko static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common) 53193a76530SGrygorii Strashko { 53293a76530SGrygorii Strashko int i; 53393a76530SGrygorii Strashko 53493a76530SGrygorii Strashko if (common->usage_count != 1) 53593a76530SGrygorii Strashko return 0; 53693a76530SGrygorii Strashko 53793a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 53893a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 53993a76530SGrygorii Strashko 54093a76530SGrygorii Strashko /* shutdown tx channels */ 54193a76530SGrygorii Strashko atomic_set(&common->tdown_cnt, common->tx_ch_num); 54293a76530SGrygorii Strashko /* ensure new tdown_cnt value is visible */ 54393a76530SGrygorii Strashko smp_mb__after_atomic(); 54493a76530SGrygorii Strashko reinit_completion(&common->tdown_complete); 54593a76530SGrygorii Strashko 54693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 54793a76530SGrygorii Strashko k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false); 54893a76530SGrygorii Strashko 54993a76530SGrygorii Strashko i = wait_for_completion_timeout(&common->tdown_complete, 55093a76530SGrygorii Strashko msecs_to_jiffies(1000)); 55193a76530SGrygorii Strashko if (!i) 55293a76530SGrygorii Strashko dev_err(common->dev, "tx timeout\n"); 55393a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 55493a76530SGrygorii Strashko napi_disable(&common->tx_chns[i].napi_tx); 55593a76530SGrygorii Strashko 55693a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 55793a76530SGrygorii Strashko k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn, 55893a76530SGrygorii Strashko &common->tx_chns[i], 55993a76530SGrygorii Strashko am65_cpsw_nuss_tx_cleanup); 56093a76530SGrygorii Strashko k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn); 56193a76530SGrygorii Strashko } 56293a76530SGrygorii Strashko 56393a76530SGrygorii Strashko k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true); 56493a76530SGrygorii Strashko napi_disable(&common->napi_rx); 56593a76530SGrygorii Strashko 56693a76530SGrygorii Strashko for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++) 56793a76530SGrygorii Strashko k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i, 56893a76530SGrygorii Strashko &common->rx_chns, 56993a76530SGrygorii Strashko am65_cpsw_nuss_rx_cleanup, !!i); 57093a76530SGrygorii Strashko 57193a76530SGrygorii Strashko k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn); 57293a76530SGrygorii Strashko 57393a76530SGrygorii Strashko cpsw_ale_stop(common->ale); 57493a76530SGrygorii Strashko 57593a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_CTL); 57693a76530SGrygorii Strashko writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 57793a76530SGrygorii Strashko 57893a76530SGrygorii Strashko dev_dbg(common->dev, "cpsw_nuss stopped\n"); 57993a76530SGrygorii Strashko return 0; 58093a76530SGrygorii Strashko } 58193a76530SGrygorii Strashko 58293a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev) 58393a76530SGrygorii Strashko { 58493a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 58593a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 58693a76530SGrygorii Strashko int ret; 58793a76530SGrygorii Strashko 58893a76530SGrygorii Strashko if (port->slave.phy) 58993a76530SGrygorii Strashko phy_stop(port->slave.phy); 59093a76530SGrygorii Strashko 59193a76530SGrygorii Strashko netif_tx_stop_all_queues(ndev); 59293a76530SGrygorii Strashko 59393a76530SGrygorii Strashko if (port->slave.phy) { 59493a76530SGrygorii Strashko phy_disconnect(port->slave.phy); 59593a76530SGrygorii Strashko port->slave.phy = NULL; 59693a76530SGrygorii Strashko } 59793a76530SGrygorii Strashko 59893a76530SGrygorii Strashko ret = am65_cpsw_nuss_common_stop(common); 59993a76530SGrygorii Strashko if (ret) 60093a76530SGrygorii Strashko return ret; 60193a76530SGrygorii Strashko 60293a76530SGrygorii Strashko common->usage_count--; 60393a76530SGrygorii Strashko pm_runtime_put(common->dev); 60493a76530SGrygorii Strashko return 0; 60593a76530SGrygorii Strashko } 60693a76530SGrygorii Strashko 6077bcffde0SGrygorii Strashko static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) 6087bcffde0SGrygorii Strashko { 6097bcffde0SGrygorii Strashko struct am65_cpsw_port *port = arg; 6107bcffde0SGrygorii Strashko 6117bcffde0SGrygorii Strashko if (!vdev) 6127bcffde0SGrygorii Strashko return 0; 6137bcffde0SGrygorii Strashko 6147bcffde0SGrygorii Strashko return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid); 6157bcffde0SGrygorii Strashko } 6167bcffde0SGrygorii Strashko 61793a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev) 61893a76530SGrygorii Strashko { 61993a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 62093a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 62193a76530SGrygorii Strashko int ret, i; 62293a76530SGrygorii Strashko 62393a76530SGrygorii Strashko ret = pm_runtime_get_sync(common->dev); 62493a76530SGrygorii Strashko if (ret < 0) { 62593a76530SGrygorii Strashko pm_runtime_put_noidle(common->dev); 62693a76530SGrygorii Strashko return ret; 62793a76530SGrygorii Strashko } 62893a76530SGrygorii Strashko 62993a76530SGrygorii Strashko /* Notify the stack of the actual queue counts. */ 63093a76530SGrygorii Strashko ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num); 63193a76530SGrygorii Strashko if (ret) { 63293a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of tx queues\n"); 63393a76530SGrygorii Strashko return ret; 63493a76530SGrygorii Strashko } 63593a76530SGrygorii Strashko 63693a76530SGrygorii Strashko ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES); 63793a76530SGrygorii Strashko if (ret) { 63893a76530SGrygorii Strashko dev_err(common->dev, "cannot set real number of rx queues\n"); 63993a76530SGrygorii Strashko return ret; 64093a76530SGrygorii Strashko } 64193a76530SGrygorii Strashko 64293a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) 64393a76530SGrygorii Strashko netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i)); 64493a76530SGrygorii Strashko 64593a76530SGrygorii Strashko ret = am65_cpsw_nuss_common_open(common, ndev->features); 64693a76530SGrygorii Strashko if (ret) 64793a76530SGrygorii Strashko return ret; 64893a76530SGrygorii Strashko 64993a76530SGrygorii Strashko common->usage_count++; 65093a76530SGrygorii Strashko 65193a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, ndev->dev_addr); 65293a76530SGrygorii Strashko 65358356eb3SVignesh Raghavendra if (common->is_emac_mode) 65458356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 65558356eb3SVignesh Raghavendra else 65658356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 65793a76530SGrygorii Strashko 65893a76530SGrygorii Strashko /* mac_sl should be configured via phy-link interface */ 65993a76530SGrygorii Strashko am65_cpsw_sl_ctl_reset(port); 66093a76530SGrygorii Strashko 66193a76530SGrygorii Strashko ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, 66293a76530SGrygorii Strashko port->slave.phy_if); 66393a76530SGrygorii Strashko if (ret) 66493a76530SGrygorii Strashko goto error_cleanup; 66593a76530SGrygorii Strashko 66693a76530SGrygorii Strashko if (port->slave.phy_node) { 66793a76530SGrygorii Strashko port->slave.phy = of_phy_connect(ndev, 66893a76530SGrygorii Strashko port->slave.phy_node, 66993a76530SGrygorii Strashko &am65_cpsw_nuss_adjust_link, 67093a76530SGrygorii Strashko 0, port->slave.phy_if); 67193a76530SGrygorii Strashko if (!port->slave.phy) { 67293a76530SGrygorii Strashko dev_err(common->dev, "phy %pOF not found on slave %d\n", 67393a76530SGrygorii Strashko port->slave.phy_node, 67493a76530SGrygorii Strashko port->port_id); 67593a76530SGrygorii Strashko ret = -ENODEV; 67693a76530SGrygorii Strashko goto error_cleanup; 67793a76530SGrygorii Strashko } 67893a76530SGrygorii Strashko } 67993a76530SGrygorii Strashko 6807bcffde0SGrygorii Strashko /* restore vlan configurations */ 6817bcffde0SGrygorii Strashko vlan_for_each(ndev, cpsw_restore_vlans, port); 6827bcffde0SGrygorii Strashko 68393a76530SGrygorii Strashko phy_attached_info(port->slave.phy); 68493a76530SGrygorii Strashko phy_start(port->slave.phy); 68593a76530SGrygorii Strashko 68693a76530SGrygorii Strashko return 0; 68793a76530SGrygorii Strashko 68893a76530SGrygorii Strashko error_cleanup: 68993a76530SGrygorii Strashko am65_cpsw_nuss_ndo_slave_stop(ndev); 69093a76530SGrygorii Strashko return ret; 69193a76530SGrygorii Strashko } 69293a76530SGrygorii Strashko 69393a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma) 69493a76530SGrygorii Strashko { 69593a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = data; 69693a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 69793a76530SGrygorii Strashko struct sk_buff *skb; 69893a76530SGrygorii Strashko dma_addr_t buf_dma; 69993a76530SGrygorii Strashko u32 buf_dma_len; 70093a76530SGrygorii Strashko void **swdata; 70193a76530SGrygorii Strashko 70293a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 70393a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 70493a76530SGrygorii Strashko skb = *swdata; 70593a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 70639fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 70793a76530SGrygorii Strashko 708ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 70993a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 71093a76530SGrygorii Strashko 71193a76530SGrygorii Strashko dev_kfree_skb_any(skb); 71293a76530SGrygorii Strashko } 71393a76530SGrygorii Strashko 714b1f66a5bSGrygorii Strashko static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata) 715b1f66a5bSGrygorii Strashko { 716b1f66a5bSGrygorii Strashko struct skb_shared_hwtstamps *ssh; 717b1f66a5bSGrygorii Strashko u64 ns; 718b1f66a5bSGrygorii Strashko 719b1f66a5bSGrygorii Strashko ns = ((u64)psdata[1] << 32) | psdata[0]; 720b1f66a5bSGrygorii Strashko 721b1f66a5bSGrygorii Strashko ssh = skb_hwtstamps(skb); 722b1f66a5bSGrygorii Strashko memset(ssh, 0, sizeof(*ssh)); 723b1f66a5bSGrygorii Strashko ssh->hwtstamp = ns_to_ktime(ns); 724b1f66a5bSGrygorii Strashko } 725b1f66a5bSGrygorii Strashko 72693a76530SGrygorii Strashko /* RX psdata[2] word format - checksum information */ 72793a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0) 72893a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16) 72993a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17) 73093a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IS_TCP BIT(18) 73193a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19) 73293a76530SGrygorii Strashko #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20) 73393a76530SGrygorii Strashko 73493a76530SGrygorii Strashko static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info) 73593a76530SGrygorii Strashko { 73693a76530SGrygorii Strashko /* HW can verify IPv4/IPv6 TCP/UDP packets checksum 73793a76530SGrygorii Strashko * csum information provides in psdata[2] word: 73893a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error 73993a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID 74093a76530SGrygorii Strashko * bits - indicates IPv4/IPv6 packet 74193a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet 74293a76530SGrygorii Strashko * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets 74393a76530SGrygorii Strashko * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR 74493a76530SGrygorii Strashko */ 74593a76530SGrygorii Strashko skb_checksum_none_assert(skb); 74693a76530SGrygorii Strashko 74793a76530SGrygorii Strashko if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) 74893a76530SGrygorii Strashko return; 74993a76530SGrygorii Strashko 75093a76530SGrygorii Strashko if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID | 75193a76530SGrygorii Strashko AM65_CPSW_RX_PSD_IPV4_VALID)) && 75293a76530SGrygorii Strashko !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) { 75393a76530SGrygorii Strashko /* csum for fragmented packets is unsupported */ 75493a76530SGrygorii Strashko if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT)) 75593a76530SGrygorii Strashko skb->ip_summed = CHECKSUM_UNNECESSARY; 75693a76530SGrygorii Strashko } 75793a76530SGrygorii Strashko } 75893a76530SGrygorii Strashko 75993a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common, 76093a76530SGrygorii Strashko u32 flow_idx) 76193a76530SGrygorii Strashko { 76293a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 76393a76530SGrygorii Strashko u32 buf_dma_len, pkt_len, port_id = 0, csum_info; 76493a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 76593a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 76693a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_rx; 76793a76530SGrygorii Strashko struct device *dev = common->dev; 76893a76530SGrygorii Strashko struct sk_buff *skb, *new_skb; 76993a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 77093a76530SGrygorii Strashko struct am65_cpsw_port *port; 77193a76530SGrygorii Strashko struct net_device *ndev; 77293a76530SGrygorii Strashko void **swdata; 77393a76530SGrygorii Strashko u32 *psdata; 77493a76530SGrygorii Strashko int ret = 0; 77593a76530SGrygorii Strashko 77693a76530SGrygorii Strashko ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma); 77793a76530SGrygorii Strashko if (ret) { 77893a76530SGrygorii Strashko if (ret != -ENODATA) 77993a76530SGrygorii Strashko dev_err(dev, "RX: pop chn fail %d\n", ret); 78093a76530SGrygorii Strashko return ret; 78193a76530SGrygorii Strashko } 78293a76530SGrygorii Strashko 7836a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 78493a76530SGrygorii Strashko dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx); 78593a76530SGrygorii Strashko return 0; 78693a76530SGrygorii Strashko } 78793a76530SGrygorii Strashko 78893a76530SGrygorii Strashko desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 78993a76530SGrygorii Strashko dev_dbg(dev, "%s flow_idx: %u desc %pad\n", 79093a76530SGrygorii Strashko __func__, flow_idx, &desc_dma); 79193a76530SGrygorii Strashko 79293a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_rx); 79393a76530SGrygorii Strashko skb = *swdata; 79493a76530SGrygorii Strashko cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 79539fd0547SPeter Ujfalusi k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 79693a76530SGrygorii Strashko pkt_len = cppi5_hdesc_get_pktlen(desc_rx); 79793a76530SGrygorii Strashko cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL); 79893a76530SGrygorii Strashko dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id); 79993a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 80093a76530SGrygorii Strashko ndev = port->ndev; 80193a76530SGrygorii Strashko skb->dev = ndev; 80293a76530SGrygorii Strashko 80393a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(desc_rx); 804b1f66a5bSGrygorii Strashko /* add RX timestamp */ 805b1f66a5bSGrygorii Strashko if (port->rx_ts_enabled) 806b1f66a5bSGrygorii Strashko am65_cpsw_nuss_rx_ts(skb, psdata); 80793a76530SGrygorii Strashko csum_info = psdata[2]; 80893a76530SGrygorii Strashko dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info); 80993a76530SGrygorii Strashko 810ed569ed9SPeter Ujfalusi dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 81193a76530SGrygorii Strashko 81293a76530SGrygorii Strashko k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 81393a76530SGrygorii Strashko 81493a76530SGrygorii Strashko new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE); 81593a76530SGrygorii Strashko if (new_skb) { 81686e8b070SVignesh Raghavendra ndev_priv = netdev_priv(ndev); 81786e8b070SVignesh Raghavendra am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark); 81893a76530SGrygorii Strashko skb_put(skb, pkt_len); 81993a76530SGrygorii Strashko skb->protocol = eth_type_trans(skb, ndev); 82093a76530SGrygorii Strashko am65_cpsw_nuss_rx_csum(skb, csum_info); 82193a76530SGrygorii Strashko napi_gro_receive(&common->napi_rx, skb); 82293a76530SGrygorii Strashko 82393a76530SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 82493a76530SGrygorii Strashko 82593a76530SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 82693a76530SGrygorii Strashko stats->rx_packets++; 82793a76530SGrygorii Strashko stats->rx_bytes += pkt_len; 82893a76530SGrygorii Strashko u64_stats_update_end(&stats->syncp); 82993a76530SGrygorii Strashko kmemleak_not_leak(new_skb); 83093a76530SGrygorii Strashko } else { 83193a76530SGrygorii Strashko ndev->stats.rx_dropped++; 83293a76530SGrygorii Strashko new_skb = skb; 83393a76530SGrygorii Strashko } 83493a76530SGrygorii Strashko 83593a76530SGrygorii Strashko if (netif_dormant(ndev)) { 83693a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 83793a76530SGrygorii Strashko ndev->stats.rx_dropped++; 83893a76530SGrygorii Strashko return 0; 83993a76530SGrygorii Strashko } 84093a76530SGrygorii Strashko 84193a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_push(common, new_skb); 84293a76530SGrygorii Strashko if (WARN_ON(ret < 0)) { 84393a76530SGrygorii Strashko dev_kfree_skb_any(new_skb); 84493a76530SGrygorii Strashko ndev->stats.rx_errors++; 84593a76530SGrygorii Strashko ndev->stats.rx_dropped++; 84693a76530SGrygorii Strashko } 84793a76530SGrygorii Strashko 84893a76530SGrygorii Strashko return ret; 84993a76530SGrygorii Strashko } 85093a76530SGrygorii Strashko 85193a76530SGrygorii Strashko static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget) 85293a76530SGrygorii Strashko { 85393a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx); 85493a76530SGrygorii Strashko int flow = AM65_CPSW_MAX_RX_FLOWS; 85593a76530SGrygorii Strashko int cur_budget, ret; 85693a76530SGrygorii Strashko int num_rx = 0; 85793a76530SGrygorii Strashko 85893a76530SGrygorii Strashko /* process every flow */ 85993a76530SGrygorii Strashko while (flow--) { 86093a76530SGrygorii Strashko cur_budget = budget - num_rx; 86193a76530SGrygorii Strashko 86293a76530SGrygorii Strashko while (cur_budget--) { 86393a76530SGrygorii Strashko ret = am65_cpsw_nuss_rx_packets(common, flow); 86493a76530SGrygorii Strashko if (ret) 86593a76530SGrygorii Strashko break; 86693a76530SGrygorii Strashko num_rx++; 86793a76530SGrygorii Strashko } 86893a76530SGrygorii Strashko 86993a76530SGrygorii Strashko if (num_rx >= budget) 87093a76530SGrygorii Strashko break; 87193a76530SGrygorii Strashko } 87293a76530SGrygorii Strashko 87393a76530SGrygorii Strashko dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget); 87493a76530SGrygorii Strashko 87593a76530SGrygorii Strashko if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) 87693a76530SGrygorii Strashko enable_irq(common->rx_chns.irq); 87793a76530SGrygorii Strashko 87893a76530SGrygorii Strashko return num_rx; 87993a76530SGrygorii Strashko } 88093a76530SGrygorii Strashko 88193a76530SGrygorii Strashko static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn, 88293a76530SGrygorii Strashko struct cppi5_host_desc_t *desc) 88393a76530SGrygorii Strashko { 88493a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc; 88593a76530SGrygorii Strashko dma_addr_t buf_dma, next_desc_dma; 88693a76530SGrygorii Strashko u32 buf_dma_len; 88793a76530SGrygorii Strashko 88893a76530SGrygorii Strashko first_desc = desc; 88993a76530SGrygorii Strashko next_desc = first_desc; 89093a76530SGrygorii Strashko 89193a76530SGrygorii Strashko cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len); 89239fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 89393a76530SGrygorii Strashko 894ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE); 89593a76530SGrygorii Strashko 89693a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc); 89739fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 89893a76530SGrygorii Strashko while (next_desc_dma) { 89993a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 90093a76530SGrygorii Strashko next_desc_dma); 90193a76530SGrygorii Strashko cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len); 90239fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 90393a76530SGrygorii Strashko 904ed569ed9SPeter Ujfalusi dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len, 90593a76530SGrygorii Strashko DMA_TO_DEVICE); 90693a76530SGrygorii Strashko 90793a76530SGrygorii Strashko next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc); 90839fd0547SPeter Ujfalusi k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 90993a76530SGrygorii Strashko 91093a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 91193a76530SGrygorii Strashko } 91293a76530SGrygorii Strashko 91393a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc); 91493a76530SGrygorii Strashko } 91593a76530SGrygorii Strashko 91693a76530SGrygorii Strashko static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma) 91793a76530SGrygorii Strashko { 91893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = data; 91993a76530SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 92093a76530SGrygorii Strashko struct sk_buff *skb; 92193a76530SGrygorii Strashko void **swdata; 92293a76530SGrygorii Strashko 92393a76530SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma); 92493a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 92593a76530SGrygorii Strashko skb = *(swdata); 926ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 92793a76530SGrygorii Strashko 92893a76530SGrygorii Strashko dev_kfree_skb_any(skb); 92993a76530SGrygorii Strashko } 93093a76530SGrygorii Strashko 931a9e60cf0SGrygorii Strashko static struct sk_buff * 932a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn, 933a9e60cf0SGrygorii Strashko dma_addr_t desc_dma) 934a9e60cf0SGrygorii Strashko { 935a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 936a9e60cf0SGrygorii Strashko struct am65_cpsw_ndev_stats *stats; 937a9e60cf0SGrygorii Strashko struct cppi5_host_desc_t *desc_tx; 938a9e60cf0SGrygorii Strashko struct net_device *ndev; 939a9e60cf0SGrygorii Strashko struct sk_buff *skb; 940a9e60cf0SGrygorii Strashko void **swdata; 941a9e60cf0SGrygorii Strashko 942a9e60cf0SGrygorii Strashko desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 943a9e60cf0SGrygorii Strashko desc_dma); 944a9e60cf0SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(desc_tx); 945a9e60cf0SGrygorii Strashko skb = *(swdata); 946ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 947a9e60cf0SGrygorii Strashko 948a9e60cf0SGrygorii Strashko ndev = skb->dev; 949a9e60cf0SGrygorii Strashko 950a9e60cf0SGrygorii Strashko am65_cpts_tx_timestamp(tx_chn->common->cpts, skb); 951a9e60cf0SGrygorii Strashko 952a9e60cf0SGrygorii Strashko ndev_priv = netdev_priv(ndev); 953a9e60cf0SGrygorii Strashko stats = this_cpu_ptr(ndev_priv->stats); 954a9e60cf0SGrygorii Strashko u64_stats_update_begin(&stats->syncp); 955a9e60cf0SGrygorii Strashko stats->tx_packets++; 956a9e60cf0SGrygorii Strashko stats->tx_bytes += skb->len; 957a9e60cf0SGrygorii Strashko u64_stats_update_end(&stats->syncp); 958a9e60cf0SGrygorii Strashko 959a9e60cf0SGrygorii Strashko return skb; 960a9e60cf0SGrygorii Strashko } 961a9e60cf0SGrygorii Strashko 962a9e60cf0SGrygorii Strashko static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev, 963a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq) 964a9e60cf0SGrygorii Strashko { 965a9e60cf0SGrygorii Strashko if (netif_tx_queue_stopped(netif_txq)) { 966a9e60cf0SGrygorii Strashko /* Check whether the queue is stopped due to stalled 967a9e60cf0SGrygorii Strashko * tx dma, if the queue is stopped then wake the queue 968a9e60cf0SGrygorii Strashko * as we have free desc for tx 969a9e60cf0SGrygorii Strashko */ 970a9e60cf0SGrygorii Strashko __netif_tx_lock(netif_txq, smp_processor_id()); 971a9e60cf0SGrygorii Strashko if (netif_running(ndev) && 972a9e60cf0SGrygorii Strashko (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS)) 973a9e60cf0SGrygorii Strashko netif_tx_wake_queue(netif_txq); 974a9e60cf0SGrygorii Strashko 975a9e60cf0SGrygorii Strashko __netif_tx_unlock(netif_txq); 976a9e60cf0SGrygorii Strashko } 977a9e60cf0SGrygorii Strashko } 978a9e60cf0SGrygorii Strashko 97993a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common, 98093a76530SGrygorii Strashko int chn, unsigned int budget) 98193a76530SGrygorii Strashko { 98293a76530SGrygorii Strashko struct device *dev = common->dev; 98393a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 98493a76530SGrygorii Strashko struct netdev_queue *netif_txq; 98593a76530SGrygorii Strashko unsigned int total_bytes = 0; 98693a76530SGrygorii Strashko struct net_device *ndev; 98793a76530SGrygorii Strashko struct sk_buff *skb; 98893a76530SGrygorii Strashko dma_addr_t desc_dma; 98993a76530SGrygorii Strashko int res, num_tx = 0; 99093a76530SGrygorii Strashko 99193a76530SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 99293a76530SGrygorii Strashko 99393a76530SGrygorii Strashko while (true) { 994a9e60cf0SGrygorii Strashko spin_lock(&tx_chn->lock); 995a9e60cf0SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 996a9e60cf0SGrygorii Strashko spin_unlock(&tx_chn->lock); 997a9e60cf0SGrygorii Strashko if (res == -ENODATA) 998a9e60cf0SGrygorii Strashko break; 99993a76530SGrygorii Strashko 1000a9e60cf0SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 1001a9e60cf0SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 1002a9e60cf0SGrygorii Strashko complete(&common->tdown_complete); 1003a9e60cf0SGrygorii Strashko break; 1004a9e60cf0SGrygorii Strashko } 1005a9e60cf0SGrygorii Strashko 1006a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 1007a9e60cf0SGrygorii Strashko total_bytes = skb->len; 1008a9e60cf0SGrygorii Strashko ndev = skb->dev; 1009a9e60cf0SGrygorii Strashko napi_consume_skb(skb, budget); 1010a9e60cf0SGrygorii Strashko num_tx++; 1011a9e60cf0SGrygorii Strashko 1012a9e60cf0SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 1013a9e60cf0SGrygorii Strashko 1014a9e60cf0SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 1015a9e60cf0SGrygorii Strashko 1016a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 1017a9e60cf0SGrygorii Strashko } 1018a9e60cf0SGrygorii Strashko 1019a9e60cf0SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 1020a9e60cf0SGrygorii Strashko 1021a9e60cf0SGrygorii Strashko return num_tx; 1022a9e60cf0SGrygorii Strashko } 1023a9e60cf0SGrygorii Strashko 1024a9e60cf0SGrygorii Strashko static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common, 1025a9e60cf0SGrygorii Strashko int chn, unsigned int budget) 1026a9e60cf0SGrygorii Strashko { 1027a9e60cf0SGrygorii Strashko struct device *dev = common->dev; 1028a9e60cf0SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 1029a9e60cf0SGrygorii Strashko struct netdev_queue *netif_txq; 1030a9e60cf0SGrygorii Strashko unsigned int total_bytes = 0; 1031a9e60cf0SGrygorii Strashko struct net_device *ndev; 1032a9e60cf0SGrygorii Strashko struct sk_buff *skb; 1033a9e60cf0SGrygorii Strashko dma_addr_t desc_dma; 1034a9e60cf0SGrygorii Strashko int res, num_tx = 0; 1035a9e60cf0SGrygorii Strashko 1036a9e60cf0SGrygorii Strashko tx_chn = &common->tx_chns[chn]; 1037a9e60cf0SGrygorii Strashko 1038a9e60cf0SGrygorii Strashko while (true) { 103993a76530SGrygorii Strashko res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 104093a76530SGrygorii Strashko if (res == -ENODATA) 104193a76530SGrygorii Strashko break; 104293a76530SGrygorii Strashko 10436a40e289SGrygorii Strashko if (cppi5_desc_is_tdcm(desc_dma)) { 104493a76530SGrygorii Strashko if (atomic_dec_and_test(&common->tdown_cnt)) 104593a76530SGrygorii Strashko complete(&common->tdown_complete); 104693a76530SGrygorii Strashko break; 104793a76530SGrygorii Strashko } 104893a76530SGrygorii Strashko 1049a9e60cf0SGrygorii Strashko skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 105093a76530SGrygorii Strashko 105193a76530SGrygorii Strashko ndev = skb->dev; 105293a76530SGrygorii Strashko total_bytes += skb->len; 105393a76530SGrygorii Strashko napi_consume_skb(skb, budget); 105493a76530SGrygorii Strashko num_tx++; 105593a76530SGrygorii Strashko } 105693a76530SGrygorii Strashko 105793a76530SGrygorii Strashko if (!num_tx) 105893a76530SGrygorii Strashko return 0; 105993a76530SGrygorii Strashko 106093a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, chn); 106193a76530SGrygorii Strashko 106293a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 106393a76530SGrygorii Strashko 1064a9e60cf0SGrygorii Strashko am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 106593a76530SGrygorii Strashko 106693a76530SGrygorii Strashko dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 106793a76530SGrygorii Strashko 106893a76530SGrygorii Strashko return num_tx; 106993a76530SGrygorii Strashko } 107093a76530SGrygorii Strashko 107193a76530SGrygorii Strashko static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget) 107293a76530SGrygorii Strashko { 107393a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx); 107493a76530SGrygorii Strashko int num_tx; 107593a76530SGrygorii Strashko 1076a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(tx_chn->common)) 1077a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget); 1078a9e60cf0SGrygorii Strashko else 1079a9e60cf0SGrygorii Strashko num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget); 1080a9e60cf0SGrygorii Strashko 108193a76530SGrygorii Strashko num_tx = min(num_tx, budget); 108293a76530SGrygorii Strashko if (num_tx < budget) { 108393a76530SGrygorii Strashko napi_complete(napi_tx); 108493a76530SGrygorii Strashko enable_irq(tx_chn->irq); 108593a76530SGrygorii Strashko } 108693a76530SGrygorii Strashko 108793a76530SGrygorii Strashko return num_tx; 108893a76530SGrygorii Strashko } 108993a76530SGrygorii Strashko 109093a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id) 109193a76530SGrygorii Strashko { 109293a76530SGrygorii Strashko struct am65_cpsw_common *common = dev_id; 109393a76530SGrygorii Strashko 109493a76530SGrygorii Strashko disable_irq_nosync(irq); 109593a76530SGrygorii Strashko napi_schedule(&common->napi_rx); 109693a76530SGrygorii Strashko 109793a76530SGrygorii Strashko return IRQ_HANDLED; 109893a76530SGrygorii Strashko } 109993a76530SGrygorii Strashko 110093a76530SGrygorii Strashko static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id) 110193a76530SGrygorii Strashko { 110293a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = dev_id; 110393a76530SGrygorii Strashko 110493a76530SGrygorii Strashko disable_irq_nosync(irq); 110593a76530SGrygorii Strashko napi_schedule(&tx_chn->napi_tx); 110693a76530SGrygorii Strashko 110793a76530SGrygorii Strashko return IRQ_HANDLED; 110893a76530SGrygorii Strashko } 110993a76530SGrygorii Strashko 111093a76530SGrygorii Strashko static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb, 111193a76530SGrygorii Strashko struct net_device *ndev) 111293a76530SGrygorii Strashko { 111393a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 111493a76530SGrygorii Strashko struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc; 111593a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 111693a76530SGrygorii Strashko struct device *dev = common->dev; 111793a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn; 111893a76530SGrygorii Strashko struct netdev_queue *netif_txq; 111993a76530SGrygorii Strashko dma_addr_t desc_dma, buf_dma; 112093a76530SGrygorii Strashko int ret, q_idx, i; 112193a76530SGrygorii Strashko void **swdata; 112293a76530SGrygorii Strashko u32 *psdata; 112393a76530SGrygorii Strashko u32 pkt_len; 112493a76530SGrygorii Strashko 112593a76530SGrygorii Strashko /* padding enabled in hw */ 112693a76530SGrygorii Strashko pkt_len = skb_headlen(skb); 112793a76530SGrygorii Strashko 1128b1f66a5bSGrygorii Strashko /* SKB TX timestamp */ 1129b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1130b1f66a5bSGrygorii Strashko am65_cpts_prep_tx_timestamp(common->cpts, skb); 1131b1f66a5bSGrygorii Strashko 113293a76530SGrygorii Strashko q_idx = skb_get_queue_mapping(skb); 113393a76530SGrygorii Strashko dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx); 113493a76530SGrygorii Strashko 113593a76530SGrygorii Strashko tx_chn = &common->tx_chns[q_idx]; 113693a76530SGrygorii Strashko netif_txq = netdev_get_tx_queue(ndev, q_idx); 113793a76530SGrygorii Strashko 113893a76530SGrygorii Strashko /* Map the linear buffer */ 1139ed569ed9SPeter Ujfalusi buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len, 114093a76530SGrygorii Strashko DMA_TO_DEVICE); 1141ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 114293a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb buffer\n"); 114393a76530SGrygorii Strashko ndev->stats.tx_errors++; 114493a76530SGrygorii Strashko goto err_free_skb; 114593a76530SGrygorii Strashko } 114693a76530SGrygorii Strashko 114793a76530SGrygorii Strashko first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 114893a76530SGrygorii Strashko if (!first_desc) { 114993a76530SGrygorii Strashko dev_dbg(dev, "Failed to allocate descriptor\n"); 1150ed569ed9SPeter Ujfalusi dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len, 1151ed569ed9SPeter Ujfalusi DMA_TO_DEVICE); 115293a76530SGrygorii Strashko goto busy_stop_q; 115393a76530SGrygorii Strashko } 115493a76530SGrygorii Strashko 115593a76530SGrygorii Strashko cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT, 115693a76530SGrygorii Strashko AM65_CPSW_NAV_PS_DATA_SIZE); 115793a76530SGrygorii Strashko cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF); 115893a76530SGrygorii Strashko cppi5_hdesc_set_pkttype(first_desc, 0x7); 115993a76530SGrygorii Strashko cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id); 116093a76530SGrygorii Strashko 116139fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 116293a76530SGrygorii Strashko cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len); 116393a76530SGrygorii Strashko swdata = cppi5_hdesc_get_swdata(first_desc); 116493a76530SGrygorii Strashko *(swdata) = skb; 116593a76530SGrygorii Strashko psdata = cppi5_hdesc_get_psdata(first_desc); 116693a76530SGrygorii Strashko 116793a76530SGrygorii Strashko /* HW csum offload if enabled */ 116893a76530SGrygorii Strashko psdata[2] = 0; 116993a76530SGrygorii Strashko if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) { 117093a76530SGrygorii Strashko unsigned int cs_start, cs_offset; 117193a76530SGrygorii Strashko 117293a76530SGrygorii Strashko cs_start = skb_transport_offset(skb); 117393a76530SGrygorii Strashko cs_offset = cs_start + skb->csum_offset; 117493a76530SGrygorii Strashko /* HW numerates bytes starting from 1 */ 117593a76530SGrygorii Strashko psdata[2] = ((cs_offset + 1) << 24) | 117693a76530SGrygorii Strashko ((cs_start + 1) << 16) | (skb->len - cs_start); 117793a76530SGrygorii Strashko dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]); 117893a76530SGrygorii Strashko } 117993a76530SGrygorii Strashko 118093a76530SGrygorii Strashko if (!skb_is_nonlinear(skb)) 118193a76530SGrygorii Strashko goto done_tx; 118293a76530SGrygorii Strashko 118393a76530SGrygorii Strashko dev_dbg(dev, "fragmented SKB\n"); 118493a76530SGrygorii Strashko 118593a76530SGrygorii Strashko /* Handle the case where skb is fragmented in pages */ 118693a76530SGrygorii Strashko cur_desc = first_desc; 118793a76530SGrygorii Strashko for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 118893a76530SGrygorii Strashko skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 118993a76530SGrygorii Strashko u32 frag_size = skb_frag_size(frag); 119093a76530SGrygorii Strashko 119193a76530SGrygorii Strashko next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 119293a76530SGrygorii Strashko if (!next_desc) { 119393a76530SGrygorii Strashko dev_err(dev, "Failed to allocate descriptor\n"); 119493a76530SGrygorii Strashko goto busy_free_descs; 119593a76530SGrygorii Strashko } 119693a76530SGrygorii Strashko 1197ed569ed9SPeter Ujfalusi buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size, 119893a76530SGrygorii Strashko DMA_TO_DEVICE); 1199ed569ed9SPeter Ujfalusi if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 120093a76530SGrygorii Strashko dev_err(dev, "Failed to map tx skb page\n"); 120193a76530SGrygorii Strashko k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 120293a76530SGrygorii Strashko ndev->stats.tx_errors++; 120393a76530SGrygorii Strashko goto err_free_descs; 120493a76530SGrygorii Strashko } 120593a76530SGrygorii Strashko 120693a76530SGrygorii Strashko cppi5_hdesc_reset_hbdesc(next_desc); 120739fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 120893a76530SGrygorii Strashko cppi5_hdesc_attach_buf(next_desc, 120993a76530SGrygorii Strashko buf_dma, frag_size, buf_dma, frag_size); 121093a76530SGrygorii Strashko 121193a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, 121293a76530SGrygorii Strashko next_desc); 121339fd0547SPeter Ujfalusi k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma); 121493a76530SGrygorii Strashko cppi5_hdesc_link_hbdesc(cur_desc, desc_dma); 121593a76530SGrygorii Strashko 121693a76530SGrygorii Strashko pkt_len += frag_size; 121793a76530SGrygorii Strashko cur_desc = next_desc; 121893a76530SGrygorii Strashko } 121993a76530SGrygorii Strashko WARN_ON(pkt_len != skb->len); 122093a76530SGrygorii Strashko 122193a76530SGrygorii Strashko done_tx: 122293a76530SGrygorii Strashko skb_tx_timestamp(skb); 122393a76530SGrygorii Strashko 122493a76530SGrygorii Strashko /* report bql before sending packet */ 122593a76530SGrygorii Strashko netdev_tx_sent_queue(netif_txq, pkt_len); 122693a76530SGrygorii Strashko 122793a76530SGrygorii Strashko cppi5_hdesc_set_pktlen(first_desc, pkt_len); 122893a76530SGrygorii Strashko desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc); 1229a9e60cf0SGrygorii Strashko if (AM65_CPSW_IS_CPSW2G(common)) { 123093a76530SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1231a9e60cf0SGrygorii Strashko } else { 1232a9e60cf0SGrygorii Strashko spin_lock_bh(&tx_chn->lock); 1233a9e60cf0SGrygorii Strashko ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1234a9e60cf0SGrygorii Strashko spin_unlock_bh(&tx_chn->lock); 1235a9e60cf0SGrygorii Strashko } 123693a76530SGrygorii Strashko if (ret) { 123793a76530SGrygorii Strashko dev_err(dev, "can't push desc %d\n", ret); 123893a76530SGrygorii Strashko /* inform bql */ 123993a76530SGrygorii Strashko netdev_tx_completed_queue(netif_txq, 1, pkt_len); 124093a76530SGrygorii Strashko ndev->stats.tx_errors++; 124193a76530SGrygorii Strashko goto err_free_descs; 124293a76530SGrygorii Strashko } 124393a76530SGrygorii Strashko 124493a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) { 124593a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 124693a76530SGrygorii Strashko /* Barrier, so that stop_queue visible to other cpus */ 124793a76530SGrygorii Strashko smp_mb__after_atomic(); 124893a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx); 124993a76530SGrygorii Strashko 125093a76530SGrygorii Strashko /* re-check for smp */ 125193a76530SGrygorii Strashko if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= 125293a76530SGrygorii Strashko MAX_SKB_FRAGS) { 125393a76530SGrygorii Strashko netif_tx_wake_queue(netif_txq); 125493a76530SGrygorii Strashko dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx); 125593a76530SGrygorii Strashko } 125693a76530SGrygorii Strashko } 125793a76530SGrygorii Strashko 125893a76530SGrygorii Strashko return NETDEV_TX_OK; 125993a76530SGrygorii Strashko 126093a76530SGrygorii Strashko err_free_descs: 1261ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 126293a76530SGrygorii Strashko err_free_skb: 126393a76530SGrygorii Strashko ndev->stats.tx_dropped++; 126493a76530SGrygorii Strashko dev_kfree_skb_any(skb); 126593a76530SGrygorii Strashko return NETDEV_TX_OK; 126693a76530SGrygorii Strashko 126793a76530SGrygorii Strashko busy_free_descs: 1268ed569ed9SPeter Ujfalusi am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 126993a76530SGrygorii Strashko busy_stop_q: 127093a76530SGrygorii Strashko netif_tx_stop_queue(netif_txq); 127193a76530SGrygorii Strashko return NETDEV_TX_BUSY; 127293a76530SGrygorii Strashko } 127393a76530SGrygorii Strashko 127493a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev, 127593a76530SGrygorii Strashko void *addr) 127693a76530SGrygorii Strashko { 127793a76530SGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 127893a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 127993a76530SGrygorii Strashko struct sockaddr *sockaddr = (struct sockaddr *)addr; 128093a76530SGrygorii Strashko int ret; 128193a76530SGrygorii Strashko 128293a76530SGrygorii Strashko ret = eth_prepare_mac_addr_change(ndev, addr); 128393a76530SGrygorii Strashko if (ret < 0) 128493a76530SGrygorii Strashko return ret; 128593a76530SGrygorii Strashko 128693a76530SGrygorii Strashko ret = pm_runtime_get_sync(common->dev); 128793a76530SGrygorii Strashko if (ret < 0) { 128893a76530SGrygorii Strashko pm_runtime_put_noidle(common->dev); 128993a76530SGrygorii Strashko return ret; 129093a76530SGrygorii Strashko } 129193a76530SGrygorii Strashko 129293a76530SGrygorii Strashko cpsw_ale_del_ucast(common->ale, ndev->dev_addr, 129393a76530SGrygorii Strashko HOST_PORT_NUM, 0, 0); 129493a76530SGrygorii Strashko cpsw_ale_add_ucast(common->ale, sockaddr->sa_data, 129593a76530SGrygorii Strashko HOST_PORT_NUM, ALE_SECURE, 0); 129693a76530SGrygorii Strashko 129793a76530SGrygorii Strashko am65_cpsw_port_set_sl_mac(port, addr); 129893a76530SGrygorii Strashko eth_commit_mac_addr_change(ndev, sockaddr); 129993a76530SGrygorii Strashko 130093a76530SGrygorii Strashko pm_runtime_put(common->dev); 130193a76530SGrygorii Strashko 130293a76530SGrygorii Strashko return 0; 130393a76530SGrygorii Strashko } 130493a76530SGrygorii Strashko 1305b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev, 1306b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1307b1f66a5bSGrygorii Strashko { 1308b1f66a5bSGrygorii Strashko struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1309b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1310b1f66a5bSGrygorii Strashko u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype; 1311b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1312b1f66a5bSGrygorii Strashko 1313b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1314b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1315b1f66a5bSGrygorii Strashko 1316b1f66a5bSGrygorii Strashko if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1317b1f66a5bSGrygorii Strashko return -EFAULT; 1318b1f66a5bSGrygorii Strashko 1319b1f66a5bSGrygorii Strashko /* TX HW timestamp */ 1320b1f66a5bSGrygorii Strashko switch (cfg.tx_type) { 1321b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_OFF: 1322b1f66a5bSGrygorii Strashko case HWTSTAMP_TX_ON: 1323b1f66a5bSGrygorii Strashko break; 1324b1f66a5bSGrygorii Strashko default: 1325b1f66a5bSGrygorii Strashko return -ERANGE; 1326b1f66a5bSGrygorii Strashko } 1327b1f66a5bSGrygorii Strashko 1328b1f66a5bSGrygorii Strashko switch (cfg.rx_filter) { 1329b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NONE: 1330b1f66a5bSGrygorii Strashko port->rx_ts_enabled = false; 1331b1f66a5bSGrygorii Strashko break; 1332b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_ALL: 1333b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_SOME: 1334b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1335b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1336b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1337b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1338b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1339b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1340b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1341b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1342b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1343b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_EVENT: 1344b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_SYNC: 1345b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1346b1f66a5bSGrygorii Strashko case HWTSTAMP_FILTER_NTP_ALL: 1347b1f66a5bSGrygorii Strashko port->rx_ts_enabled = true; 1348b1f66a5bSGrygorii Strashko cfg.rx_filter = HWTSTAMP_FILTER_ALL; 1349b1f66a5bSGrygorii Strashko break; 1350b1f66a5bSGrygorii Strashko default: 1351b1f66a5bSGrygorii Strashko return -ERANGE; 1352b1f66a5bSGrygorii Strashko } 1353b1f66a5bSGrygorii Strashko 1354b1f66a5bSGrygorii Strashko port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON); 1355b1f66a5bSGrygorii Strashko 1356b1f66a5bSGrygorii Strashko /* cfg TX timestamp */ 1357b1f66a5bSGrygorii Strashko seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET << 1358b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588; 1359b1f66a5bSGrygorii Strashko 1360b1f66a5bSGrygorii Strashko ts_vlan_ltype = ETH_P_8021Q; 1361b1f66a5bSGrygorii Strashko 1362b1f66a5bSGrygorii Strashko ts_ctrl_ltype2 = ETH_P_1588 | 1363b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 | 1364b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 | 1365b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 | 1366b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 | 1367b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 | 1368b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 | 1369b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 | 1370b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO; 1371b1f66a5bSGrygorii Strashko 1372b1f66a5bSGrygorii Strashko ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS << 1373b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT; 1374b1f66a5bSGrygorii Strashko 1375b1f66a5bSGrygorii Strashko if (port->tx_ts_enabled) 1376b1f66a5bSGrygorii Strashko ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN | 1377b1f66a5bSGrygorii Strashko AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN; 1378b1f66a5bSGrygorii Strashko 1379b1f66a5bSGrygorii Strashko writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG); 1380b1f66a5bSGrygorii Strashko writel(ts_vlan_ltype, port->port_base + 1381b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG); 1382b1f66a5bSGrygorii Strashko writel(ts_ctrl_ltype2, port->port_base + 1383b1f66a5bSGrygorii Strashko AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2); 1384b1f66a5bSGrygorii Strashko writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL); 1385b1f66a5bSGrygorii Strashko 1386b1f66a5bSGrygorii Strashko /* en/dis RX timestamp */ 1387b1f66a5bSGrygorii Strashko am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled); 1388b1f66a5bSGrygorii Strashko 1389b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1390b1f66a5bSGrygorii Strashko } 1391b1f66a5bSGrygorii Strashko 1392b1f66a5bSGrygorii Strashko static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev, 1393b1f66a5bSGrygorii Strashko struct ifreq *ifr) 1394b1f66a5bSGrygorii Strashko { 1395b1f66a5bSGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1396b1f66a5bSGrygorii Strashko struct hwtstamp_config cfg; 1397b1f66a5bSGrygorii Strashko 1398b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1399b1f66a5bSGrygorii Strashko return -EOPNOTSUPP; 1400b1f66a5bSGrygorii Strashko 1401b1f66a5bSGrygorii Strashko cfg.flags = 0; 1402b1f66a5bSGrygorii Strashko cfg.tx_type = port->tx_ts_enabled ? 1403b1f66a5bSGrygorii Strashko HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1404b1f66a5bSGrygorii Strashko cfg.rx_filter = port->rx_ts_enabled ? 1405b1f66a5bSGrygorii Strashko HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 1406b1f66a5bSGrygorii Strashko 1407b1f66a5bSGrygorii Strashko return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1408b1f66a5bSGrygorii Strashko } 1409b1f66a5bSGrygorii Strashko 141093a76530SGrygorii Strashko static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev, 141193a76530SGrygorii Strashko struct ifreq *req, int cmd) 141293a76530SGrygorii Strashko { 141393a76530SGrygorii Strashko struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 141493a76530SGrygorii Strashko 141593a76530SGrygorii Strashko if (!netif_running(ndev)) 141693a76530SGrygorii Strashko return -EINVAL; 141793a76530SGrygorii Strashko 1418b1f66a5bSGrygorii Strashko switch (cmd) { 1419b1f66a5bSGrygorii Strashko case SIOCSHWTSTAMP: 1420b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_set(ndev, req); 1421b1f66a5bSGrygorii Strashko case SIOCGHWTSTAMP: 1422b1f66a5bSGrygorii Strashko return am65_cpsw_nuss_hwtstamp_get(ndev, req); 1423b1f66a5bSGrygorii Strashko } 1424b1f66a5bSGrygorii Strashko 142593a76530SGrygorii Strashko if (!port->slave.phy) 142693a76530SGrygorii Strashko return -EOPNOTSUPP; 142793a76530SGrygorii Strashko 142893a76530SGrygorii Strashko return phy_mii_ioctl(port->slave.phy, req, cmd); 142993a76530SGrygorii Strashko } 143093a76530SGrygorii Strashko 143193a76530SGrygorii Strashko static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev, 143293a76530SGrygorii Strashko struct rtnl_link_stats64 *stats) 143393a76530SGrygorii Strashko { 143493a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev); 143593a76530SGrygorii Strashko unsigned int start; 143693a76530SGrygorii Strashko int cpu; 143793a76530SGrygorii Strashko 143893a76530SGrygorii Strashko for_each_possible_cpu(cpu) { 143993a76530SGrygorii Strashko struct am65_cpsw_ndev_stats *cpu_stats; 144093a76530SGrygorii Strashko u64 rx_packets; 144193a76530SGrygorii Strashko u64 rx_bytes; 144293a76530SGrygorii Strashko u64 tx_packets; 144393a76530SGrygorii Strashko u64 tx_bytes; 144493a76530SGrygorii Strashko 144593a76530SGrygorii Strashko cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu); 144693a76530SGrygorii Strashko do { 144793a76530SGrygorii Strashko start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 144893a76530SGrygorii Strashko rx_packets = cpu_stats->rx_packets; 144993a76530SGrygorii Strashko rx_bytes = cpu_stats->rx_bytes; 145093a76530SGrygorii Strashko tx_packets = cpu_stats->tx_packets; 145193a76530SGrygorii Strashko tx_bytes = cpu_stats->tx_bytes; 145293a76530SGrygorii Strashko } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 145393a76530SGrygorii Strashko 145493a76530SGrygorii Strashko stats->rx_packets += rx_packets; 145593a76530SGrygorii Strashko stats->rx_bytes += rx_bytes; 145693a76530SGrygorii Strashko stats->tx_packets += tx_packets; 145793a76530SGrygorii Strashko stats->tx_bytes += tx_bytes; 145893a76530SGrygorii Strashko } 145993a76530SGrygorii Strashko 146093a76530SGrygorii Strashko stats->rx_errors = dev->stats.rx_errors; 146193a76530SGrygorii Strashko stats->rx_dropped = dev->stats.rx_dropped; 146293a76530SGrygorii Strashko stats->tx_dropped = dev->stats.tx_dropped; 146393a76530SGrygorii Strashko } 146493a76530SGrygorii Strashko 146558356eb3SVignesh Raghavendra static struct devlink_port *am65_cpsw_ndo_get_devlink_port(struct net_device *ndev) 146658356eb3SVignesh Raghavendra { 146758356eb3SVignesh Raghavendra struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 146858356eb3SVignesh Raghavendra 146958356eb3SVignesh Raghavendra return &port->devlink_port; 147058356eb3SVignesh Raghavendra } 147158356eb3SVignesh Raghavendra 147284b4aa49SGrygorii Strashko static const struct net_device_ops am65_cpsw_nuss_netdev_ops = { 147393a76530SGrygorii Strashko .ndo_open = am65_cpsw_nuss_ndo_slave_open, 147493a76530SGrygorii Strashko .ndo_stop = am65_cpsw_nuss_ndo_slave_stop, 147593a76530SGrygorii Strashko .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit, 147693a76530SGrygorii Strashko .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode, 147793a76530SGrygorii Strashko .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats, 147893a76530SGrygorii Strashko .ndo_validate_addr = eth_validate_addr, 147993a76530SGrygorii Strashko .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address, 148093a76530SGrygorii Strashko .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout, 148193a76530SGrygorii Strashko .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid, 148293a76530SGrygorii Strashko .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid, 1483*a7605370SArnd Bergmann .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl, 14848127224cSIvan Khoronzhuk .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc, 148558356eb3SVignesh Raghavendra .ndo_get_devlink_port = am65_cpsw_ndo_get_devlink_port, 148693a76530SGrygorii Strashko }; 148793a76530SGrygorii Strashko 148893a76530SGrygorii Strashko static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port) 148993a76530SGrygorii Strashko { 149093a76530SGrygorii Strashko struct am65_cpsw_common *common = port->common; 149193a76530SGrygorii Strashko 149293a76530SGrygorii Strashko if (!port->disabled) 149393a76530SGrygorii Strashko return; 149493a76530SGrygorii Strashko 149593a76530SGrygorii Strashko cpsw_ale_control_set(common->ale, port->port_id, 149693a76530SGrygorii Strashko ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 149793a76530SGrygorii Strashko 149893a76530SGrygorii Strashko cpsw_sl_reset(port->slave.mac_sl, 100); 149993a76530SGrygorii Strashko cpsw_sl_ctl_reset(port->slave.mac_sl); 150093a76530SGrygorii Strashko } 150193a76530SGrygorii Strashko 150293a76530SGrygorii Strashko static void am65_cpsw_nuss_free_tx_chns(void *data) 150393a76530SGrygorii Strashko { 150493a76530SGrygorii Strashko struct am65_cpsw_common *common = data; 150593a76530SGrygorii Strashko int i; 150693a76530SGrygorii Strashko 150793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 150893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 150993a76530SGrygorii Strashko 151093a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 151193a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 151293a76530SGrygorii Strashko 1513ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1514ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1515ce8eb4c7SVignesh Raghavendra 151693a76530SGrygorii Strashko memset(tx_chn, 0, sizeof(*tx_chn)); 151793a76530SGrygorii Strashko } 151893a76530SGrygorii Strashko } 151993a76530SGrygorii Strashko 152093a76530SGrygorii Strashko void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common) 152193a76530SGrygorii Strashko { 152293a76530SGrygorii Strashko struct device *dev = common->dev; 152393a76530SGrygorii Strashko int i; 152493a76530SGrygorii Strashko 152593a76530SGrygorii Strashko devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common); 152693a76530SGrygorii Strashko 152793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 152893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 152993a76530SGrygorii Strashko 153093a76530SGrygorii Strashko if (tx_chn->irq) 153193a76530SGrygorii Strashko devm_free_irq(dev, tx_chn->irq, tx_chn); 153293a76530SGrygorii Strashko 153393a76530SGrygorii Strashko netif_napi_del(&tx_chn->napi_tx); 153493a76530SGrygorii Strashko 153593a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 153693a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 153793a76530SGrygorii Strashko 1538ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1539ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1540ce8eb4c7SVignesh Raghavendra 154193a76530SGrygorii Strashko memset(tx_chn, 0, sizeof(*tx_chn)); 154293a76530SGrygorii Strashko } 154393a76530SGrygorii Strashko } 154493a76530SGrygorii Strashko 154593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common) 154693a76530SGrygorii Strashko { 154793a76530SGrygorii Strashko u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS); 154893a76530SGrygorii Strashko struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 }; 154993a76530SGrygorii Strashko struct device *dev = common->dev; 155093a76530SGrygorii Strashko struct k3_ring_cfg ring_cfg = { 155193a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 155293a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 155393a76530SGrygorii Strashko .flags = 0 155493a76530SGrygorii Strashko }; 155593a76530SGrygorii Strashko u32 hdesc_size; 155693a76530SGrygorii Strashko int i, ret = 0; 155793a76530SGrygorii Strashko 155893a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 155993a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 156093a76530SGrygorii Strashko 156193a76530SGrygorii Strashko tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 156293a76530SGrygorii Strashko tx_cfg.tx_cfg = ring_cfg; 156393a76530SGrygorii Strashko tx_cfg.txcq_cfg = ring_cfg; 156493a76530SGrygorii Strashko tx_cfg.tx_cfg.size = max_desc_num; 156593a76530SGrygorii Strashko tx_cfg.txcq_cfg.size = max_desc_num; 156693a76530SGrygorii Strashko 156793a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 156893a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 156993a76530SGrygorii Strashko 157093a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 157193a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "tx%d", i); 157293a76530SGrygorii Strashko 1573a9e60cf0SGrygorii Strashko spin_lock_init(&tx_chn->lock); 157493a76530SGrygorii Strashko tx_chn->common = common; 157593a76530SGrygorii Strashko tx_chn->id = i; 157693a76530SGrygorii Strashko tx_chn->descs_num = max_desc_num; 157793a76530SGrygorii Strashko 157893a76530SGrygorii Strashko tx_chn->tx_chn = 157993a76530SGrygorii Strashko k3_udma_glue_request_tx_chn(dev, 158093a76530SGrygorii Strashko tx_chn->tx_chn_name, 158193a76530SGrygorii Strashko &tx_cfg); 158293a76530SGrygorii Strashko if (IS_ERR(tx_chn->tx_chn)) { 15838fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn), 15848fbc2f9eSGrygorii Strashko "Failed to request tx dma channel\n"); 158593a76530SGrygorii Strashko goto err; 158693a76530SGrygorii Strashko } 1587ed569ed9SPeter Ujfalusi tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn); 1588ed569ed9SPeter Ujfalusi 1589ed569ed9SPeter Ujfalusi tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev, 1590ed569ed9SPeter Ujfalusi tx_chn->descs_num, 1591ed569ed9SPeter Ujfalusi hdesc_size, 1592ed569ed9SPeter Ujfalusi tx_chn->tx_chn_name); 1593ed569ed9SPeter Ujfalusi if (IS_ERR(tx_chn->desc_pool)) { 1594ed569ed9SPeter Ujfalusi ret = PTR_ERR(tx_chn->desc_pool); 1595ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create poll %d\n", ret); 1596ed569ed9SPeter Ujfalusi goto err; 1597ed569ed9SPeter Ujfalusi } 159893a76530SGrygorii Strashko 159993a76530SGrygorii Strashko tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn); 160093a76530SGrygorii Strashko if (tx_chn->irq <= 0) { 160193a76530SGrygorii Strashko dev_err(dev, "Failed to get tx dma irq %d\n", 160293a76530SGrygorii Strashko tx_chn->irq); 160393a76530SGrygorii Strashko goto err; 160493a76530SGrygorii Strashko } 160593a76530SGrygorii Strashko 160693a76530SGrygorii Strashko snprintf(tx_chn->tx_chn_name, 160793a76530SGrygorii Strashko sizeof(tx_chn->tx_chn_name), "%s-tx%d", 160893a76530SGrygorii Strashko dev_name(dev), tx_chn->id); 160993a76530SGrygorii Strashko } 161093a76530SGrygorii Strashko 161193a76530SGrygorii Strashko err: 161293a76530SGrygorii Strashko i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common); 161393a76530SGrygorii Strashko if (i) { 1614e6aaeafdSChristophe JAILLET dev_err(dev, "Failed to add free_tx_chns action %d\n", i); 161593a76530SGrygorii Strashko return i; 161693a76530SGrygorii Strashko } 161793a76530SGrygorii Strashko 161893a76530SGrygorii Strashko return ret; 161993a76530SGrygorii Strashko } 162093a76530SGrygorii Strashko 162193a76530SGrygorii Strashko static void am65_cpsw_nuss_free_rx_chns(void *data) 162293a76530SGrygorii Strashko { 162393a76530SGrygorii Strashko struct am65_cpsw_common *common = data; 162493a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn; 162593a76530SGrygorii Strashko 162693a76530SGrygorii Strashko rx_chn = &common->rx_chns; 162793a76530SGrygorii Strashko 162893a76530SGrygorii Strashko if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 162993a76530SGrygorii Strashko k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 1630ce8eb4c7SVignesh Raghavendra 1631ce8eb4c7SVignesh Raghavendra if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 1632ce8eb4c7SVignesh Raghavendra k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 163393a76530SGrygorii Strashko } 163493a76530SGrygorii Strashko 163593a76530SGrygorii Strashko static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common) 163693a76530SGrygorii Strashko { 163793a76530SGrygorii Strashko struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 163893a76530SGrygorii Strashko struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 }; 163993a76530SGrygorii Strashko u32 max_desc_num = AM65_CPSW_MAX_RX_DESC; 164093a76530SGrygorii Strashko struct device *dev = common->dev; 164193a76530SGrygorii Strashko u32 hdesc_size; 164293a76530SGrygorii Strashko u32 fdqring_id; 164393a76530SGrygorii Strashko int i, ret = 0; 164493a76530SGrygorii Strashko 164593a76530SGrygorii Strashko hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 164693a76530SGrygorii Strashko AM65_CPSW_NAV_SW_DATA_SIZE); 164793a76530SGrygorii Strashko 164893a76530SGrygorii Strashko rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 164993a76530SGrygorii Strashko rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS; 165093a76530SGrygorii Strashko rx_cfg.flow_id_base = common->rx_flow_id_base; 165193a76530SGrygorii Strashko 165293a76530SGrygorii Strashko /* init all flows */ 165393a76530SGrygorii Strashko rx_chn->dev = dev; 165493a76530SGrygorii Strashko rx_chn->descs_num = max_desc_num; 165593a76530SGrygorii Strashko 165693a76530SGrygorii Strashko rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg); 165793a76530SGrygorii Strashko if (IS_ERR(rx_chn->rx_chn)) { 16588fbc2f9eSGrygorii Strashko ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn), 16598fbc2f9eSGrygorii Strashko "Failed to request rx dma channel\n"); 166093a76530SGrygorii Strashko goto err; 166193a76530SGrygorii Strashko } 1662ed569ed9SPeter Ujfalusi rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn); 1663ed569ed9SPeter Ujfalusi 1664ed569ed9SPeter Ujfalusi rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev, 1665ed569ed9SPeter Ujfalusi rx_chn->descs_num, 1666ed569ed9SPeter Ujfalusi hdesc_size, "rx"); 1667ed569ed9SPeter Ujfalusi if (IS_ERR(rx_chn->desc_pool)) { 1668ed569ed9SPeter Ujfalusi ret = PTR_ERR(rx_chn->desc_pool); 1669ed569ed9SPeter Ujfalusi dev_err(dev, "Failed to create rx poll %d\n", ret); 1670ed569ed9SPeter Ujfalusi goto err; 1671ed569ed9SPeter Ujfalusi } 167293a76530SGrygorii Strashko 167393a76530SGrygorii Strashko common->rx_flow_id_base = 167493a76530SGrygorii Strashko k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn); 167593a76530SGrygorii Strashko dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base); 167693a76530SGrygorii Strashko 167793a76530SGrygorii Strashko fdqring_id = K3_RINGACC_RING_ID_ANY; 167893a76530SGrygorii Strashko for (i = 0; i < rx_cfg.flow_id_num; i++) { 167993a76530SGrygorii Strashko struct k3_ring_cfg rxring_cfg = { 168093a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 168193a76530SGrygorii Strashko .mode = K3_RINGACC_RING_MODE_RING, 168293a76530SGrygorii Strashko .flags = 0, 168393a76530SGrygorii Strashko }; 168493a76530SGrygorii Strashko struct k3_ring_cfg fdqring_cfg = { 168593a76530SGrygorii Strashko .elm_size = K3_RINGACC_RING_ELSIZE_8, 168693a76530SGrygorii Strashko .flags = K3_RINGACC_RING_SHARED, 168793a76530SGrygorii Strashko }; 168893a76530SGrygorii Strashko struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = { 168993a76530SGrygorii Strashko .rx_cfg = rxring_cfg, 169093a76530SGrygorii Strashko .rxfdq_cfg = fdqring_cfg, 169193a76530SGrygorii Strashko .ring_rxq_id = K3_RINGACC_RING_ID_ANY, 169293a76530SGrygorii Strashko .src_tag_lo_sel = 169393a76530SGrygorii Strashko K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG, 169493a76530SGrygorii Strashko }; 169593a76530SGrygorii Strashko 169693a76530SGrygorii Strashko rx_flow_cfg.ring_rxfdq0_id = fdqring_id; 169793a76530SGrygorii Strashko rx_flow_cfg.rx_cfg.size = max_desc_num; 169893a76530SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.size = max_desc_num; 1699c6275c02SGrygorii Strashko rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode; 170093a76530SGrygorii Strashko 170193a76530SGrygorii Strashko ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn, 170293a76530SGrygorii Strashko i, &rx_flow_cfg); 170393a76530SGrygorii Strashko if (ret) { 170493a76530SGrygorii Strashko dev_err(dev, "Failed to init rx flow%d %d\n", i, ret); 170593a76530SGrygorii Strashko goto err; 170693a76530SGrygorii Strashko } 170793a76530SGrygorii Strashko if (!i) 170893a76530SGrygorii Strashko fdqring_id = 170993a76530SGrygorii Strashko k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn, 171093a76530SGrygorii Strashko i); 171193a76530SGrygorii Strashko 171293a76530SGrygorii Strashko rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i); 171393a76530SGrygorii Strashko 171493a76530SGrygorii Strashko if (rx_chn->irq <= 0) { 171593a76530SGrygorii Strashko dev_err(dev, "Failed to get rx dma irq %d\n", 171693a76530SGrygorii Strashko rx_chn->irq); 171793a76530SGrygorii Strashko ret = -ENXIO; 171893a76530SGrygorii Strashko goto err; 171993a76530SGrygorii Strashko } 172093a76530SGrygorii Strashko } 172193a76530SGrygorii Strashko 172293a76530SGrygorii Strashko err: 172393a76530SGrygorii Strashko i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common); 172493a76530SGrygorii Strashko if (i) { 1725e6aaeafdSChristophe JAILLET dev_err(dev, "Failed to add free_rx_chns action %d\n", i); 172693a76530SGrygorii Strashko return i; 172793a76530SGrygorii Strashko } 172893a76530SGrygorii Strashko 172993a76530SGrygorii Strashko return ret; 173093a76530SGrygorii Strashko } 173193a76530SGrygorii Strashko 173293a76530SGrygorii Strashko static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common) 173393a76530SGrygorii Strashko { 173493a76530SGrygorii Strashko struct am65_cpsw_host *host_p = am65_common_get_host(common); 173593a76530SGrygorii Strashko 173693a76530SGrygorii Strashko host_p->common = common; 173793a76530SGrygorii Strashko host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE; 173893a76530SGrygorii Strashko host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE; 173993a76530SGrygorii Strashko 174093a76530SGrygorii Strashko return 0; 174193a76530SGrygorii Strashko } 174293a76530SGrygorii Strashko 174393a76530SGrygorii Strashko static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node, 174493a76530SGrygorii Strashko int slave, u8 *mac_addr) 174593a76530SGrygorii Strashko { 174693a76530SGrygorii Strashko u32 mac_lo, mac_hi, offset; 174793a76530SGrygorii Strashko struct regmap *syscon; 174893a76530SGrygorii Strashko int ret; 174993a76530SGrygorii Strashko 175093a76530SGrygorii Strashko syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse"); 175193a76530SGrygorii Strashko if (IS_ERR(syscon)) { 175293a76530SGrygorii Strashko if (PTR_ERR(syscon) == -ENODEV) 175393a76530SGrygorii Strashko return 0; 175493a76530SGrygorii Strashko return PTR_ERR(syscon); 175593a76530SGrygorii Strashko } 175693a76530SGrygorii Strashko 175793a76530SGrygorii Strashko ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1, 175893a76530SGrygorii Strashko &offset); 175993a76530SGrygorii Strashko if (ret) 176093a76530SGrygorii Strashko return ret; 176193a76530SGrygorii Strashko 176293a76530SGrygorii Strashko regmap_read(syscon, offset, &mac_lo); 176393a76530SGrygorii Strashko regmap_read(syscon, offset + 4, &mac_hi); 176493a76530SGrygorii Strashko 176593a76530SGrygorii Strashko mac_addr[0] = (mac_hi >> 8) & 0xff; 176693a76530SGrygorii Strashko mac_addr[1] = mac_hi & 0xff; 176793a76530SGrygorii Strashko mac_addr[2] = (mac_lo >> 24) & 0xff; 176893a76530SGrygorii Strashko mac_addr[3] = (mac_lo >> 16) & 0xff; 176993a76530SGrygorii Strashko mac_addr[4] = (mac_lo >> 8) & 0xff; 177093a76530SGrygorii Strashko mac_addr[5] = mac_lo & 0xff; 177193a76530SGrygorii Strashko 177293a76530SGrygorii Strashko return 0; 177393a76530SGrygorii Strashko } 177493a76530SGrygorii Strashko 1775b1f66a5bSGrygorii Strashko static int am65_cpsw_init_cpts(struct am65_cpsw_common *common) 1776b1f66a5bSGrygorii Strashko { 1777b1f66a5bSGrygorii Strashko struct device *dev = common->dev; 1778b1f66a5bSGrygorii Strashko struct device_node *node; 1779b1f66a5bSGrygorii Strashko struct am65_cpts *cpts; 1780b1f66a5bSGrygorii Strashko void __iomem *reg_base; 1781b1f66a5bSGrygorii Strashko 1782b1f66a5bSGrygorii Strashko if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1783b1f66a5bSGrygorii Strashko return 0; 1784b1f66a5bSGrygorii Strashko 1785b1f66a5bSGrygorii Strashko node = of_get_child_by_name(dev->of_node, "cpts"); 1786b1f66a5bSGrygorii Strashko if (!node) { 1787b1f66a5bSGrygorii Strashko dev_err(dev, "%s cpts not found\n", __func__); 1788b1f66a5bSGrygorii Strashko return -ENOENT; 1789b1f66a5bSGrygorii Strashko } 1790b1f66a5bSGrygorii Strashko 1791b1f66a5bSGrygorii Strashko reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE; 1792b1f66a5bSGrygorii Strashko cpts = am65_cpts_create(dev, reg_base, node); 1793b1f66a5bSGrygorii Strashko if (IS_ERR(cpts)) { 1794b1f66a5bSGrygorii Strashko int ret = PTR_ERR(cpts); 1795b1f66a5bSGrygorii Strashko 1796b1f66a5bSGrygorii Strashko if (ret == -EOPNOTSUPP) { 1797b1f66a5bSGrygorii Strashko dev_info(dev, "cpts disabled\n"); 1798b1f66a5bSGrygorii Strashko return 0; 1799b1f66a5bSGrygorii Strashko } 1800b1f66a5bSGrygorii Strashko 1801b1f66a5bSGrygorii Strashko dev_err(dev, "cpts create err %d\n", ret); 1802b1f66a5bSGrygorii Strashko return ret; 1803b1f66a5bSGrygorii Strashko } 1804b1f66a5bSGrygorii Strashko common->cpts = cpts; 1805a9c74700SGrygorii Strashko /* Forbid PM runtime if CPTS is running. 1806a9c74700SGrygorii Strashko * K3 CPSWxG modules may completely lose context during ON->OFF 1807a9c74700SGrygorii Strashko * transitions depending on integration. 1808a9c74700SGrygorii Strashko * AM65x/J721E MCU CPSW2G: false 1809a9c74700SGrygorii Strashko * J721E MAIN_CPSW9G: true 1810a9c74700SGrygorii Strashko */ 1811a9c74700SGrygorii Strashko pm_runtime_forbid(dev); 1812b1f66a5bSGrygorii Strashko 1813b1f66a5bSGrygorii Strashko return 0; 1814b1f66a5bSGrygorii Strashko } 1815b1f66a5bSGrygorii Strashko 181693a76530SGrygorii Strashko static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common) 181793a76530SGrygorii Strashko { 181893a76530SGrygorii Strashko struct device_node *node, *port_np; 181993a76530SGrygorii Strashko struct device *dev = common->dev; 182093a76530SGrygorii Strashko int ret; 182193a76530SGrygorii Strashko 182293a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 182393a76530SGrygorii Strashko if (!node) 182493a76530SGrygorii Strashko return -ENOENT; 182593a76530SGrygorii Strashko 182693a76530SGrygorii Strashko for_each_child_of_node(node, port_np) { 182793a76530SGrygorii Strashko struct am65_cpsw_port *port; 182893a76530SGrygorii Strashko u32 port_id; 182993a76530SGrygorii Strashko 183093a76530SGrygorii Strashko /* it is not a slave port node, continue */ 183193a76530SGrygorii Strashko if (strcmp(port_np->name, "port")) 183293a76530SGrygorii Strashko continue; 183393a76530SGrygorii Strashko 183493a76530SGrygorii Strashko ret = of_property_read_u32(port_np, "reg", &port_id); 183593a76530SGrygorii Strashko if (ret < 0) { 183693a76530SGrygorii Strashko dev_err(dev, "%pOF error reading port_id %d\n", 183793a76530SGrygorii Strashko port_np, ret); 183893a76530SGrygorii Strashko return ret; 183993a76530SGrygorii Strashko } 184093a76530SGrygorii Strashko 184193a76530SGrygorii Strashko if (!port_id || port_id > common->port_num) { 184293a76530SGrygorii Strashko dev_err(dev, "%pOF has invalid port_id %u %s\n", 184393a76530SGrygorii Strashko port_np, port_id, port_np->name); 184493a76530SGrygorii Strashko return -EINVAL; 184593a76530SGrygorii Strashko } 184693a76530SGrygorii Strashko 184793a76530SGrygorii Strashko port = am65_common_get_port(common, port_id); 184893a76530SGrygorii Strashko port->port_id = port_id; 184993a76530SGrygorii Strashko port->common = common; 185093a76530SGrygorii Strashko port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE + 185193a76530SGrygorii Strashko AM65_CPSW_NU_PORTS_OFFSET * (port_id); 185293a76530SGrygorii Strashko port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE + 185393a76530SGrygorii Strashko (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id); 185493a76530SGrygorii Strashko port->name = of_get_property(port_np, "label", NULL); 18558127224cSIvan Khoronzhuk port->fetch_ram_base = 18568127224cSIvan Khoronzhuk common->cpsw_base + AM65_CPSW_NU_FRAM_BASE + 18578127224cSIvan Khoronzhuk (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1)); 185893a76530SGrygorii Strashko 1859d6d0aeafSGrygorii Strashko port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base); 1860d6d0aeafSGrygorii Strashko if (IS_ERR(port->slave.mac_sl)) 1861d6d0aeafSGrygorii Strashko return PTR_ERR(port->slave.mac_sl); 1862d6d0aeafSGrygorii Strashko 186393a76530SGrygorii Strashko port->disabled = !of_device_is_available(port_np); 186484b4aa49SGrygorii Strashko if (port->disabled) { 186584b4aa49SGrygorii Strashko common->disabled_ports_mask |= BIT(port->port_id); 186693a76530SGrygorii Strashko continue; 186784b4aa49SGrygorii Strashko } 186893a76530SGrygorii Strashko 186993a76530SGrygorii Strashko port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL); 187093a76530SGrygorii Strashko if (IS_ERR(port->slave.ifphy)) { 187193a76530SGrygorii Strashko ret = PTR_ERR(port->slave.ifphy); 187293a76530SGrygorii Strashko dev_err(dev, "%pOF error retrieving port phy: %d\n", 187393a76530SGrygorii Strashko port_np, ret); 187493a76530SGrygorii Strashko return ret; 187593a76530SGrygorii Strashko } 187693a76530SGrygorii Strashko 187793a76530SGrygorii Strashko port->slave.mac_only = 187893a76530SGrygorii Strashko of_property_read_bool(port_np, "ti,mac-only"); 187993a76530SGrygorii Strashko 188093a76530SGrygorii Strashko /* get phy/link info */ 188193a76530SGrygorii Strashko if (of_phy_is_fixed_link(port_np)) { 188293a76530SGrygorii Strashko ret = of_phy_register_fixed_link(port_np); 18838fbc2f9eSGrygorii Strashko if (ret) 18848fbc2f9eSGrygorii Strashko return dev_err_probe(dev, ret, 18858fbc2f9eSGrygorii Strashko "failed to register fixed-link phy %pOF\n", 18868fbc2f9eSGrygorii Strashko port_np); 188793a76530SGrygorii Strashko port->slave.phy_node = of_node_get(port_np); 188893a76530SGrygorii Strashko } else { 188993a76530SGrygorii Strashko port->slave.phy_node = 189093a76530SGrygorii Strashko of_parse_phandle(port_np, "phy-handle", 0); 189193a76530SGrygorii Strashko } 189293a76530SGrygorii Strashko 189393a76530SGrygorii Strashko if (!port->slave.phy_node) { 189493a76530SGrygorii Strashko dev_err(dev, 189593a76530SGrygorii Strashko "slave[%d] no phy found\n", port_id); 189693a76530SGrygorii Strashko return -ENODEV; 189793a76530SGrygorii Strashko } 189893a76530SGrygorii Strashko 189993a76530SGrygorii Strashko ret = of_get_phy_mode(port_np, &port->slave.phy_if); 190093a76530SGrygorii Strashko if (ret) { 190193a76530SGrygorii Strashko dev_err(dev, "%pOF read phy-mode err %d\n", 190293a76530SGrygorii Strashko port_np, ret); 190393a76530SGrygorii Strashko return ret; 190493a76530SGrygorii Strashko } 190593a76530SGrygorii Strashko 190683216e39SMichael Walle ret = of_get_mac_address(port_np, port->slave.mac_addr); 190783216e39SMichael Walle if (ret) { 190883216e39SMichael Walle am65_cpsw_am654_get_efuse_macid(port_np, 190993a76530SGrygorii Strashko port->port_id, 191083216e39SMichael Walle port->slave.mac_addr); 191183216e39SMichael Walle if (!is_valid_ether_addr(port->slave.mac_addr)) { 191293a76530SGrygorii Strashko random_ether_addr(port->slave.mac_addr); 191376dcbd23SColin Ian King dev_err(dev, "Use random MAC address\n"); 191493a76530SGrygorii Strashko } 191593a76530SGrygorii Strashko } 191683216e39SMichael Walle } 191793a76530SGrygorii Strashko of_node_put(node); 191893a76530SGrygorii Strashko 191984b4aa49SGrygorii Strashko /* is there at least one ext.port */ 192084b4aa49SGrygorii Strashko if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) { 192184b4aa49SGrygorii Strashko dev_err(dev, "No Ext. port are available\n"); 192284b4aa49SGrygorii Strashko return -ENODEV; 192384b4aa49SGrygorii Strashko } 192484b4aa49SGrygorii Strashko 192593a76530SGrygorii Strashko return 0; 192693a76530SGrygorii Strashko } 192793a76530SGrygorii Strashko 192893a76530SGrygorii Strashko static void am65_cpsw_pcpu_stats_free(void *data) 192993a76530SGrygorii Strashko { 193093a76530SGrygorii Strashko struct am65_cpsw_ndev_stats __percpu *stats = data; 193193a76530SGrygorii Strashko 193293a76530SGrygorii Strashko free_percpu(stats); 193393a76530SGrygorii Strashko } 193493a76530SGrygorii Strashko 193584b4aa49SGrygorii Strashko static int 193684b4aa49SGrygorii Strashko am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx) 193793a76530SGrygorii Strashko { 193893a76530SGrygorii Strashko struct am65_cpsw_ndev_priv *ndev_priv; 193993a76530SGrygorii Strashko struct device *dev = common->dev; 194093a76530SGrygorii Strashko struct am65_cpsw_port *port; 194193a76530SGrygorii Strashko int ret; 194293a76530SGrygorii Strashko 194384b4aa49SGrygorii Strashko port = &common->ports[port_idx]; 194484b4aa49SGrygorii Strashko 194584b4aa49SGrygorii Strashko if (port->disabled) 194684b4aa49SGrygorii Strashko return 0; 194793a76530SGrygorii Strashko 194893a76530SGrygorii Strashko /* alloc netdev */ 194993a76530SGrygorii Strashko port->ndev = devm_alloc_etherdev_mqs(common->dev, 195093a76530SGrygorii Strashko sizeof(struct am65_cpsw_ndev_priv), 195193a76530SGrygorii Strashko AM65_CPSW_MAX_TX_QUEUES, 195293a76530SGrygorii Strashko AM65_CPSW_MAX_RX_QUEUES); 195393a76530SGrygorii Strashko if (!port->ndev) { 195493a76530SGrygorii Strashko dev_err(dev, "error allocating slave net_device %u\n", 195593a76530SGrygorii Strashko port->port_id); 195693a76530SGrygorii Strashko return -ENOMEM; 195793a76530SGrygorii Strashko } 195893a76530SGrygorii Strashko 195993a76530SGrygorii Strashko ndev_priv = netdev_priv(port->ndev); 196093a76530SGrygorii Strashko ndev_priv->port = port; 196193a76530SGrygorii Strashko ndev_priv->msg_enable = AM65_CPSW_DEBUG; 196293a76530SGrygorii Strashko SET_NETDEV_DEV(port->ndev, dev); 196393a76530SGrygorii Strashko 196493a76530SGrygorii Strashko ether_addr_copy(port->ndev->dev_addr, port->slave.mac_addr); 196593a76530SGrygorii Strashko 196693a76530SGrygorii Strashko port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE; 196793a76530SGrygorii Strashko port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE; 196893a76530SGrygorii Strashko port->ndev->hw_features = NETIF_F_SG | 196993a76530SGrygorii Strashko NETIF_F_RXCSUM | 19702c4dc314SMurali Karicheri NETIF_F_HW_CSUM | 19712c4dc314SMurali Karicheri NETIF_F_HW_TC; 197293a76530SGrygorii Strashko port->ndev->features = port->ndev->hw_features | 197393a76530SGrygorii Strashko NETIF_F_HW_VLAN_CTAG_FILTER; 197493a76530SGrygorii Strashko port->ndev->vlan_features |= NETIF_F_SG; 197584b4aa49SGrygorii Strashko port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops; 197693a76530SGrygorii Strashko port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave; 197793a76530SGrygorii Strashko 197893a76530SGrygorii Strashko /* Disable TX checksum offload by default due to HW bug */ 197938389aa6SGrygorii Strashko if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM) 198093a76530SGrygorii Strashko port->ndev->features &= ~NETIF_F_HW_CSUM; 198193a76530SGrygorii Strashko 198293a76530SGrygorii Strashko ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats); 198393a76530SGrygorii Strashko if (!ndev_priv->stats) 198493a76530SGrygorii Strashko return -ENOMEM; 198593a76530SGrygorii Strashko 198693a76530SGrygorii Strashko ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free, 198793a76530SGrygorii Strashko ndev_priv->stats); 198884b4aa49SGrygorii Strashko if (ret) 198984b4aa49SGrygorii Strashko dev_err(dev, "failed to add percpu stat free action %d\n", ret); 199084b4aa49SGrygorii Strashko 199184b4aa49SGrygorii Strashko if (!common->dma_ndev) 199284b4aa49SGrygorii Strashko common->dma_ndev = port->ndev; 199384b4aa49SGrygorii Strashko 199493a76530SGrygorii Strashko return ret; 199593a76530SGrygorii Strashko } 199693a76530SGrygorii Strashko 199784b4aa49SGrygorii Strashko static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common) 199884b4aa49SGrygorii Strashko { 199984b4aa49SGrygorii Strashko int ret; 200084b4aa49SGrygorii Strashko int i; 200184b4aa49SGrygorii Strashko 200284b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 200384b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_port_ndev(common, i); 200484b4aa49SGrygorii Strashko if (ret) 200584b4aa49SGrygorii Strashko return ret; 200684b4aa49SGrygorii Strashko } 200784b4aa49SGrygorii Strashko 200884b4aa49SGrygorii Strashko netif_napi_add(common->dma_ndev, &common->napi_rx, 200993a76530SGrygorii Strashko am65_cpsw_nuss_rx_poll, NAPI_POLL_WEIGHT); 201093a76530SGrygorii Strashko 201193a76530SGrygorii Strashko return ret; 201293a76530SGrygorii Strashko } 201393a76530SGrygorii Strashko 201484b4aa49SGrygorii Strashko static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common) 201593a76530SGrygorii Strashko { 201693a76530SGrygorii Strashko struct device *dev = common->dev; 201793a76530SGrygorii Strashko int i, ret = 0; 201893a76530SGrygorii Strashko 201993a76530SGrygorii Strashko for (i = 0; i < common->tx_ch_num; i++) { 202093a76530SGrygorii Strashko struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 202193a76530SGrygorii Strashko 202284b4aa49SGrygorii Strashko netif_tx_napi_add(common->dma_ndev, &tx_chn->napi_tx, 202393a76530SGrygorii Strashko am65_cpsw_nuss_tx_poll, NAPI_POLL_WEIGHT); 202493a76530SGrygorii Strashko 202593a76530SGrygorii Strashko ret = devm_request_irq(dev, tx_chn->irq, 202693a76530SGrygorii Strashko am65_cpsw_nuss_tx_irq, 20276f5c27f9SGrygorii Strashko IRQF_TRIGGER_HIGH, 20286f5c27f9SGrygorii Strashko tx_chn->tx_chn_name, tx_chn); 202993a76530SGrygorii Strashko if (ret) { 203093a76530SGrygorii Strashko dev_err(dev, "failure requesting tx%u irq %u, %d\n", 203193a76530SGrygorii Strashko tx_chn->id, tx_chn->irq, ret); 203293a76530SGrygorii Strashko goto err; 203393a76530SGrygorii Strashko } 203493a76530SGrygorii Strashko } 203593a76530SGrygorii Strashko 203693a76530SGrygorii Strashko err: 203793a76530SGrygorii Strashko return ret; 203893a76530SGrygorii Strashko } 203993a76530SGrygorii Strashko 204084b4aa49SGrygorii Strashko static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common) 204184b4aa49SGrygorii Strashko { 204284b4aa49SGrygorii Strashko struct am65_cpsw_port *port; 204384b4aa49SGrygorii Strashko int i; 204484b4aa49SGrygorii Strashko 204584b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 204684b4aa49SGrygorii Strashko port = &common->ports[i]; 204784b4aa49SGrygorii Strashko if (port->ndev) 204884b4aa49SGrygorii Strashko unregister_netdev(port->ndev); 204984b4aa49SGrygorii Strashko } 205084b4aa49SGrygorii Strashko } 205184b4aa49SGrygorii Strashko 20522934db9bSVignesh Raghavendra static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common) 20532934db9bSVignesh Raghavendra { 20542934db9bSVignesh Raghavendra int set_val = 0; 20552934db9bSVignesh Raghavendra int i; 20562934db9bSVignesh Raghavendra 20572934db9bSVignesh Raghavendra if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask)) 20582934db9bSVignesh Raghavendra set_val = 1; 20592934db9bSVignesh Raghavendra 20602934db9bSVignesh Raghavendra dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val); 20612934db9bSVignesh Raghavendra 20622934db9bSVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 20632934db9bSVignesh Raghavendra struct am65_cpsw_port *port = am65_common_get_port(common, i); 20642934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(port->ndev); 20652934db9bSVignesh Raghavendra 20662934db9bSVignesh Raghavendra priv->offload_fwd_mark = set_val; 20672934db9bSVignesh Raghavendra } 20682934db9bSVignesh Raghavendra } 20692934db9bSVignesh Raghavendra 20702934db9bSVignesh Raghavendra bool am65_cpsw_port_dev_check(const struct net_device *ndev) 20712934db9bSVignesh Raghavendra { 20722934db9bSVignesh Raghavendra if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) { 20732934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 20742934db9bSVignesh Raghavendra 20752934db9bSVignesh Raghavendra return !common->is_emac_mode; 20762934db9bSVignesh Raghavendra } 20772934db9bSVignesh Raghavendra 20782934db9bSVignesh Raghavendra return false; 20792934db9bSVignesh Raghavendra } 20802934db9bSVignesh Raghavendra 20812f5dc00fSVladimir Oltean static int am65_cpsw_netdevice_port_link(struct net_device *ndev, 20822f5dc00fSVladimir Oltean struct net_device *br_ndev, 20832f5dc00fSVladimir Oltean struct netlink_ext_ack *extack) 20842934db9bSVignesh Raghavendra { 20852934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 20862934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 20872f5dc00fSVladimir Oltean int err; 20882934db9bSVignesh Raghavendra 20892934db9bSVignesh Raghavendra if (!common->br_members) { 20902934db9bSVignesh Raghavendra common->hw_bridge_dev = br_ndev; 20912934db9bSVignesh Raghavendra } else { 20922934db9bSVignesh Raghavendra /* This is adding the port to a second bridge, this is 20932934db9bSVignesh Raghavendra * unsupported 20942934db9bSVignesh Raghavendra */ 20952934db9bSVignesh Raghavendra if (common->hw_bridge_dev != br_ndev) 20962934db9bSVignesh Raghavendra return -EOPNOTSUPP; 20972934db9bSVignesh Raghavendra } 20982934db9bSVignesh Raghavendra 20994e51bf44SVladimir Oltean err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL, 210047211192STobias Waldekranz false, extack); 21012f5dc00fSVladimir Oltean if (err) 21022f5dc00fSVladimir Oltean return err; 21032f5dc00fSVladimir Oltean 21042934db9bSVignesh Raghavendra common->br_members |= BIT(priv->port->port_id); 21052934db9bSVignesh Raghavendra 21062934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 21072934db9bSVignesh Raghavendra 21082934db9bSVignesh Raghavendra return NOTIFY_DONE; 21092934db9bSVignesh Raghavendra } 21102934db9bSVignesh Raghavendra 21112934db9bSVignesh Raghavendra static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev) 21122934db9bSVignesh Raghavendra { 21132934db9bSVignesh Raghavendra struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 21142934db9bSVignesh Raghavendra struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 21152934db9bSVignesh Raghavendra 21164e51bf44SVladimir Oltean switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL); 21172f5dc00fSVladimir Oltean 21182934db9bSVignesh Raghavendra common->br_members &= ~BIT(priv->port->port_id); 21192934db9bSVignesh Raghavendra 21202934db9bSVignesh Raghavendra am65_cpsw_port_offload_fwd_mark_update(common); 21212934db9bSVignesh Raghavendra 21222934db9bSVignesh Raghavendra if (!common->br_members) 21232934db9bSVignesh Raghavendra common->hw_bridge_dev = NULL; 21242934db9bSVignesh Raghavendra } 21252934db9bSVignesh Raghavendra 21262934db9bSVignesh Raghavendra /* netdev notifier */ 21272934db9bSVignesh Raghavendra static int am65_cpsw_netdevice_event(struct notifier_block *unused, 21282934db9bSVignesh Raghavendra unsigned long event, void *ptr) 21292934db9bSVignesh Raghavendra { 21302f5dc00fSVladimir Oltean struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 21312934db9bSVignesh Raghavendra struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 21322934db9bSVignesh Raghavendra struct netdev_notifier_changeupper_info *info; 21332934db9bSVignesh Raghavendra int ret = NOTIFY_DONE; 21342934db9bSVignesh Raghavendra 21352934db9bSVignesh Raghavendra if (!am65_cpsw_port_dev_check(ndev)) 21362934db9bSVignesh Raghavendra return NOTIFY_DONE; 21372934db9bSVignesh Raghavendra 21382934db9bSVignesh Raghavendra switch (event) { 21392934db9bSVignesh Raghavendra case NETDEV_CHANGEUPPER: 21402934db9bSVignesh Raghavendra info = ptr; 21412934db9bSVignesh Raghavendra 21422934db9bSVignesh Raghavendra if (netif_is_bridge_master(info->upper_dev)) { 21432934db9bSVignesh Raghavendra if (info->linking) 21442f5dc00fSVladimir Oltean ret = am65_cpsw_netdevice_port_link(ndev, 21452f5dc00fSVladimir Oltean info->upper_dev, 21462f5dc00fSVladimir Oltean extack); 21472934db9bSVignesh Raghavendra else 21482934db9bSVignesh Raghavendra am65_cpsw_netdevice_port_unlink(ndev); 21492934db9bSVignesh Raghavendra } 21502934db9bSVignesh Raghavendra break; 21512934db9bSVignesh Raghavendra default: 21522934db9bSVignesh Raghavendra return NOTIFY_DONE; 21532934db9bSVignesh Raghavendra } 21542934db9bSVignesh Raghavendra 21552934db9bSVignesh Raghavendra return notifier_from_errno(ret); 21562934db9bSVignesh Raghavendra } 21572934db9bSVignesh Raghavendra 21582934db9bSVignesh Raghavendra static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw) 21592934db9bSVignesh Raghavendra { 21602934db9bSVignesh Raghavendra int ret = 0; 21612934db9bSVignesh Raghavendra 21622934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 21632934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 21642934db9bSVignesh Raghavendra return 0; 21652934db9bSVignesh Raghavendra 21662934db9bSVignesh Raghavendra cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event; 21672934db9bSVignesh Raghavendra ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 21682934db9bSVignesh Raghavendra if (ret) { 21692934db9bSVignesh Raghavendra dev_err(cpsw->dev, "can't register netdevice notifier\n"); 21702934db9bSVignesh Raghavendra return ret; 21712934db9bSVignesh Raghavendra } 21722934db9bSVignesh Raghavendra 217386e8b070SVignesh Raghavendra ret = am65_cpsw_switchdev_register_notifiers(cpsw); 217486e8b070SVignesh Raghavendra if (ret) 217586e8b070SVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 217686e8b070SVignesh Raghavendra 21772934db9bSVignesh Raghavendra return ret; 21782934db9bSVignesh Raghavendra } 21792934db9bSVignesh Raghavendra 21802934db9bSVignesh Raghavendra static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw) 21812934db9bSVignesh Raghavendra { 21822934db9bSVignesh Raghavendra if (AM65_CPSW_IS_CPSW2G(cpsw) || 21832934db9bSVignesh Raghavendra !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 21842934db9bSVignesh Raghavendra return; 21852934db9bSVignesh Raghavendra 218686e8b070SVignesh Raghavendra am65_cpsw_switchdev_unregister_notifiers(cpsw); 21872934db9bSVignesh Raghavendra unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 21882934db9bSVignesh Raghavendra } 21892934db9bSVignesh Raghavendra 219058356eb3SVignesh Raghavendra static const struct devlink_ops am65_cpsw_devlink_ops = {}; 219158356eb3SVignesh Raghavendra 219258356eb3SVignesh Raghavendra static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw) 219358356eb3SVignesh Raghavendra { 219458356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0, 219558356eb3SVignesh Raghavendra ALE_MCAST_BLOCK_LEARN_FWD); 219658356eb3SVignesh Raghavendra } 219758356eb3SVignesh Raghavendra 219858356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common) 219958356eb3SVignesh Raghavendra { 220058356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 220158356eb3SVignesh Raghavendra 220258356eb3SVignesh Raghavendra writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 220358356eb3SVignesh Raghavendra 220458356eb3SVignesh Raghavendra am65_cpsw_init_stp_ale_entry(common); 220558356eb3SVignesh Raghavendra 220658356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1); 220758356eb3SVignesh Raghavendra dev_dbg(common->dev, "Set P0_UNI_FLOOD\n"); 220858356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0); 220958356eb3SVignesh Raghavendra } 221058356eb3SVignesh Raghavendra 221158356eb3SVignesh Raghavendra static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common) 221258356eb3SVignesh Raghavendra { 221358356eb3SVignesh Raghavendra struct am65_cpsw_host *host = am65_common_get_host(common); 221458356eb3SVignesh Raghavendra 221558356eb3SVignesh Raghavendra writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 221658356eb3SVignesh Raghavendra 221758356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0); 221858356eb3SVignesh Raghavendra dev_dbg(common->dev, "unset P0_UNI_FLOOD\n"); 221958356eb3SVignesh Raghavendra 222058356eb3SVignesh Raghavendra /* learning make no sense in multi-mac mode */ 222158356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1); 222258356eb3SVignesh Raghavendra } 222358356eb3SVignesh Raghavendra 222458356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id, 222558356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 222658356eb3SVignesh Raghavendra { 222758356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 222858356eb3SVignesh Raghavendra struct am65_cpsw_common *common = dl_priv->common; 222958356eb3SVignesh Raghavendra 223058356eb3SVignesh Raghavendra dev_dbg(common->dev, "%s id:%u\n", __func__, id); 223158356eb3SVignesh Raghavendra 223258356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 223358356eb3SVignesh Raghavendra return -EOPNOTSUPP; 223458356eb3SVignesh Raghavendra 223558356eb3SVignesh Raghavendra ctx->val.vbool = !common->is_emac_mode; 223658356eb3SVignesh Raghavendra 223758356eb3SVignesh Raghavendra return 0; 223858356eb3SVignesh Raghavendra } 223958356eb3SVignesh Raghavendra 224058356eb3SVignesh Raghavendra static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port) 224158356eb3SVignesh Raghavendra { 224258356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 224358356eb3SVignesh Raghavendra struct am65_cpsw_common *common = port->common; 224458356eb3SVignesh Raghavendra u32 port_mask; 224558356eb3SVignesh Raghavendra 224658356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 224758356eb3SVignesh Raghavendra 224858356eb3SVignesh Raghavendra if (slave->mac_only) 224958356eb3SVignesh Raghavendra /* enable mac-only mode on port */ 225058356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, 225158356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 1); 225258356eb3SVignesh Raghavendra 225358356eb3SVignesh Raghavendra cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1); 225458356eb3SVignesh Raghavendra 225558356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 225658356eb3SVignesh Raghavendra 225758356eb3SVignesh Raghavendra cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr, 225858356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE, slave->port_vlan); 225958356eb3SVignesh Raghavendra cpsw_ale_add_mcast(common->ale, port->ndev->broadcast, 226058356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2); 226158356eb3SVignesh Raghavendra } 226258356eb3SVignesh Raghavendra 226358356eb3SVignesh Raghavendra static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port) 226458356eb3SVignesh Raghavendra { 226558356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave = &port->slave; 226658356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = port->common; 226758356eb3SVignesh Raghavendra u32 port_mask; 226858356eb3SVignesh Raghavendra 226958356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 227058356eb3SVignesh Raghavendra ALE_PORT_NOLEARN, 0); 227158356eb3SVignesh Raghavendra 227258356eb3SVignesh Raghavendra cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr, 227358356eb3SVignesh Raghavendra HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN, 227458356eb3SVignesh Raghavendra slave->port_vlan); 227558356eb3SVignesh Raghavendra 227658356eb3SVignesh Raghavendra port_mask = BIT(port->port_id) | ALE_PORT_HOST; 227758356eb3SVignesh Raghavendra 227858356eb3SVignesh Raghavendra cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast, 227958356eb3SVignesh Raghavendra port_mask, ALE_VLAN, slave->port_vlan, 228058356eb3SVignesh Raghavendra ALE_MCAST_FWD_2); 228158356eb3SVignesh Raghavendra 228258356eb3SVignesh Raghavendra writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 228358356eb3SVignesh Raghavendra 228458356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, port->port_id, 228558356eb3SVignesh Raghavendra ALE_PORT_MACONLY, 0); 228658356eb3SVignesh Raghavendra } 228758356eb3SVignesh Raghavendra 228858356eb3SVignesh Raghavendra static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id, 228958356eb3SVignesh Raghavendra struct devlink_param_gset_ctx *ctx) 229058356eb3SVignesh Raghavendra { 229158356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 229258356eb3SVignesh Raghavendra struct am65_cpsw_common *cpsw = dl_priv->common; 229358356eb3SVignesh Raghavendra bool switch_en = ctx->val.vbool; 229458356eb3SVignesh Raghavendra bool if_running = false; 229558356eb3SVignesh Raghavendra int i; 229658356eb3SVignesh Raghavendra 229758356eb3SVignesh Raghavendra dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 229858356eb3SVignesh Raghavendra 229958356eb3SVignesh Raghavendra if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 230058356eb3SVignesh Raghavendra return -EOPNOTSUPP; 230158356eb3SVignesh Raghavendra 230258356eb3SVignesh Raghavendra if (switch_en == !cpsw->is_emac_mode) 230358356eb3SVignesh Raghavendra return 0; 230458356eb3SVignesh Raghavendra 230558356eb3SVignesh Raghavendra if (!switch_en && cpsw->br_members) { 230658356eb3SVignesh Raghavendra dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n"); 230758356eb3SVignesh Raghavendra return -EINVAL; 230858356eb3SVignesh Raghavendra } 230958356eb3SVignesh Raghavendra 231058356eb3SVignesh Raghavendra rtnl_lock(); 231158356eb3SVignesh Raghavendra 231258356eb3SVignesh Raghavendra cpsw->is_emac_mode = !switch_en; 231358356eb3SVignesh Raghavendra 231458356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 231558356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 231658356eb3SVignesh Raghavendra 231758356eb3SVignesh Raghavendra if (!sl_ndev || !netif_running(sl_ndev)) 231858356eb3SVignesh Raghavendra continue; 231958356eb3SVignesh Raghavendra 232058356eb3SVignesh Raghavendra if_running = true; 232158356eb3SVignesh Raghavendra } 232258356eb3SVignesh Raghavendra 232358356eb3SVignesh Raghavendra if (!if_running) { 232458356eb3SVignesh Raghavendra /* all ndevs are down */ 232558356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 232658356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 232758356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 232858356eb3SVignesh Raghavendra 232958356eb3SVignesh Raghavendra if (!sl_ndev) 233058356eb3SVignesh Raghavendra continue; 233158356eb3SVignesh Raghavendra 233258356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 233358356eb3SVignesh Raghavendra if (switch_en) 233458356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 233558356eb3SVignesh Raghavendra else 233658356eb3SVignesh Raghavendra slave->port_vlan = 0; 233758356eb3SVignesh Raghavendra } 233858356eb3SVignesh Raghavendra 233958356eb3SVignesh Raghavendra goto exit; 234058356eb3SVignesh Raghavendra } 234158356eb3SVignesh Raghavendra 234258356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 234358356eb3SVignesh Raghavendra /* clean up ALE table */ 234458356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1); 234558356eb3SVignesh Raghavendra cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT); 234658356eb3SVignesh Raghavendra 234758356eb3SVignesh Raghavendra if (switch_en) { 234858356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Enable switch mode\n"); 234958356eb3SVignesh Raghavendra 235058356eb3SVignesh Raghavendra am65_cpsw_init_host_port_switch(cpsw); 235158356eb3SVignesh Raghavendra 235258356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 235358356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 235458356eb3SVignesh Raghavendra struct am65_cpsw_slave_data *slave; 235558356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 235658356eb3SVignesh Raghavendra 235758356eb3SVignesh Raghavendra if (!sl_ndev) 235858356eb3SVignesh Raghavendra continue; 235958356eb3SVignesh Raghavendra 236058356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 236158356eb3SVignesh Raghavendra slave = am65_ndev_to_slave(sl_ndev); 236258356eb3SVignesh Raghavendra slave->port_vlan = cpsw->default_vlan; 236358356eb3SVignesh Raghavendra 236458356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 236558356eb3SVignesh Raghavendra am65_cpsw_init_port_switch_ale(port); 236658356eb3SVignesh Raghavendra } 236758356eb3SVignesh Raghavendra 236858356eb3SVignesh Raghavendra } else { 236958356eb3SVignesh Raghavendra dev_info(cpsw->dev, "Disable switch mode\n"); 237058356eb3SVignesh Raghavendra 237158356eb3SVignesh Raghavendra am65_cpsw_init_host_port_emac(cpsw); 237258356eb3SVignesh Raghavendra 237358356eb3SVignesh Raghavendra for (i = 0; i < cpsw->port_num; i++) { 237458356eb3SVignesh Raghavendra struct net_device *sl_ndev = cpsw->ports[i].ndev; 237558356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 237658356eb3SVignesh Raghavendra 237758356eb3SVignesh Raghavendra if (!sl_ndev) 237858356eb3SVignesh Raghavendra continue; 237958356eb3SVignesh Raghavendra 238058356eb3SVignesh Raghavendra port = am65_ndev_to_port(sl_ndev); 238158356eb3SVignesh Raghavendra port->slave.port_vlan = 0; 238258356eb3SVignesh Raghavendra if (netif_running(sl_ndev)) 238358356eb3SVignesh Raghavendra am65_cpsw_init_port_emac_ale(port); 238458356eb3SVignesh Raghavendra } 238558356eb3SVignesh Raghavendra } 238658356eb3SVignesh Raghavendra cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0); 238758356eb3SVignesh Raghavendra exit: 238858356eb3SVignesh Raghavendra rtnl_unlock(); 238958356eb3SVignesh Raghavendra 239058356eb3SVignesh Raghavendra return 0; 239158356eb3SVignesh Raghavendra } 239258356eb3SVignesh Raghavendra 239358356eb3SVignesh Raghavendra static const struct devlink_param am65_cpsw_devlink_params[] = { 239458356eb3SVignesh Raghavendra DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode", 239558356eb3SVignesh Raghavendra DEVLINK_PARAM_TYPE_BOOL, 239658356eb3SVignesh Raghavendra BIT(DEVLINK_PARAM_CMODE_RUNTIME), 239758356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_get, 239858356eb3SVignesh Raghavendra am65_cpsw_dl_switch_mode_set, NULL), 239958356eb3SVignesh Raghavendra }; 240058356eb3SVignesh Raghavendra 240158356eb3SVignesh Raghavendra static void am65_cpsw_unregister_devlink_ports(struct am65_cpsw_common *common) 240258356eb3SVignesh Raghavendra { 240358356eb3SVignesh Raghavendra struct devlink_port *dl_port; 240458356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 240558356eb3SVignesh Raghavendra int i; 240658356eb3SVignesh Raghavendra 240758356eb3SVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 240858356eb3SVignesh Raghavendra port = am65_common_get_port(common, i); 240958356eb3SVignesh Raghavendra dl_port = &port->devlink_port; 241058356eb3SVignesh Raghavendra 241158356eb3SVignesh Raghavendra if (dl_port->registered) 241258356eb3SVignesh Raghavendra devlink_port_unregister(dl_port); 241358356eb3SVignesh Raghavendra } 241458356eb3SVignesh Raghavendra } 241558356eb3SVignesh Raghavendra 241658356eb3SVignesh Raghavendra static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common) 241758356eb3SVignesh Raghavendra { 241858356eb3SVignesh Raghavendra struct devlink_port_attrs attrs = {}; 241958356eb3SVignesh Raghavendra struct am65_cpsw_devlink *dl_priv; 242058356eb3SVignesh Raghavendra struct device *dev = common->dev; 242158356eb3SVignesh Raghavendra struct devlink_port *dl_port; 242258356eb3SVignesh Raghavendra struct am65_cpsw_port *port; 242358356eb3SVignesh Raghavendra int ret = 0; 242458356eb3SVignesh Raghavendra int i; 242558356eb3SVignesh Raghavendra 242658356eb3SVignesh Raghavendra common->devlink = 242758356eb3SVignesh Raghavendra devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv)); 242858356eb3SVignesh Raghavendra if (!common->devlink) 242958356eb3SVignesh Raghavendra return -ENOMEM; 243058356eb3SVignesh Raghavendra 243158356eb3SVignesh Raghavendra dl_priv = devlink_priv(common->devlink); 243258356eb3SVignesh Raghavendra dl_priv->common = common; 243358356eb3SVignesh Raghavendra 243458356eb3SVignesh Raghavendra ret = devlink_register(common->devlink, dev); 243558356eb3SVignesh Raghavendra if (ret) { 243658356eb3SVignesh Raghavendra dev_err(dev, "devlink reg fail ret:%d\n", ret); 243758356eb3SVignesh Raghavendra goto dl_free; 243858356eb3SVignesh Raghavendra } 243958356eb3SVignesh Raghavendra 244058356eb3SVignesh Raghavendra /* Provide devlink hook to switch mode when multiple external ports 244158356eb3SVignesh Raghavendra * are present NUSS switchdev driver is enabled. 244258356eb3SVignesh Raghavendra */ 244358356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 244458356eb3SVignesh Raghavendra IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) { 244558356eb3SVignesh Raghavendra ret = devlink_params_register(common->devlink, 244658356eb3SVignesh Raghavendra am65_cpsw_devlink_params, 244758356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 244858356eb3SVignesh Raghavendra if (ret) { 244958356eb3SVignesh Raghavendra dev_err(dev, "devlink params reg fail ret:%d\n", ret); 245058356eb3SVignesh Raghavendra goto dl_unreg; 245158356eb3SVignesh Raghavendra } 245258356eb3SVignesh Raghavendra devlink_params_publish(common->devlink); 245358356eb3SVignesh Raghavendra } 245458356eb3SVignesh Raghavendra 245558356eb3SVignesh Raghavendra for (i = 1; i <= common->port_num; i++) { 245658356eb3SVignesh Raghavendra port = am65_common_get_port(common, i); 245758356eb3SVignesh Raghavendra dl_port = &port->devlink_port; 245858356eb3SVignesh Raghavendra 245958356eb3SVignesh Raghavendra attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 246058356eb3SVignesh Raghavendra attrs.phys.port_number = port->port_id; 246158356eb3SVignesh Raghavendra attrs.switch_id.id_len = sizeof(resource_size_t); 246258356eb3SVignesh Raghavendra memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len); 246358356eb3SVignesh Raghavendra devlink_port_attrs_set(dl_port, &attrs); 246458356eb3SVignesh Raghavendra 246558356eb3SVignesh Raghavendra ret = devlink_port_register(common->devlink, dl_port, port->port_id); 246658356eb3SVignesh Raghavendra if (ret) { 246758356eb3SVignesh Raghavendra dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n", 246858356eb3SVignesh Raghavendra port->port_id, ret); 246958356eb3SVignesh Raghavendra goto dl_port_unreg; 247058356eb3SVignesh Raghavendra } 247158356eb3SVignesh Raghavendra devlink_port_type_eth_set(dl_port, port->ndev); 247258356eb3SVignesh Raghavendra } 247358356eb3SVignesh Raghavendra 247458356eb3SVignesh Raghavendra return ret; 247558356eb3SVignesh Raghavendra 247658356eb3SVignesh Raghavendra dl_port_unreg: 247758356eb3SVignesh Raghavendra am65_cpsw_unregister_devlink_ports(common); 247858356eb3SVignesh Raghavendra dl_unreg: 247958356eb3SVignesh Raghavendra devlink_unregister(common->devlink); 248058356eb3SVignesh Raghavendra dl_free: 248158356eb3SVignesh Raghavendra devlink_free(common->devlink); 248258356eb3SVignesh Raghavendra 248358356eb3SVignesh Raghavendra return ret; 248458356eb3SVignesh Raghavendra } 248558356eb3SVignesh Raghavendra 248658356eb3SVignesh Raghavendra static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common) 248758356eb3SVignesh Raghavendra { 248858356eb3SVignesh Raghavendra if (!AM65_CPSW_IS_CPSW2G(common) && 248958356eb3SVignesh Raghavendra IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) { 249058356eb3SVignesh Raghavendra devlink_params_unpublish(common->devlink); 249158356eb3SVignesh Raghavendra devlink_params_unregister(common->devlink, am65_cpsw_devlink_params, 249258356eb3SVignesh Raghavendra ARRAY_SIZE(am65_cpsw_devlink_params)); 249358356eb3SVignesh Raghavendra } 249458356eb3SVignesh Raghavendra 249558356eb3SVignesh Raghavendra am65_cpsw_unregister_devlink_ports(common); 249658356eb3SVignesh Raghavendra devlink_unregister(common->devlink); 249758356eb3SVignesh Raghavendra devlink_free(common->devlink); 249858356eb3SVignesh Raghavendra } 249958356eb3SVignesh Raghavendra 250084b4aa49SGrygorii Strashko static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common) 250193a76530SGrygorii Strashko { 250293a76530SGrygorii Strashko struct device *dev = common->dev; 250393a76530SGrygorii Strashko struct am65_cpsw_port *port; 250484b4aa49SGrygorii Strashko int ret = 0, i; 250593a76530SGrygorii Strashko 250684b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_ndev_add_tx_napi(common); 250793a76530SGrygorii Strashko if (ret) 250884b4aa49SGrygorii Strashko return ret; 250993a76530SGrygorii Strashko 251093a76530SGrygorii Strashko ret = devm_request_irq(dev, common->rx_chns.irq, 251193a76530SGrygorii Strashko am65_cpsw_nuss_rx_irq, 25126f5c27f9SGrygorii Strashko IRQF_TRIGGER_HIGH, dev_name(dev), common); 251393a76530SGrygorii Strashko if (ret) { 251493a76530SGrygorii Strashko dev_err(dev, "failure requesting rx irq %u, %d\n", 251593a76530SGrygorii Strashko common->rx_chns.irq, ret); 251684b4aa49SGrygorii Strashko return ret; 251793a76530SGrygorii Strashko } 251893a76530SGrygorii Strashko 251984b4aa49SGrygorii Strashko for (i = 0; i < common->port_num; i++) { 252084b4aa49SGrygorii Strashko port = &common->ports[i]; 252184b4aa49SGrygorii Strashko 252284b4aa49SGrygorii Strashko if (!port->ndev) 252384b4aa49SGrygorii Strashko continue; 252484b4aa49SGrygorii Strashko 252593a76530SGrygorii Strashko ret = register_netdev(port->ndev); 252684b4aa49SGrygorii Strashko if (ret) { 252784b4aa49SGrygorii Strashko dev_err(dev, "error registering slave net device%i %d\n", 252884b4aa49SGrygorii Strashko i, ret); 252984b4aa49SGrygorii Strashko goto err_cleanup_ndev; 253084b4aa49SGrygorii Strashko } 253184b4aa49SGrygorii Strashko } 253284b4aa49SGrygorii Strashko 25332934db9bSVignesh Raghavendra ret = am65_cpsw_register_notifiers(common); 253458356eb3SVignesh Raghavendra if (ret) 253558356eb3SVignesh Raghavendra goto err_cleanup_ndev; 253693a76530SGrygorii Strashko 25372934db9bSVignesh Raghavendra ret = am65_cpsw_nuss_register_devlink(common); 25382934db9bSVignesh Raghavendra if (ret) 25392934db9bSVignesh Raghavendra goto clean_unregister_notifiers; 25402934db9bSVignesh Raghavendra 254193a76530SGrygorii Strashko /* can't auto unregister ndev using devm_add_action() due to 254293a76530SGrygorii Strashko * devres release sequence in DD core for DMA 254393a76530SGrygorii Strashko */ 254484b4aa49SGrygorii Strashko 254558356eb3SVignesh Raghavendra return 0; 25462934db9bSVignesh Raghavendra clean_unregister_notifiers: 25472934db9bSVignesh Raghavendra am65_cpsw_unregister_notifiers(common); 254884b4aa49SGrygorii Strashko err_cleanup_ndev: 254984b4aa49SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 25502934db9bSVignesh Raghavendra 255193a76530SGrygorii Strashko return ret; 255293a76530SGrygorii Strashko } 255393a76530SGrygorii Strashko 255493a76530SGrygorii Strashko int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx) 255593a76530SGrygorii Strashko { 255693a76530SGrygorii Strashko int ret; 255793a76530SGrygorii Strashko 255893a76530SGrygorii Strashko common->tx_ch_num = num_tx; 255993a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_tx_chns(common); 256093a76530SGrygorii Strashko if (ret) 256193a76530SGrygorii Strashko return ret; 256293a76530SGrygorii Strashko 256384b4aa49SGrygorii Strashko return am65_cpsw_nuss_ndev_add_tx_napi(common); 256493a76530SGrygorii Strashko } 256593a76530SGrygorii Strashko 256638389aa6SGrygorii Strashko struct am65_cpsw_soc_pdata { 256738389aa6SGrygorii Strashko u32 quirks_dis; 256838389aa6SGrygorii Strashko }; 256938389aa6SGrygorii Strashko 257038389aa6SGrygorii Strashko static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = { 257138389aa6SGrygorii Strashko .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 257238389aa6SGrygorii Strashko }; 257338389aa6SGrygorii Strashko 257438389aa6SGrygorii Strashko static const struct soc_device_attribute am65_cpsw_socinfo[] = { 257538389aa6SGrygorii Strashko { .family = "AM65X", 257638389aa6SGrygorii Strashko .revision = "SR2.0", 257738389aa6SGrygorii Strashko .data = &am65x_soc_sr2_0 257838389aa6SGrygorii Strashko }, 257938389aa6SGrygorii Strashko {/* sentinel */} 258038389aa6SGrygorii Strashko }; 258138389aa6SGrygorii Strashko 258293a76530SGrygorii Strashko static const struct am65_cpsw_pdata am65x_sr1_0 = { 258393a76530SGrygorii Strashko .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 25847747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2585c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 258693a76530SGrygorii Strashko }; 258793a76530SGrygorii Strashko 258838389aa6SGrygorii Strashko static const struct am65_cpsw_pdata j721e_pdata = { 258993a76530SGrygorii Strashko .quirks = 0, 25907747d4b7SGrygorii Strashko .ale_dev_id = "am65x-cpsw2g", 2591c6275c02SGrygorii Strashko .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 259293a76530SGrygorii Strashko }; 259393a76530SGrygorii Strashko 25944f7cce27SVignesh Raghavendra static const struct am65_cpsw_pdata am64x_cpswxg_pdata = { 25954f7cce27SVignesh Raghavendra .quirks = 0, 25964f7cce27SVignesh Raghavendra .ale_dev_id = "am64-cpswxg", 25974f7cce27SVignesh Raghavendra .fdqring_mode = K3_RINGACC_RING_MODE_RING, 25984f7cce27SVignesh Raghavendra }; 25994f7cce27SVignesh Raghavendra 260093a76530SGrygorii Strashko static const struct of_device_id am65_cpsw_nuss_of_mtable[] = { 260193a76530SGrygorii Strashko { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0}, 260238389aa6SGrygorii Strashko { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata}, 26034f7cce27SVignesh Raghavendra { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata}, 260493a76530SGrygorii Strashko { /* sentinel */ }, 260593a76530SGrygorii Strashko }; 260693a76530SGrygorii Strashko MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable); 260793a76530SGrygorii Strashko 260838389aa6SGrygorii Strashko static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common) 260938389aa6SGrygorii Strashko { 261038389aa6SGrygorii Strashko const struct soc_device_attribute *soc; 261138389aa6SGrygorii Strashko 261238389aa6SGrygorii Strashko soc = soc_device_match(am65_cpsw_socinfo); 261338389aa6SGrygorii Strashko if (soc && soc->data) { 261438389aa6SGrygorii Strashko const struct am65_cpsw_soc_pdata *socdata = soc->data; 261538389aa6SGrygorii Strashko 261638389aa6SGrygorii Strashko /* disable quirks */ 261738389aa6SGrygorii Strashko common->pdata.quirks &= ~socdata->quirks_dis; 261838389aa6SGrygorii Strashko } 261938389aa6SGrygorii Strashko } 262038389aa6SGrygorii Strashko 262193a76530SGrygorii Strashko static int am65_cpsw_nuss_probe(struct platform_device *pdev) 262293a76530SGrygorii Strashko { 26232074f9eaSGrygorii Strashko struct cpsw_ale_params ale_params = { 0 }; 262493a76530SGrygorii Strashko const struct of_device_id *of_id; 262593a76530SGrygorii Strashko struct device *dev = &pdev->dev; 262693a76530SGrygorii Strashko struct am65_cpsw_common *common; 262793a76530SGrygorii Strashko struct device_node *node; 262893a76530SGrygorii Strashko struct resource *res; 262923015ff1SGrygorii Strashko struct clk *clk; 263058356eb3SVignesh Raghavendra u64 id_temp; 263193a76530SGrygorii Strashko int ret, i; 263293a76530SGrygorii Strashko 263393a76530SGrygorii Strashko common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL); 263493a76530SGrygorii Strashko if (!common) 263593a76530SGrygorii Strashko return -ENOMEM; 263693a76530SGrygorii Strashko common->dev = dev; 263793a76530SGrygorii Strashko 263893a76530SGrygorii Strashko of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev); 263993a76530SGrygorii Strashko if (!of_id) 264093a76530SGrygorii Strashko return -EINVAL; 264138389aa6SGrygorii Strashko common->pdata = *(const struct am65_cpsw_pdata *)of_id->data; 264238389aa6SGrygorii Strashko 264338389aa6SGrygorii Strashko am65_cpsw_nuss_apply_socinfo(common); 264493a76530SGrygorii Strashko 264593a76530SGrygorii Strashko res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss"); 264693a76530SGrygorii Strashko common->ss_base = devm_ioremap_resource(&pdev->dev, res); 264793a76530SGrygorii Strashko if (IS_ERR(common->ss_base)) 264893a76530SGrygorii Strashko return PTR_ERR(common->ss_base); 264993a76530SGrygorii Strashko common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE; 265058356eb3SVignesh Raghavendra /* Use device's physical base address as switch id */ 265158356eb3SVignesh Raghavendra id_temp = cpu_to_be64(res->start); 265258356eb3SVignesh Raghavendra memcpy(common->switch_id, &id_temp, sizeof(res->start)); 265393a76530SGrygorii Strashko 265493a76530SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 265593a76530SGrygorii Strashko if (!node) 265693a76530SGrygorii Strashko return -ENOENT; 265793a76530SGrygorii Strashko common->port_num = of_get_child_count(node); 265893a76530SGrygorii Strashko if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS) 265993a76530SGrygorii Strashko return -ENOENT; 266093a76530SGrygorii Strashko of_node_put(node); 266193a76530SGrygorii Strashko 266293a76530SGrygorii Strashko common->rx_flow_id_base = -1; 266393a76530SGrygorii Strashko init_completion(&common->tdown_complete); 266493a76530SGrygorii Strashko common->tx_ch_num = 1; 266551824048SGrygorii Strashko common->pf_p0_rx_ptype_rrobin = false; 266658356eb3SVignesh Raghavendra common->default_vlan = 1; 266793a76530SGrygorii Strashko 266893a76530SGrygorii Strashko common->ports = devm_kcalloc(dev, common->port_num, 266993a76530SGrygorii Strashko sizeof(*common->ports), 267093a76530SGrygorii Strashko GFP_KERNEL); 267193a76530SGrygorii Strashko if (!common->ports) 267293a76530SGrygorii Strashko return -ENOMEM; 267393a76530SGrygorii Strashko 267423015ff1SGrygorii Strashko clk = devm_clk_get(dev, "fck"); 26758fbc2f9eSGrygorii Strashko if (IS_ERR(clk)) 26768fbc2f9eSGrygorii Strashko return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n"); 267723015ff1SGrygorii Strashko common->bus_freq = clk_get_rate(clk); 267823015ff1SGrygorii Strashko 267993a76530SGrygorii Strashko pm_runtime_enable(dev); 268093a76530SGrygorii Strashko ret = pm_runtime_get_sync(dev); 268193a76530SGrygorii Strashko if (ret < 0) { 268293a76530SGrygorii Strashko pm_runtime_put_noidle(dev); 268393a76530SGrygorii Strashko pm_runtime_disable(dev); 268493a76530SGrygorii Strashko return ret; 268593a76530SGrygorii Strashko } 268693a76530SGrygorii Strashko 2687a45cfcc6SGrygorii Strashko node = of_get_child_by_name(dev->of_node, "mdio"); 2688a45cfcc6SGrygorii Strashko if (!node) { 2689a45cfcc6SGrygorii Strashko dev_warn(dev, "MDIO node not found\n"); 2690a45cfcc6SGrygorii Strashko } else if (of_device_is_available(node)) { 2691a45cfcc6SGrygorii Strashko struct platform_device *mdio_pdev; 2692a45cfcc6SGrygorii Strashko 2693a45cfcc6SGrygorii Strashko mdio_pdev = of_platform_device_create(node, NULL, dev); 2694a45cfcc6SGrygorii Strashko if (!mdio_pdev) { 2695a45cfcc6SGrygorii Strashko ret = -ENODEV; 2696a45cfcc6SGrygorii Strashko goto err_pm_clear; 2697a45cfcc6SGrygorii Strashko } 2698a45cfcc6SGrygorii Strashko 2699a45cfcc6SGrygorii Strashko common->mdio_dev = &mdio_pdev->dev; 2700a45cfcc6SGrygorii Strashko } 2701a45cfcc6SGrygorii Strashko of_node_put(node); 270293a76530SGrygorii Strashko 270393a76530SGrygorii Strashko am65_cpsw_nuss_get_ver(common); 270493a76530SGrygorii Strashko 270593a76530SGrygorii Strashko /* init tx channels */ 270693a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_tx_chns(common); 270793a76530SGrygorii Strashko if (ret) 270893a76530SGrygorii Strashko goto err_of_clear; 270993a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_rx_chns(common); 271093a76530SGrygorii Strashko if (ret) 271193a76530SGrygorii Strashko goto err_of_clear; 271293a76530SGrygorii Strashko 271393a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_host_p(common); 271493a76530SGrygorii Strashko if (ret) 271593a76530SGrygorii Strashko goto err_of_clear; 271693a76530SGrygorii Strashko 271793a76530SGrygorii Strashko ret = am65_cpsw_nuss_init_slave_ports(common); 271893a76530SGrygorii Strashko if (ret) 271993a76530SGrygorii Strashko goto err_of_clear; 272093a76530SGrygorii Strashko 272193a76530SGrygorii Strashko /* init common data */ 272293a76530SGrygorii Strashko ale_params.dev = dev; 272393a76530SGrygorii Strashko ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT; 272493a76530SGrygorii Strashko ale_params.ale_ports = common->port_num + 1; 272593a76530SGrygorii Strashko ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE; 27267747d4b7SGrygorii Strashko ale_params.dev_id = common->pdata.ale_dev_id; 272723015ff1SGrygorii Strashko ale_params.bus_freq = common->bus_freq; 272893a76530SGrygorii Strashko 272993a76530SGrygorii Strashko common->ale = cpsw_ale_create(&ale_params); 27301401cf60SWei Yongjun if (IS_ERR(common->ale)) { 273193a76530SGrygorii Strashko dev_err(dev, "error initializing ale engine\n"); 27321401cf60SWei Yongjun ret = PTR_ERR(common->ale); 273393a76530SGrygorii Strashko goto err_of_clear; 273493a76530SGrygorii Strashko } 273593a76530SGrygorii Strashko 2736b1f66a5bSGrygorii Strashko ret = am65_cpsw_init_cpts(common); 2737b1f66a5bSGrygorii Strashko if (ret) 2738b1f66a5bSGrygorii Strashko goto err_of_clear; 2739b1f66a5bSGrygorii Strashko 274093a76530SGrygorii Strashko /* init ports */ 274193a76530SGrygorii Strashko for (i = 0; i < common->port_num; i++) 274293a76530SGrygorii Strashko am65_cpsw_nuss_slave_disable_unused(&common->ports[i]); 274393a76530SGrygorii Strashko 274493a76530SGrygorii Strashko dev_set_drvdata(dev, common); 274593a76530SGrygorii Strashko 274658356eb3SVignesh Raghavendra common->is_emac_mode = true; 274758356eb3SVignesh Raghavendra 274884b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_init_ndevs(common); 274993a76530SGrygorii Strashko if (ret) 275093a76530SGrygorii Strashko goto err_of_clear; 275193a76530SGrygorii Strashko 275284b4aa49SGrygorii Strashko ret = am65_cpsw_nuss_register_ndevs(common); 275393a76530SGrygorii Strashko if (ret) 275493a76530SGrygorii Strashko goto err_of_clear; 275593a76530SGrygorii Strashko 275693a76530SGrygorii Strashko pm_runtime_put(dev); 275793a76530SGrygorii Strashko return 0; 275893a76530SGrygorii Strashko 275993a76530SGrygorii Strashko err_of_clear: 2760a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 2761a45cfcc6SGrygorii Strashko err_pm_clear: 276293a76530SGrygorii Strashko pm_runtime_put_sync(dev); 276393a76530SGrygorii Strashko pm_runtime_disable(dev); 276493a76530SGrygorii Strashko return ret; 276593a76530SGrygorii Strashko } 276693a76530SGrygorii Strashko 276793a76530SGrygorii Strashko static int am65_cpsw_nuss_remove(struct platform_device *pdev) 276893a76530SGrygorii Strashko { 276993a76530SGrygorii Strashko struct device *dev = &pdev->dev; 277093a76530SGrygorii Strashko struct am65_cpsw_common *common; 277193a76530SGrygorii Strashko int ret; 277293a76530SGrygorii Strashko 277393a76530SGrygorii Strashko common = dev_get_drvdata(dev); 277493a76530SGrygorii Strashko 277593a76530SGrygorii Strashko ret = pm_runtime_get_sync(&pdev->dev); 277693a76530SGrygorii Strashko if (ret < 0) { 277793a76530SGrygorii Strashko pm_runtime_put_noidle(&pdev->dev); 277893a76530SGrygorii Strashko return ret; 277993a76530SGrygorii Strashko } 278093a76530SGrygorii Strashko 278158356eb3SVignesh Raghavendra am65_cpsw_unregister_devlink(common); 27822934db9bSVignesh Raghavendra am65_cpsw_unregister_notifiers(common); 278358356eb3SVignesh Raghavendra 278493a76530SGrygorii Strashko /* must unregister ndevs here because DD release_driver routine calls 278593a76530SGrygorii Strashko * dma_deconfigure(dev) before devres_release_all(dev) 278693a76530SGrygorii Strashko */ 278793a76530SGrygorii Strashko am65_cpsw_nuss_cleanup_ndev(common); 278893a76530SGrygorii Strashko 2789a45cfcc6SGrygorii Strashko of_platform_device_destroy(common->mdio_dev, NULL); 279093a76530SGrygorii Strashko 279193a76530SGrygorii Strashko pm_runtime_put_sync(&pdev->dev); 279293a76530SGrygorii Strashko pm_runtime_disable(&pdev->dev); 279393a76530SGrygorii Strashko return 0; 279493a76530SGrygorii Strashko } 279593a76530SGrygorii Strashko 279693a76530SGrygorii Strashko static struct platform_driver am65_cpsw_nuss_driver = { 279793a76530SGrygorii Strashko .driver = { 279893a76530SGrygorii Strashko .name = AM65_CPSW_DRV_NAME, 279993a76530SGrygorii Strashko .of_match_table = am65_cpsw_nuss_of_mtable, 280093a76530SGrygorii Strashko }, 280193a76530SGrygorii Strashko .probe = am65_cpsw_nuss_probe, 280293a76530SGrygorii Strashko .remove = am65_cpsw_nuss_remove, 280393a76530SGrygorii Strashko }; 280493a76530SGrygorii Strashko 280593a76530SGrygorii Strashko module_platform_driver(am65_cpsw_nuss_driver); 280693a76530SGrygorii Strashko 280793a76530SGrygorii Strashko MODULE_LICENSE("GPL v2"); 280893a76530SGrygorii Strashko MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 280993a76530SGrygorii Strashko MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver"); 2810