171947923SIoana Ciornei // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 271947923SIoana Ciornei /* Copyright 2019 NXP */ 371947923SIoana Ciornei 43264f599SCalvin Johnson #include <linux/acpi.h> 5e7026f15SColin Foster #include <linux/pcs-lynx.h> 6f978fe85SIoana Ciornei #include <linux/phy/phy.h> 73264f599SCalvin Johnson #include <linux/property.h> 83264f599SCalvin Johnson 971947923SIoana Ciornei #include "dpaa2-eth.h" 1071947923SIoana Ciornei #include "dpaa2-mac.h" 1171947923SIoana Ciornei 1271947923SIoana Ciornei #define phylink_to_dpaa2_mac(config) \ 1371947923SIoana Ciornei container_of((config), struct dpaa2_mac, phylink_config) 1471947923SIoana Ciornei 15dff95381SIoana Ciornei #define DPMAC_PROTOCOL_CHANGE_VER_MAJOR 4 16dff95381SIoana Ciornei #define DPMAC_PROTOCOL_CHANGE_VER_MINOR 8 17dff95381SIoana Ciornei 18dff95381SIoana Ciornei #define DPAA2_MAC_FEATURE_PROTOCOL_CHANGE BIT(0) 19dff95381SIoana Ciornei 20dff95381SIoana Ciornei static int dpaa2_mac_cmp_ver(struct dpaa2_mac *mac, 21dff95381SIoana Ciornei u16 ver_major, u16 ver_minor) 22dff95381SIoana Ciornei { 23dff95381SIoana Ciornei if (mac->ver_major == ver_major) 24dff95381SIoana Ciornei return mac->ver_minor - ver_minor; 25dff95381SIoana Ciornei return mac->ver_major - ver_major; 26dff95381SIoana Ciornei } 27dff95381SIoana Ciornei 28dff95381SIoana Ciornei static void dpaa2_mac_detect_features(struct dpaa2_mac *mac) 29dff95381SIoana Ciornei { 30dff95381SIoana Ciornei mac->features = 0; 31dff95381SIoana Ciornei 32dff95381SIoana Ciornei if (dpaa2_mac_cmp_ver(mac, DPMAC_PROTOCOL_CHANGE_VER_MAJOR, 33dff95381SIoana Ciornei DPMAC_PROTOCOL_CHANGE_VER_MINOR) >= 0) 34dff95381SIoana Ciornei mac->features |= DPAA2_MAC_FEATURE_PROTOCOL_CHANGE; 35dff95381SIoana Ciornei } 36dff95381SIoana Ciornei 37226df3efSIoana Ciornei static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode) 3871947923SIoana Ciornei { 39226df3efSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_NA; 40226df3efSIoana Ciornei 4171947923SIoana Ciornei switch (eth_if) { 4271947923SIoana Ciornei case DPMAC_ETH_IF_RGMII: 43226df3efSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_RGMII; 44226df3efSIoana Ciornei break; 4594ae899bSIoana Ciornei case DPMAC_ETH_IF_USXGMII: 4694ae899bSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_USXGMII; 4794ae899bSIoana Ciornei break; 4894ae899bSIoana Ciornei case DPMAC_ETH_IF_QSGMII: 4994ae899bSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_QSGMII; 5094ae899bSIoana Ciornei break; 5194ae899bSIoana Ciornei case DPMAC_ETH_IF_SGMII: 5294ae899bSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_SGMII; 5394ae899bSIoana Ciornei break; 5494ae899bSIoana Ciornei case DPMAC_ETH_IF_XFI: 5594ae899bSIoana Ciornei *if_mode = PHY_INTERFACE_MODE_10GBASER; 5694ae899bSIoana Ciornei break; 5771947923SIoana Ciornei default: 5871947923SIoana Ciornei return -EINVAL; 5971947923SIoana Ciornei } 60226df3efSIoana Ciornei 61226df3efSIoana Ciornei return 0; 6271947923SIoana Ciornei } 6371947923SIoana Ciornei 64f978fe85SIoana Ciornei static enum dpmac_eth_if dpmac_eth_if_mode(phy_interface_t if_mode) 65f978fe85SIoana Ciornei { 66f978fe85SIoana Ciornei switch (if_mode) { 67f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_RGMII: 68f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_RGMII_ID: 69f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_RGMII_RXID: 70f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_RGMII_TXID: 71f978fe85SIoana Ciornei return DPMAC_ETH_IF_RGMII; 72f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_USXGMII: 73f978fe85SIoana Ciornei return DPMAC_ETH_IF_USXGMII; 74f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_QSGMII: 75f978fe85SIoana Ciornei return DPMAC_ETH_IF_QSGMII; 76f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_SGMII: 77f978fe85SIoana Ciornei return DPMAC_ETH_IF_SGMII; 78f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_10GBASER: 79f978fe85SIoana Ciornei return DPMAC_ETH_IF_XFI; 80f978fe85SIoana Ciornei case PHY_INTERFACE_MODE_1000BASEX: 81f978fe85SIoana Ciornei return DPMAC_ETH_IF_1000BASEX; 82f978fe85SIoana Ciornei default: 83f978fe85SIoana Ciornei return DPMAC_ETH_IF_MII; 84f978fe85SIoana Ciornei } 85f978fe85SIoana Ciornei } 86f978fe85SIoana Ciornei 873264f599SCalvin Johnson static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev, 883264f599SCalvin Johnson u16 dpmac_id) 8971947923SIoana Ciornei { 905b1e38c0SRobert-Ionut Alexa struct fwnode_handle *fwnode, *parent = NULL, *child = NULL; 913264f599SCalvin Johnson struct device_node *dpmacs = NULL; 9271947923SIoana Ciornei int err; 933264f599SCalvin Johnson u32 id; 9471947923SIoana Ciornei 953264f599SCalvin Johnson fwnode = dev_fwnode(dev->parent); 963264f599SCalvin Johnson if (is_of_node(fwnode)) { 9771947923SIoana Ciornei dpmacs = of_find_node_by_name(NULL, "dpmacs"); 9871947923SIoana Ciornei if (!dpmacs) 9971947923SIoana Ciornei return NULL; 1003264f599SCalvin Johnson parent = of_fwnode_handle(dpmacs); 1013264f599SCalvin Johnson } else if (is_acpi_node(fwnode)) { 1023264f599SCalvin Johnson parent = fwnode; 1034e30e98cSIoana Ciornei } else { 1044e30e98cSIoana Ciornei /* The root dprc device didn't yet get to finalize it's probe, 1054e30e98cSIoana Ciornei * thus the fwnode field is not yet set. Defer probe if we are 1064e30e98cSIoana Ciornei * facing this situation. 1074e30e98cSIoana Ciornei */ 108*37fe9b98SSean Anderson dev_dbg(dev, "dprc not finished probing\n"); 1094e30e98cSIoana Ciornei return ERR_PTR(-EPROBE_DEFER); 1103264f599SCalvin Johnson } 11171947923SIoana Ciornei 1123264f599SCalvin Johnson fwnode_for_each_child_node(parent, child) { 1133264f599SCalvin Johnson err = -EINVAL; 1143264f599SCalvin Johnson if (is_acpi_device_node(child)) 1153264f599SCalvin Johnson err = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), &id); 1163264f599SCalvin Johnson else if (is_of_node(child)) 1173264f599SCalvin Johnson err = of_property_read_u32(to_of_node(child), "reg", &id); 11871947923SIoana Ciornei if (err) 11971947923SIoana Ciornei continue; 12071947923SIoana Ciornei 1213264f599SCalvin Johnson if (id == dpmac_id) { 12271947923SIoana Ciornei of_node_put(dpmacs); 1233264f599SCalvin Johnson return child; 1243264f599SCalvin Johnson } 1253264f599SCalvin Johnson } 1263264f599SCalvin Johnson of_node_put(dpmacs); 1273264f599SCalvin Johnson return NULL; 12871947923SIoana Ciornei } 12971947923SIoana Ciornei 1303264f599SCalvin Johnson static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node, 13171947923SIoana Ciornei struct dpmac_attr attr) 13271947923SIoana Ciornei { 1330c65b2b9SAndrew Lunn phy_interface_t if_mode; 1340c65b2b9SAndrew Lunn int err; 13571947923SIoana Ciornei 1363264f599SCalvin Johnson err = fwnode_get_phy_mode(dpmac_node); 1373264f599SCalvin Johnson if (err > 0) 1383264f599SCalvin Johnson return err; 13971947923SIoana Ciornei 140226df3efSIoana Ciornei err = phy_mode(attr.eth_if, &if_mode); 141226df3efSIoana Ciornei if (!err) 14271947923SIoana Ciornei return if_mode; 14371947923SIoana Ciornei 144226df3efSIoana Ciornei return err; 14571947923SIoana Ciornei } 14671947923SIoana Ciornei 147c592286aSRussell King (Oracle) static struct phylink_pcs *dpaa2_mac_select_pcs(struct phylink_config *config, 148c592286aSRussell King (Oracle) phy_interface_t interface) 149c592286aSRussell King (Oracle) { 150c592286aSRussell King (Oracle) struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 151c592286aSRussell King (Oracle) 152c592286aSRussell King (Oracle) return mac->pcs; 153c592286aSRussell King (Oracle) } 154c592286aSRussell King (Oracle) 15571947923SIoana Ciornei static void dpaa2_mac_config(struct phylink_config *config, unsigned int mode, 15671947923SIoana Ciornei const struct phylink_link_state *state) 15771947923SIoana Ciornei { 15871947923SIoana Ciornei struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 15971947923SIoana Ciornei struct dpmac_link_state *dpmac_state = &mac->state; 16071947923SIoana Ciornei int err; 16171947923SIoana Ciornei 16271947923SIoana Ciornei if (state->an_enabled) 16371947923SIoana Ciornei dpmac_state->options |= DPMAC_LINK_OPT_AUTONEG; 16471947923SIoana Ciornei else 16571947923SIoana Ciornei dpmac_state->options &= ~DPMAC_LINK_OPT_AUTONEG; 16671947923SIoana Ciornei 16771947923SIoana Ciornei err = dpmac_set_link_state(mac->mc_io, 0, 16871947923SIoana Ciornei mac->mc_dev->mc_handle, dpmac_state); 16971947923SIoana Ciornei if (err) 17037556a4aSRussell King netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 17137556a4aSRussell King __func__, err); 172f978fe85SIoana Ciornei 173f978fe85SIoana Ciornei if (!mac->serdes_phy) 174f978fe85SIoana Ciornei return; 175f978fe85SIoana Ciornei 176f978fe85SIoana Ciornei /* This happens only if we support changing of protocol at runtime */ 177f978fe85SIoana Ciornei err = dpmac_set_protocol(mac->mc_io, 0, mac->mc_dev->mc_handle, 178f978fe85SIoana Ciornei dpmac_eth_if_mode(state->interface)); 179f978fe85SIoana Ciornei if (err) 180f978fe85SIoana Ciornei netdev_err(mac->net_dev, "dpmac_set_protocol() = %d\n", err); 181f978fe85SIoana Ciornei 182f978fe85SIoana Ciornei err = phy_set_mode_ext(mac->serdes_phy, PHY_MODE_ETHERNET, state->interface); 183f978fe85SIoana Ciornei if (err) 184f978fe85SIoana Ciornei netdev_err(mac->net_dev, "phy_set_mode_ext() = %d\n", err); 18571947923SIoana Ciornei } 18671947923SIoana Ciornei 18791a208f2SRussell King static void dpaa2_mac_link_up(struct phylink_config *config, 18891a208f2SRussell King struct phy_device *phy, 18991a208f2SRussell King unsigned int mode, phy_interface_t interface, 19091a208f2SRussell King int speed, int duplex, 19191a208f2SRussell King bool tx_pause, bool rx_pause) 19271947923SIoana Ciornei { 19371947923SIoana Ciornei struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 19471947923SIoana Ciornei struct dpmac_link_state *dpmac_state = &mac->state; 19571947923SIoana Ciornei int err; 19671947923SIoana Ciornei 19771947923SIoana Ciornei dpmac_state->up = 1; 19837556a4aSRussell King 19937556a4aSRussell King dpmac_state->rate = speed; 20037556a4aSRussell King 20137556a4aSRussell King if (duplex == DUPLEX_HALF) 20237556a4aSRussell King dpmac_state->options |= DPMAC_LINK_OPT_HALF_DUPLEX; 20337556a4aSRussell King else if (duplex == DUPLEX_FULL) 20437556a4aSRussell King dpmac_state->options &= ~DPMAC_LINK_OPT_HALF_DUPLEX; 20537556a4aSRussell King 20637556a4aSRussell King if (rx_pause) 20737556a4aSRussell King dpmac_state->options |= DPMAC_LINK_OPT_PAUSE; 20837556a4aSRussell King else 20937556a4aSRussell King dpmac_state->options &= ~DPMAC_LINK_OPT_PAUSE; 21037556a4aSRussell King 21137556a4aSRussell King if (rx_pause ^ tx_pause) 21237556a4aSRussell King dpmac_state->options |= DPMAC_LINK_OPT_ASYM_PAUSE; 21337556a4aSRussell King else 21437556a4aSRussell King dpmac_state->options &= ~DPMAC_LINK_OPT_ASYM_PAUSE; 21537556a4aSRussell King 21671947923SIoana Ciornei err = dpmac_set_link_state(mac->mc_io, 0, 21771947923SIoana Ciornei mac->mc_dev->mc_handle, dpmac_state); 21871947923SIoana Ciornei if (err) 21937556a4aSRussell King netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 22037556a4aSRussell King __func__, err); 22171947923SIoana Ciornei } 22271947923SIoana Ciornei 22371947923SIoana Ciornei static void dpaa2_mac_link_down(struct phylink_config *config, 22471947923SIoana Ciornei unsigned int mode, 22571947923SIoana Ciornei phy_interface_t interface) 22671947923SIoana Ciornei { 22771947923SIoana Ciornei struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 22871947923SIoana Ciornei struct dpmac_link_state *dpmac_state = &mac->state; 22971947923SIoana Ciornei int err; 23071947923SIoana Ciornei 23171947923SIoana Ciornei dpmac_state->up = 0; 23271947923SIoana Ciornei err = dpmac_set_link_state(mac->mc_io, 0, 23371947923SIoana Ciornei mac->mc_dev->mc_handle, dpmac_state); 23471947923SIoana Ciornei if (err) 23571947923SIoana Ciornei netdev_err(mac->net_dev, "dpmac_set_link_state() = %d\n", err); 23671947923SIoana Ciornei } 23771947923SIoana Ciornei 23871947923SIoana Ciornei static const struct phylink_mac_ops dpaa2_mac_phylink_ops = { 2396d386f66SRussell King (Oracle) .validate = phylink_generic_validate, 240c592286aSRussell King (Oracle) .mac_select_pcs = dpaa2_mac_select_pcs, 24171947923SIoana Ciornei .mac_config = dpaa2_mac_config, 24271947923SIoana Ciornei .mac_link_up = dpaa2_mac_link_up, 24371947923SIoana Ciornei .mac_link_down = dpaa2_mac_link_down, 24471947923SIoana Ciornei }; 24571947923SIoana Ciornei 24694ae899bSIoana Ciornei static int dpaa2_pcs_create(struct dpaa2_mac *mac, 2473264f599SCalvin Johnson struct fwnode_handle *dpmac_node, 2483264f599SCalvin Johnson int id) 24994ae899bSIoana Ciornei { 25094ae899bSIoana Ciornei struct mdio_device *mdiodev; 2513264f599SCalvin Johnson struct fwnode_handle *node; 25294ae899bSIoana Ciornei 2533264f599SCalvin Johnson node = fwnode_find_reference(dpmac_node, "pcs-handle", 0); 2543264f599SCalvin Johnson if (IS_ERR(node)) { 25594ae899bSIoana Ciornei /* do not error out on old DTS files */ 25694ae899bSIoana Ciornei netdev_warn(mac->net_dev, "pcs-handle node not found\n"); 25794ae899bSIoana Ciornei return 0; 25894ae899bSIoana Ciornei } 25994ae899bSIoana Ciornei 2603264f599SCalvin Johnson if (!fwnode_device_is_available(node)) { 26194ae899bSIoana Ciornei netdev_err(mac->net_dev, "pcs-handle node not available\n"); 2623264f599SCalvin Johnson fwnode_handle_put(node); 26394ae899bSIoana Ciornei return -ENODEV; 26494ae899bSIoana Ciornei } 26594ae899bSIoana Ciornei 2663264f599SCalvin Johnson mdiodev = fwnode_mdio_find_device(node); 2673264f599SCalvin Johnson fwnode_handle_put(node); 268*37fe9b98SSean Anderson if (!mdiodev) { 269*37fe9b98SSean Anderson netdev_dbg(mac->net_dev, "missing PCS device\n"); 27094ae899bSIoana Ciornei return -EPROBE_DEFER; 271*37fe9b98SSean Anderson } 27294ae899bSIoana Ciornei 27394ae899bSIoana Ciornei mac->pcs = lynx_pcs_create(mdiodev); 27494ae899bSIoana Ciornei if (!mac->pcs) { 27594ae899bSIoana Ciornei netdev_err(mac->net_dev, "lynx_pcs_create() failed\n"); 27694ae899bSIoana Ciornei put_device(&mdiodev->dev); 27794ae899bSIoana Ciornei return -ENOMEM; 27894ae899bSIoana Ciornei } 27994ae899bSIoana Ciornei 28094ae899bSIoana Ciornei return 0; 28194ae899bSIoana Ciornei } 28294ae899bSIoana Ciornei 28394ae899bSIoana Ciornei static void dpaa2_pcs_destroy(struct dpaa2_mac *mac) 28494ae899bSIoana Ciornei { 285e7026f15SColin Foster struct phylink_pcs *phylink_pcs = mac->pcs; 28694ae899bSIoana Ciornei 287e7026f15SColin Foster if (phylink_pcs) { 288e7026f15SColin Foster struct mdio_device *mdio = lynx_get_mdio_device(phylink_pcs); 289e7026f15SColin Foster struct device *dev = &mdio->dev; 290e7026f15SColin Foster 291e7026f15SColin Foster lynx_pcs_destroy(phylink_pcs); 29294ae899bSIoana Ciornei put_device(dev); 29394ae899bSIoana Ciornei mac->pcs = NULL; 29494ae899bSIoana Ciornei } 29594ae899bSIoana Ciornei } 29694ae899bSIoana Ciornei 297aa95c371SIoana Ciornei static void dpaa2_mac_set_supported_interfaces(struct dpaa2_mac *mac) 298aa95c371SIoana Ciornei { 299f978fe85SIoana Ciornei int intf, err; 300f978fe85SIoana Ciornei 301aa95c371SIoana Ciornei /* We support the current interface mode, and if we have a PCS 302aa95c371SIoana Ciornei * similar interface modes that do not require the SerDes lane to be 303aa95c371SIoana Ciornei * reconfigured. 304aa95c371SIoana Ciornei */ 305aa95c371SIoana Ciornei __set_bit(mac->if_mode, mac->phylink_config.supported_interfaces); 306aa95c371SIoana Ciornei if (mac->pcs) { 307aa95c371SIoana Ciornei switch (mac->if_mode) { 308aa95c371SIoana Ciornei case PHY_INTERFACE_MODE_1000BASEX: 309aa95c371SIoana Ciornei case PHY_INTERFACE_MODE_SGMII: 310aa95c371SIoana Ciornei __set_bit(PHY_INTERFACE_MODE_1000BASEX, 311aa95c371SIoana Ciornei mac->phylink_config.supported_interfaces); 312aa95c371SIoana Ciornei __set_bit(PHY_INTERFACE_MODE_SGMII, 313aa95c371SIoana Ciornei mac->phylink_config.supported_interfaces); 314aa95c371SIoana Ciornei break; 315aa95c371SIoana Ciornei 316aa95c371SIoana Ciornei default: 317aa95c371SIoana Ciornei break; 318aa95c371SIoana Ciornei } 319aa95c371SIoana Ciornei } 320f978fe85SIoana Ciornei 321f978fe85SIoana Ciornei if (!mac->serdes_phy) 322f978fe85SIoana Ciornei return; 323f978fe85SIoana Ciornei 324f978fe85SIoana Ciornei /* In case we have access to the SerDes phy/lane, then ask the SerDes 325f978fe85SIoana Ciornei * driver what interfaces are supported based on the current PLL 326f978fe85SIoana Ciornei * configuration. 327f978fe85SIoana Ciornei */ 328f978fe85SIoana Ciornei for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) { 329f978fe85SIoana Ciornei if (intf == PHY_INTERFACE_MODE_NA) 330f978fe85SIoana Ciornei continue; 331f978fe85SIoana Ciornei 332f978fe85SIoana Ciornei err = phy_validate(mac->serdes_phy, PHY_MODE_ETHERNET, intf, NULL); 333f978fe85SIoana Ciornei if (err) 334f978fe85SIoana Ciornei continue; 335f978fe85SIoana Ciornei 336f978fe85SIoana Ciornei __set_bit(intf, mac->phylink_config.supported_interfaces); 337f978fe85SIoana Ciornei } 338f978fe85SIoana Ciornei } 339f978fe85SIoana Ciornei 340f978fe85SIoana Ciornei void dpaa2_mac_start(struct dpaa2_mac *mac) 341f978fe85SIoana Ciornei { 342f978fe85SIoana Ciornei if (mac->serdes_phy) 343f978fe85SIoana Ciornei phy_power_on(mac->serdes_phy); 344f978fe85SIoana Ciornei } 345f978fe85SIoana Ciornei 346f978fe85SIoana Ciornei void dpaa2_mac_stop(struct dpaa2_mac *mac) 347f978fe85SIoana Ciornei { 348f978fe85SIoana Ciornei if (mac->serdes_phy) 349f978fe85SIoana Ciornei phy_power_off(mac->serdes_phy); 350aa95c371SIoana Ciornei } 351aa95c371SIoana Ciornei 35271947923SIoana Ciornei int dpaa2_mac_connect(struct dpaa2_mac *mac) 35371947923SIoana Ciornei { 35471947923SIoana Ciornei struct net_device *net_dev = mac->net_dev; 3553264f599SCalvin Johnson struct fwnode_handle *dpmac_node; 356f978fe85SIoana Ciornei struct phy *serdes_phy = NULL; 35771947923SIoana Ciornei struct phylink *phylink; 35871947923SIoana Ciornei int err; 35971947923SIoana Ciornei 360095dca16SIoana Ciornei mac->if_link_type = mac->attr.link_type; 361095dca16SIoana Ciornei 3623264f599SCalvin Johnson dpmac_node = mac->fw_node; 363095dca16SIoana Ciornei if (!dpmac_node) { 364095dca16SIoana Ciornei netdev_err(net_dev, "No dpmac@%d node found.\n", mac->attr.id); 36571947923SIoana Ciornei return -ENODEV; 36671947923SIoana Ciornei } 36771947923SIoana Ciornei 368095dca16SIoana Ciornei err = dpaa2_mac_get_if_mode(dpmac_node, mac->attr); 369b193f2edSIoana Ciornei if (err < 0) 370b193f2edSIoana Ciornei return -EINVAL; 37171947923SIoana Ciornei mac->if_mode = err; 37271947923SIoana Ciornei 373f978fe85SIoana Ciornei if (mac->features & DPAA2_MAC_FEATURE_PROTOCOL_CHANGE && 374f978fe85SIoana Ciornei !phy_interface_mode_is_rgmii(mac->if_mode) && 375f978fe85SIoana Ciornei is_of_node(dpmac_node)) { 376f978fe85SIoana Ciornei serdes_phy = of_phy_get(to_of_node(dpmac_node), NULL); 377f978fe85SIoana Ciornei 378f978fe85SIoana Ciornei if (serdes_phy == ERR_PTR(-ENODEV)) 379f978fe85SIoana Ciornei serdes_phy = NULL; 380f978fe85SIoana Ciornei else if (IS_ERR(serdes_phy)) 381f978fe85SIoana Ciornei return PTR_ERR(serdes_phy); 382f978fe85SIoana Ciornei else 383f978fe85SIoana Ciornei phy_init(serdes_phy); 384f978fe85SIoana Ciornei } 385f978fe85SIoana Ciornei mac->serdes_phy = serdes_phy; 386f978fe85SIoana Ciornei 38771947923SIoana Ciornei /* The MAC does not have the capability to add RGMII delays so 38871947923SIoana Ciornei * error out if the interface mode requests them and there is no PHY 38971947923SIoana Ciornei * to act upon them 39071947923SIoana Ciornei */ 3913264f599SCalvin Johnson if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && 39271947923SIoana Ciornei (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || 39371947923SIoana Ciornei mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || 39471947923SIoana Ciornei mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { 39571947923SIoana Ciornei netdev_err(net_dev, "RGMII delay not supported\n"); 396b193f2edSIoana Ciornei return -EINVAL; 39771947923SIoana Ciornei } 39871947923SIoana Ciornei 399085f1776SRussell King if ((mac->attr.link_type == DPMAC_LINK_TYPE_PHY && 400085f1776SRussell King mac->attr.eth_if != DPMAC_ETH_IF_RGMII) || 401085f1776SRussell King mac->attr.link_type == DPMAC_LINK_TYPE_BACKPLANE) { 402095dca16SIoana Ciornei err = dpaa2_pcs_create(mac, dpmac_node, mac->attr.id); 40394ae899bSIoana Ciornei if (err) 404b193f2edSIoana Ciornei return err; 40594ae899bSIoana Ciornei } 40694ae899bSIoana Ciornei 40715d0b14cSRussell King memset(&mac->phylink_config, 0, sizeof(mac->phylink_config)); 40871947923SIoana Ciornei mac->phylink_config.dev = &net_dev->dev; 40971947923SIoana Ciornei mac->phylink_config.type = PHYLINK_NETDEV; 41071947923SIoana Ciornei 4116d386f66SRussell King (Oracle) mac->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | 4126d386f66SRussell King (Oracle) MAC_10FD | MAC_100FD | MAC_1000FD | MAC_2500FD | MAC_5000FD | 4136d386f66SRussell King (Oracle) MAC_10000FD; 4146d386f66SRussell King (Oracle) 415aa95c371SIoana Ciornei dpaa2_mac_set_supported_interfaces(mac); 41615d0b14cSRussell King 41771947923SIoana Ciornei phylink = phylink_create(&mac->phylink_config, 4183264f599SCalvin Johnson dpmac_node, mac->if_mode, 41971947923SIoana Ciornei &dpaa2_mac_phylink_ops); 42071947923SIoana Ciornei if (IS_ERR(phylink)) { 42171947923SIoana Ciornei err = PTR_ERR(phylink); 42294ae899bSIoana Ciornei goto err_pcs_destroy; 42371947923SIoana Ciornei } 42471947923SIoana Ciornei mac->phylink = phylink; 42571947923SIoana Ciornei 4263264f599SCalvin Johnson err = phylink_fwnode_phy_connect(mac->phylink, dpmac_node, 0); 42771947923SIoana Ciornei if (err) { 4283264f599SCalvin Johnson netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); 42971947923SIoana Ciornei goto err_phylink_destroy; 43071947923SIoana Ciornei } 43171947923SIoana Ciornei 43271947923SIoana Ciornei return 0; 43371947923SIoana Ciornei 43471947923SIoana Ciornei err_phylink_destroy: 43571947923SIoana Ciornei phylink_destroy(mac->phylink); 43694ae899bSIoana Ciornei err_pcs_destroy: 43794ae899bSIoana Ciornei dpaa2_pcs_destroy(mac); 438095dca16SIoana Ciornei 43971947923SIoana Ciornei return err; 44071947923SIoana Ciornei } 44171947923SIoana Ciornei 44271947923SIoana Ciornei void dpaa2_mac_disconnect(struct dpaa2_mac *mac) 44371947923SIoana Ciornei { 44471947923SIoana Ciornei if (!mac->phylink) 44571947923SIoana Ciornei return; 44671947923SIoana Ciornei 44771947923SIoana Ciornei phylink_disconnect_phy(mac->phylink); 44871947923SIoana Ciornei phylink_destroy(mac->phylink); 44994ae899bSIoana Ciornei dpaa2_pcs_destroy(mac); 450f978fe85SIoana Ciornei of_phy_put(mac->serdes_phy); 451f978fe85SIoana Ciornei mac->serdes_phy = NULL; 452095dca16SIoana Ciornei } 45394ae899bSIoana Ciornei 454095dca16SIoana Ciornei int dpaa2_mac_open(struct dpaa2_mac *mac) 455095dca16SIoana Ciornei { 456095dca16SIoana Ciornei struct fsl_mc_device *dpmac_dev = mac->mc_dev; 457095dca16SIoana Ciornei struct net_device *net_dev = mac->net_dev; 4584e30e98cSIoana Ciornei struct fwnode_handle *fw_node; 459095dca16SIoana Ciornei int err; 460095dca16SIoana Ciornei 461095dca16SIoana Ciornei err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, 462095dca16SIoana Ciornei &dpmac_dev->mc_handle); 463095dca16SIoana Ciornei if (err || !dpmac_dev->mc_handle) { 464095dca16SIoana Ciornei netdev_err(net_dev, "dpmac_open() = %d\n", err); 465095dca16SIoana Ciornei return -ENODEV; 466095dca16SIoana Ciornei } 467095dca16SIoana Ciornei 468095dca16SIoana Ciornei err = dpmac_get_attributes(mac->mc_io, 0, dpmac_dev->mc_handle, 469095dca16SIoana Ciornei &mac->attr); 470095dca16SIoana Ciornei if (err) { 471095dca16SIoana Ciornei netdev_err(net_dev, "dpmac_get_attributes() = %d\n", err); 472095dca16SIoana Ciornei goto err_close_dpmac; 473095dca16SIoana Ciornei } 474095dca16SIoana Ciornei 475dff95381SIoana Ciornei err = dpmac_get_api_version(mac->mc_io, 0, &mac->ver_major, &mac->ver_minor); 476dff95381SIoana Ciornei if (err) { 477dff95381SIoana Ciornei netdev_err(net_dev, "dpmac_get_api_version() = %d\n", err); 478dff95381SIoana Ciornei goto err_close_dpmac; 479dff95381SIoana Ciornei } 480dff95381SIoana Ciornei 481dff95381SIoana Ciornei dpaa2_mac_detect_features(mac); 482dff95381SIoana Ciornei 483b193f2edSIoana Ciornei /* Find the device node representing the MAC device and link the device 484b193f2edSIoana Ciornei * behind the associated netdev to it. 485b193f2edSIoana Ciornei */ 4864e30e98cSIoana Ciornei fw_node = dpaa2_mac_get_node(&mac->mc_dev->dev, mac->attr.id); 4874e30e98cSIoana Ciornei if (IS_ERR(fw_node)) { 4884e30e98cSIoana Ciornei err = PTR_ERR(fw_node); 4894e30e98cSIoana Ciornei goto err_close_dpmac; 4904e30e98cSIoana Ciornei } 4914e30e98cSIoana Ciornei 4924e30e98cSIoana Ciornei mac->fw_node = fw_node; 4933264f599SCalvin Johnson net_dev->dev.of_node = to_of_node(mac->fw_node); 494b193f2edSIoana Ciornei 495095dca16SIoana Ciornei return 0; 496095dca16SIoana Ciornei 497095dca16SIoana Ciornei err_close_dpmac: 498095dca16SIoana Ciornei dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 499095dca16SIoana Ciornei return err; 500095dca16SIoana Ciornei } 501095dca16SIoana Ciornei 502095dca16SIoana Ciornei void dpaa2_mac_close(struct dpaa2_mac *mac) 503095dca16SIoana Ciornei { 504095dca16SIoana Ciornei struct fsl_mc_device *dpmac_dev = mac->mc_dev; 505095dca16SIoana Ciornei 506095dca16SIoana Ciornei dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 5073264f599SCalvin Johnson if (mac->fw_node) 5083264f599SCalvin Johnson fwnode_handle_put(mac->fw_node); 50971947923SIoana Ciornei } 510991df1fbSIoana Ciornei 511991df1fbSIoana Ciornei static char dpaa2_mac_ethtool_stats[][ETH_GSTRING_LEN] = { 512991df1fbSIoana Ciornei [DPMAC_CNT_ING_ALL_FRAME] = "[mac] rx all frames", 513991df1fbSIoana Ciornei [DPMAC_CNT_ING_GOOD_FRAME] = "[mac] rx frames ok", 514991df1fbSIoana Ciornei [DPMAC_CNT_ING_ERR_FRAME] = "[mac] rx frame errors", 515991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_DISCARD] = "[mac] rx frame discards", 516991df1fbSIoana Ciornei [DPMAC_CNT_ING_UCAST_FRAME] = "[mac] rx u-cast", 517991df1fbSIoana Ciornei [DPMAC_CNT_ING_BCAST_FRAME] = "[mac] rx b-cast", 518991df1fbSIoana Ciornei [DPMAC_CNT_ING_MCAST_FRAME] = "[mac] rx m-cast", 519991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_64] = "[mac] rx 64 bytes", 520991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_127] = "[mac] rx 65-127 bytes", 521991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_255] = "[mac] rx 128-255 bytes", 522991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_511] = "[mac] rx 256-511 bytes", 523991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_1023] = "[mac] rx 512-1023 bytes", 524991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_1518] = "[mac] rx 1024-1518 bytes", 525991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAME_1519_MAX] = "[mac] rx 1519-max bytes", 526991df1fbSIoana Ciornei [DPMAC_CNT_ING_FRAG] = "[mac] rx frags", 527991df1fbSIoana Ciornei [DPMAC_CNT_ING_JABBER] = "[mac] rx jabber", 528991df1fbSIoana Ciornei [DPMAC_CNT_ING_ALIGN_ERR] = "[mac] rx align errors", 529991df1fbSIoana Ciornei [DPMAC_CNT_ING_OVERSIZED] = "[mac] rx oversized", 530991df1fbSIoana Ciornei [DPMAC_CNT_ING_VALID_PAUSE_FRAME] = "[mac] rx pause", 531991df1fbSIoana Ciornei [DPMAC_CNT_ING_BYTE] = "[mac] rx bytes", 532991df1fbSIoana Ciornei [DPMAC_CNT_EGR_GOOD_FRAME] = "[mac] tx frames ok", 533991df1fbSIoana Ciornei [DPMAC_CNT_EGR_UCAST_FRAME] = "[mac] tx u-cast", 534991df1fbSIoana Ciornei [DPMAC_CNT_EGR_MCAST_FRAME] = "[mac] tx m-cast", 535991df1fbSIoana Ciornei [DPMAC_CNT_EGR_BCAST_FRAME] = "[mac] tx b-cast", 536991df1fbSIoana Ciornei [DPMAC_CNT_EGR_ERR_FRAME] = "[mac] tx frame errors", 537991df1fbSIoana Ciornei [DPMAC_CNT_EGR_UNDERSIZED] = "[mac] tx undersized", 538991df1fbSIoana Ciornei [DPMAC_CNT_EGR_VALID_PAUSE_FRAME] = "[mac] tx b-pause", 539991df1fbSIoana Ciornei [DPMAC_CNT_EGR_BYTE] = "[mac] tx bytes", 540991df1fbSIoana Ciornei }; 541991df1fbSIoana Ciornei 542991df1fbSIoana Ciornei #define DPAA2_MAC_NUM_STATS ARRAY_SIZE(dpaa2_mac_ethtool_stats) 543991df1fbSIoana Ciornei 544991df1fbSIoana Ciornei int dpaa2_mac_get_sset_count(void) 545991df1fbSIoana Ciornei { 546991df1fbSIoana Ciornei return DPAA2_MAC_NUM_STATS; 547991df1fbSIoana Ciornei } 548991df1fbSIoana Ciornei 549991df1fbSIoana Ciornei void dpaa2_mac_get_strings(u8 *data) 550991df1fbSIoana Ciornei { 551991df1fbSIoana Ciornei u8 *p = data; 552991df1fbSIoana Ciornei int i; 553991df1fbSIoana Ciornei 554991df1fbSIoana Ciornei for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 555f029c781SWolfram Sang strscpy(p, dpaa2_mac_ethtool_stats[i], ETH_GSTRING_LEN); 556991df1fbSIoana Ciornei p += ETH_GSTRING_LEN; 557991df1fbSIoana Ciornei } 558991df1fbSIoana Ciornei } 559991df1fbSIoana Ciornei 560991df1fbSIoana Ciornei void dpaa2_mac_get_ethtool_stats(struct dpaa2_mac *mac, u64 *data) 561991df1fbSIoana Ciornei { 562991df1fbSIoana Ciornei struct fsl_mc_device *dpmac_dev = mac->mc_dev; 563991df1fbSIoana Ciornei int i, err; 564991df1fbSIoana Ciornei u64 value; 565991df1fbSIoana Ciornei 566991df1fbSIoana Ciornei for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 567991df1fbSIoana Ciornei err = dpmac_get_counter(mac->mc_io, 0, dpmac_dev->mc_handle, 568991df1fbSIoana Ciornei i, &value); 569991df1fbSIoana Ciornei if (err) { 570991df1fbSIoana Ciornei netdev_err_once(mac->net_dev, 571991df1fbSIoana Ciornei "dpmac_get_counter error %d\n", err); 572991df1fbSIoana Ciornei *(data + i) = U64_MAX; 573991df1fbSIoana Ciornei continue; 574991df1fbSIoana Ciornei } 575991df1fbSIoana Ciornei *(data + i) = value; 576991df1fbSIoana Ciornei } 577991df1fbSIoana Ciornei } 578