1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2019 NXP */ 3 4 #include <linux/acpi.h> 5 #include <linux/pcs-lynx.h> 6 #include <linux/phy/phy.h> 7 #include <linux/property.h> 8 9 #include "dpaa2-eth.h" 10 #include "dpaa2-mac.h" 11 12 #define phylink_to_dpaa2_mac(config) \ 13 container_of((config), struct dpaa2_mac, phylink_config) 14 15 #define DPMAC_PROTOCOL_CHANGE_VER_MAJOR 4 16 #define DPMAC_PROTOCOL_CHANGE_VER_MINOR 8 17 18 #define DPAA2_MAC_FEATURE_PROTOCOL_CHANGE BIT(0) 19 20 static int dpaa2_mac_cmp_ver(struct dpaa2_mac *mac, 21 u16 ver_major, u16 ver_minor) 22 { 23 if (mac->ver_major == ver_major) 24 return mac->ver_minor - ver_minor; 25 return mac->ver_major - ver_major; 26 } 27 28 static void dpaa2_mac_detect_features(struct dpaa2_mac *mac) 29 { 30 mac->features = 0; 31 32 if (dpaa2_mac_cmp_ver(mac, DPMAC_PROTOCOL_CHANGE_VER_MAJOR, 33 DPMAC_PROTOCOL_CHANGE_VER_MINOR) >= 0) 34 mac->features |= DPAA2_MAC_FEATURE_PROTOCOL_CHANGE; 35 } 36 37 static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode) 38 { 39 *if_mode = PHY_INTERFACE_MODE_NA; 40 41 switch (eth_if) { 42 case DPMAC_ETH_IF_RGMII: 43 *if_mode = PHY_INTERFACE_MODE_RGMII; 44 break; 45 case DPMAC_ETH_IF_USXGMII: 46 *if_mode = PHY_INTERFACE_MODE_USXGMII; 47 break; 48 case DPMAC_ETH_IF_QSGMII: 49 *if_mode = PHY_INTERFACE_MODE_QSGMII; 50 break; 51 case DPMAC_ETH_IF_SGMII: 52 *if_mode = PHY_INTERFACE_MODE_SGMII; 53 break; 54 case DPMAC_ETH_IF_XFI: 55 *if_mode = PHY_INTERFACE_MODE_10GBASER; 56 break; 57 default: 58 return -EINVAL; 59 } 60 61 return 0; 62 } 63 64 static enum dpmac_eth_if dpmac_eth_if_mode(phy_interface_t if_mode) 65 { 66 switch (if_mode) { 67 case PHY_INTERFACE_MODE_RGMII: 68 case PHY_INTERFACE_MODE_RGMII_ID: 69 case PHY_INTERFACE_MODE_RGMII_RXID: 70 case PHY_INTERFACE_MODE_RGMII_TXID: 71 return DPMAC_ETH_IF_RGMII; 72 case PHY_INTERFACE_MODE_USXGMII: 73 return DPMAC_ETH_IF_USXGMII; 74 case PHY_INTERFACE_MODE_QSGMII: 75 return DPMAC_ETH_IF_QSGMII; 76 case PHY_INTERFACE_MODE_SGMII: 77 return DPMAC_ETH_IF_SGMII; 78 case PHY_INTERFACE_MODE_10GBASER: 79 return DPMAC_ETH_IF_XFI; 80 case PHY_INTERFACE_MODE_1000BASEX: 81 return DPMAC_ETH_IF_1000BASEX; 82 default: 83 return DPMAC_ETH_IF_MII; 84 } 85 } 86 87 static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev, 88 u16 dpmac_id) 89 { 90 struct fwnode_handle *fwnode, *parent = NULL, *child = NULL; 91 struct device_node *dpmacs = NULL; 92 int err; 93 u32 id; 94 95 fwnode = dev_fwnode(dev->parent); 96 if (is_of_node(fwnode)) { 97 dpmacs = of_find_node_by_name(NULL, "dpmacs"); 98 if (!dpmacs) 99 return NULL; 100 parent = of_fwnode_handle(dpmacs); 101 } else if (is_acpi_node(fwnode)) { 102 parent = fwnode; 103 } else { 104 /* The root dprc device didn't yet get to finalize it's probe, 105 * thus the fwnode field is not yet set. Defer probe if we are 106 * facing this situation. 107 */ 108 dev_dbg(dev, "dprc not finished probing\n"); 109 return ERR_PTR(-EPROBE_DEFER); 110 } 111 112 fwnode_for_each_child_node(parent, child) { 113 err = -EINVAL; 114 if (is_acpi_device_node(child)) 115 err = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), &id); 116 else if (is_of_node(child)) 117 err = of_property_read_u32(to_of_node(child), "reg", &id); 118 if (err) 119 continue; 120 121 if (id == dpmac_id) { 122 of_node_put(dpmacs); 123 return child; 124 } 125 } 126 of_node_put(dpmacs); 127 return NULL; 128 } 129 130 static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node, 131 struct dpmac_attr attr) 132 { 133 phy_interface_t if_mode; 134 int err; 135 136 err = fwnode_get_phy_mode(dpmac_node); 137 if (err > 0) 138 return err; 139 140 err = phy_mode(attr.eth_if, &if_mode); 141 if (!err) 142 return if_mode; 143 144 return err; 145 } 146 147 static struct phylink_pcs *dpaa2_mac_select_pcs(struct phylink_config *config, 148 phy_interface_t interface) 149 { 150 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 151 152 return mac->pcs; 153 } 154 155 static void dpaa2_mac_config(struct phylink_config *config, unsigned int mode, 156 const struct phylink_link_state *state) 157 { 158 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 159 struct dpmac_link_state *dpmac_state = &mac->state; 160 int err; 161 162 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 163 state->advertising)) 164 dpmac_state->options |= DPMAC_LINK_OPT_AUTONEG; 165 else 166 dpmac_state->options &= ~DPMAC_LINK_OPT_AUTONEG; 167 168 err = dpmac_set_link_state(mac->mc_io, 0, 169 mac->mc_dev->mc_handle, dpmac_state); 170 if (err) 171 netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 172 __func__, err); 173 174 if (!mac->serdes_phy) 175 return; 176 177 /* This happens only if we support changing of protocol at runtime */ 178 err = dpmac_set_protocol(mac->mc_io, 0, mac->mc_dev->mc_handle, 179 dpmac_eth_if_mode(state->interface)); 180 if (err) 181 netdev_err(mac->net_dev, "dpmac_set_protocol() = %d\n", err); 182 183 err = phy_set_mode_ext(mac->serdes_phy, PHY_MODE_ETHERNET, state->interface); 184 if (err) 185 netdev_err(mac->net_dev, "phy_set_mode_ext() = %d\n", err); 186 } 187 188 static void dpaa2_mac_link_up(struct phylink_config *config, 189 struct phy_device *phy, 190 unsigned int mode, phy_interface_t interface, 191 int speed, int duplex, 192 bool tx_pause, bool rx_pause) 193 { 194 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 195 struct dpmac_link_state *dpmac_state = &mac->state; 196 int err; 197 198 dpmac_state->up = 1; 199 200 dpmac_state->rate = speed; 201 202 if (duplex == DUPLEX_HALF) 203 dpmac_state->options |= DPMAC_LINK_OPT_HALF_DUPLEX; 204 else if (duplex == DUPLEX_FULL) 205 dpmac_state->options &= ~DPMAC_LINK_OPT_HALF_DUPLEX; 206 207 if (rx_pause) 208 dpmac_state->options |= DPMAC_LINK_OPT_PAUSE; 209 else 210 dpmac_state->options &= ~DPMAC_LINK_OPT_PAUSE; 211 212 if (rx_pause ^ tx_pause) 213 dpmac_state->options |= DPMAC_LINK_OPT_ASYM_PAUSE; 214 else 215 dpmac_state->options &= ~DPMAC_LINK_OPT_ASYM_PAUSE; 216 217 err = dpmac_set_link_state(mac->mc_io, 0, 218 mac->mc_dev->mc_handle, dpmac_state); 219 if (err) 220 netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 221 __func__, err); 222 } 223 224 static void dpaa2_mac_link_down(struct phylink_config *config, 225 unsigned int mode, 226 phy_interface_t interface) 227 { 228 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 229 struct dpmac_link_state *dpmac_state = &mac->state; 230 int err; 231 232 dpmac_state->up = 0; 233 err = dpmac_set_link_state(mac->mc_io, 0, 234 mac->mc_dev->mc_handle, dpmac_state); 235 if (err) 236 netdev_err(mac->net_dev, "dpmac_set_link_state() = %d\n", err); 237 } 238 239 static const struct phylink_mac_ops dpaa2_mac_phylink_ops = { 240 .mac_select_pcs = dpaa2_mac_select_pcs, 241 .mac_config = dpaa2_mac_config, 242 .mac_link_up = dpaa2_mac_link_up, 243 .mac_link_down = dpaa2_mac_link_down, 244 }; 245 246 static int dpaa2_pcs_create(struct dpaa2_mac *mac, 247 struct fwnode_handle *dpmac_node, 248 int id) 249 { 250 struct fwnode_handle *node; 251 struct phylink_pcs *pcs; 252 253 node = fwnode_find_reference(dpmac_node, "pcs-handle", 0); 254 if (IS_ERR(node)) { 255 /* do not error out on old DTS files */ 256 netdev_warn(mac->net_dev, "pcs-handle node not found\n"); 257 return 0; 258 } 259 260 pcs = lynx_pcs_create_fwnode(node); 261 fwnode_handle_put(node); 262 263 if (pcs == ERR_PTR(-EPROBE_DEFER)) { 264 netdev_dbg(mac->net_dev, "missing PCS device\n"); 265 return -EPROBE_DEFER; 266 } 267 268 if (pcs == ERR_PTR(-ENODEV)) { 269 netdev_err(mac->net_dev, "pcs-handle node not available\n"); 270 return PTR_ERR(pcs); 271 } 272 273 if (IS_ERR(pcs)) { 274 netdev_err(mac->net_dev, 275 "lynx_pcs_create_fwnode() failed: %pe\n", pcs); 276 return PTR_ERR(pcs); 277 } 278 279 mac->pcs = pcs; 280 281 return 0; 282 } 283 284 static void dpaa2_pcs_destroy(struct dpaa2_mac *mac) 285 { 286 struct phylink_pcs *phylink_pcs = mac->pcs; 287 288 if (phylink_pcs) { 289 lynx_pcs_destroy(phylink_pcs); 290 mac->pcs = NULL; 291 } 292 } 293 294 static void dpaa2_mac_set_supported_interfaces(struct dpaa2_mac *mac) 295 { 296 int intf, err; 297 298 /* We support the current interface mode, and if we have a PCS 299 * similar interface modes that do not require the SerDes lane to be 300 * reconfigured. 301 */ 302 __set_bit(mac->if_mode, mac->phylink_config.supported_interfaces); 303 if (mac->pcs) { 304 switch (mac->if_mode) { 305 case PHY_INTERFACE_MODE_1000BASEX: 306 case PHY_INTERFACE_MODE_SGMII: 307 __set_bit(PHY_INTERFACE_MODE_1000BASEX, 308 mac->phylink_config.supported_interfaces); 309 __set_bit(PHY_INTERFACE_MODE_SGMII, 310 mac->phylink_config.supported_interfaces); 311 break; 312 313 default: 314 break; 315 } 316 } 317 318 if (!mac->serdes_phy) 319 return; 320 321 /* In case we have access to the SerDes phy/lane, then ask the SerDes 322 * driver what interfaces are supported based on the current PLL 323 * configuration. 324 */ 325 for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) { 326 if (intf == PHY_INTERFACE_MODE_NA) 327 continue; 328 329 err = phy_validate(mac->serdes_phy, PHY_MODE_ETHERNET, intf, NULL); 330 if (err) 331 continue; 332 333 __set_bit(intf, mac->phylink_config.supported_interfaces); 334 } 335 } 336 337 void dpaa2_mac_start(struct dpaa2_mac *mac) 338 { 339 ASSERT_RTNL(); 340 341 if (mac->serdes_phy) 342 phy_power_on(mac->serdes_phy); 343 344 phylink_start(mac->phylink); 345 } 346 347 void dpaa2_mac_stop(struct dpaa2_mac *mac) 348 { 349 ASSERT_RTNL(); 350 351 phylink_stop(mac->phylink); 352 353 if (mac->serdes_phy) 354 phy_power_off(mac->serdes_phy); 355 } 356 357 int dpaa2_mac_connect(struct dpaa2_mac *mac) 358 { 359 struct net_device *net_dev = mac->net_dev; 360 struct fwnode_handle *dpmac_node; 361 struct phy *serdes_phy = NULL; 362 struct phylink *phylink; 363 int err; 364 365 mac->if_link_type = mac->attr.link_type; 366 367 dpmac_node = mac->fw_node; 368 if (!dpmac_node) { 369 netdev_err(net_dev, "No dpmac@%d node found.\n", mac->attr.id); 370 return -ENODEV; 371 } 372 373 err = dpaa2_mac_get_if_mode(dpmac_node, mac->attr); 374 if (err < 0) 375 return -EINVAL; 376 mac->if_mode = err; 377 378 if (mac->features & DPAA2_MAC_FEATURE_PROTOCOL_CHANGE && 379 !phy_interface_mode_is_rgmii(mac->if_mode) && 380 is_of_node(dpmac_node)) { 381 serdes_phy = of_phy_get(to_of_node(dpmac_node), NULL); 382 383 if (serdes_phy == ERR_PTR(-ENODEV)) 384 serdes_phy = NULL; 385 else if (IS_ERR(serdes_phy)) 386 return PTR_ERR(serdes_phy); 387 else 388 phy_init(serdes_phy); 389 } 390 mac->serdes_phy = serdes_phy; 391 392 /* The MAC does not have the capability to add RGMII delays so 393 * error out if the interface mode requests them and there is no PHY 394 * to act upon them 395 */ 396 if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && 397 (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || 398 mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || 399 mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { 400 netdev_err(net_dev, "RGMII delay not supported\n"); 401 return -EINVAL; 402 } 403 404 if ((mac->attr.link_type == DPMAC_LINK_TYPE_PHY && 405 mac->attr.eth_if != DPMAC_ETH_IF_RGMII) || 406 mac->attr.link_type == DPMAC_LINK_TYPE_BACKPLANE) { 407 err = dpaa2_pcs_create(mac, dpmac_node, mac->attr.id); 408 if (err) 409 return err; 410 } 411 412 memset(&mac->phylink_config, 0, sizeof(mac->phylink_config)); 413 mac->phylink_config.dev = &net_dev->dev; 414 mac->phylink_config.type = PHYLINK_NETDEV; 415 416 mac->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | 417 MAC_10FD | MAC_100FD | MAC_1000FD | MAC_2500FD | MAC_5000FD | 418 MAC_10000FD; 419 420 dpaa2_mac_set_supported_interfaces(mac); 421 422 phylink = phylink_create(&mac->phylink_config, 423 dpmac_node, mac->if_mode, 424 &dpaa2_mac_phylink_ops); 425 if (IS_ERR(phylink)) { 426 err = PTR_ERR(phylink); 427 goto err_pcs_destroy; 428 } 429 mac->phylink = phylink; 430 431 rtnl_lock(); 432 err = phylink_fwnode_phy_connect(mac->phylink, dpmac_node, 0); 433 rtnl_unlock(); 434 if (err) { 435 netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); 436 goto err_phylink_destroy; 437 } 438 439 return 0; 440 441 err_phylink_destroy: 442 phylink_destroy(mac->phylink); 443 err_pcs_destroy: 444 dpaa2_pcs_destroy(mac); 445 446 return err; 447 } 448 449 void dpaa2_mac_disconnect(struct dpaa2_mac *mac) 450 { 451 rtnl_lock(); 452 phylink_disconnect_phy(mac->phylink); 453 rtnl_unlock(); 454 455 phylink_destroy(mac->phylink); 456 dpaa2_pcs_destroy(mac); 457 of_phy_put(mac->serdes_phy); 458 mac->serdes_phy = NULL; 459 } 460 461 int dpaa2_mac_open(struct dpaa2_mac *mac) 462 { 463 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 464 struct net_device *net_dev = mac->net_dev; 465 struct fwnode_handle *fw_node; 466 int err; 467 468 err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, 469 &dpmac_dev->mc_handle); 470 if (err || !dpmac_dev->mc_handle) { 471 netdev_err(net_dev, "dpmac_open() = %d\n", err); 472 return -ENODEV; 473 } 474 475 err = dpmac_get_attributes(mac->mc_io, 0, dpmac_dev->mc_handle, 476 &mac->attr); 477 if (err) { 478 netdev_err(net_dev, "dpmac_get_attributes() = %d\n", err); 479 goto err_close_dpmac; 480 } 481 482 err = dpmac_get_api_version(mac->mc_io, 0, &mac->ver_major, &mac->ver_minor); 483 if (err) { 484 netdev_err(net_dev, "dpmac_get_api_version() = %d\n", err); 485 goto err_close_dpmac; 486 } 487 488 dpaa2_mac_detect_features(mac); 489 490 /* Find the device node representing the MAC device and link the device 491 * behind the associated netdev to it. 492 */ 493 fw_node = dpaa2_mac_get_node(&mac->mc_dev->dev, mac->attr.id); 494 if (IS_ERR(fw_node)) { 495 err = PTR_ERR(fw_node); 496 goto err_close_dpmac; 497 } 498 499 mac->fw_node = fw_node; 500 net_dev->dev.of_node = to_of_node(mac->fw_node); 501 502 return 0; 503 504 err_close_dpmac: 505 dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 506 return err; 507 } 508 509 void dpaa2_mac_close(struct dpaa2_mac *mac) 510 { 511 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 512 513 dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 514 if (mac->fw_node) 515 fwnode_handle_put(mac->fw_node); 516 } 517 518 static char dpaa2_mac_ethtool_stats[][ETH_GSTRING_LEN] = { 519 [DPMAC_CNT_ING_ALL_FRAME] = "[mac] rx all frames", 520 [DPMAC_CNT_ING_GOOD_FRAME] = "[mac] rx frames ok", 521 [DPMAC_CNT_ING_ERR_FRAME] = "[mac] rx frame errors", 522 [DPMAC_CNT_ING_FRAME_DISCARD] = "[mac] rx frame discards", 523 [DPMAC_CNT_ING_UCAST_FRAME] = "[mac] rx u-cast", 524 [DPMAC_CNT_ING_BCAST_FRAME] = "[mac] rx b-cast", 525 [DPMAC_CNT_ING_MCAST_FRAME] = "[mac] rx m-cast", 526 [DPMAC_CNT_ING_FRAME_64] = "[mac] rx 64 bytes", 527 [DPMAC_CNT_ING_FRAME_127] = "[mac] rx 65-127 bytes", 528 [DPMAC_CNT_ING_FRAME_255] = "[mac] rx 128-255 bytes", 529 [DPMAC_CNT_ING_FRAME_511] = "[mac] rx 256-511 bytes", 530 [DPMAC_CNT_ING_FRAME_1023] = "[mac] rx 512-1023 bytes", 531 [DPMAC_CNT_ING_FRAME_1518] = "[mac] rx 1024-1518 bytes", 532 [DPMAC_CNT_ING_FRAME_1519_MAX] = "[mac] rx 1519-max bytes", 533 [DPMAC_CNT_ING_FRAG] = "[mac] rx frags", 534 [DPMAC_CNT_ING_JABBER] = "[mac] rx jabber", 535 [DPMAC_CNT_ING_ALIGN_ERR] = "[mac] rx align errors", 536 [DPMAC_CNT_ING_OVERSIZED] = "[mac] rx oversized", 537 [DPMAC_CNT_ING_VALID_PAUSE_FRAME] = "[mac] rx pause", 538 [DPMAC_CNT_ING_BYTE] = "[mac] rx bytes", 539 [DPMAC_CNT_EGR_GOOD_FRAME] = "[mac] tx frames ok", 540 [DPMAC_CNT_EGR_UCAST_FRAME] = "[mac] tx u-cast", 541 [DPMAC_CNT_EGR_MCAST_FRAME] = "[mac] tx m-cast", 542 [DPMAC_CNT_EGR_BCAST_FRAME] = "[mac] tx b-cast", 543 [DPMAC_CNT_EGR_ERR_FRAME] = "[mac] tx frame errors", 544 [DPMAC_CNT_EGR_UNDERSIZED] = "[mac] tx undersized", 545 [DPMAC_CNT_EGR_VALID_PAUSE_FRAME] = "[mac] tx b-pause", 546 [DPMAC_CNT_EGR_BYTE] = "[mac] tx bytes", 547 }; 548 549 #define DPAA2_MAC_NUM_STATS ARRAY_SIZE(dpaa2_mac_ethtool_stats) 550 551 int dpaa2_mac_get_sset_count(void) 552 { 553 return DPAA2_MAC_NUM_STATS; 554 } 555 556 void dpaa2_mac_get_strings(u8 *data) 557 { 558 u8 *p = data; 559 int i; 560 561 for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 562 strscpy(p, dpaa2_mac_ethtool_stats[i], ETH_GSTRING_LEN); 563 p += ETH_GSTRING_LEN; 564 } 565 } 566 567 void dpaa2_mac_get_ethtool_stats(struct dpaa2_mac *mac, u64 *data) 568 { 569 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 570 int i, err; 571 u64 value; 572 573 for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 574 err = dpmac_get_counter(mac->mc_io, 0, dpmac_dev->mc_handle, 575 i, &value); 576 if (err) { 577 netdev_err_once(mac->net_dev, 578 "dpmac_get_counter error %d\n", err); 579 *(data + i) = U64_MAX; 580 continue; 581 } 582 *(data + i) = value; 583 } 584 } 585