Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'net-dsa-qca8k-Improve-SGMII-interface-handling'

Jonathan McDowell says:

====================
net: dsa: qca8k: Improve SGMII interface handling

This 3 patch series migrates the qca8k switch driver over to PHYLINK,
and then adds the SGMII clean-ups (i.e. the missing initialisation) on
top of that as a second patch. The final patch is a simple spelling fix
in a comment.

As before, tested with a device where the CPU connection is RGMII (i.e.
the common current use case) + one where the CPU connection is SGMII. I
don't have any devices where the SGMII interface is brought out to
something other than the CPU.

v5:
- Move spelling fix to separate patch
- Use ds directly rather than ds->priv
v4:
- Enable pcs_poll so we keep phylink updated when doing in-band
negotiation
- Explicitly check for PHY_INTERFACE_MODE_1000BASEX when setting SGMII
port mode.
- Address Vladimir's review comments
v3:
- Move phylink changes to separate patch
- Address rmk review comments
v2:
- Switch to phylink
- Avoid need for device tree configuration options
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+257 -99
+244 -99
drivers/net/dsa/qca8k.c
··· 14 14 #include <linux/of_platform.h> 15 15 #include <linux/if_bridge.h> 16 16 #include <linux/mdio.h> 17 + #include <linux/phylink.h> 17 18 #include <linux/gpio/consumer.h> 18 19 #include <linux/etherdevice.h> 19 20 ··· 419 418 mutex_unlock(&priv->reg_mutex); 420 419 } 421 420 422 - static int 423 - qca8k_set_pad_ctrl(struct qca8k_priv *priv, int port, int mode) 424 - { 425 - u32 reg, val; 426 - 427 - switch (port) { 428 - case 0: 429 - reg = QCA8K_REG_PORT0_PAD_CTRL; 430 - break; 431 - case 6: 432 - reg = QCA8K_REG_PORT6_PAD_CTRL; 433 - break; 434 - default: 435 - pr_err("Can't set PAD_CTRL on port %d\n", port); 436 - return -EINVAL; 437 - } 438 - 439 - /* Configure a port to be directly connected to an external 440 - * PHY or MAC. 441 - */ 442 - switch (mode) { 443 - case PHY_INTERFACE_MODE_RGMII: 444 - /* RGMII mode means no delay so don't enable the delay */ 445 - val = QCA8K_PORT_PAD_RGMII_EN; 446 - qca8k_write(priv, reg, val); 447 - break; 448 - case PHY_INTERFACE_MODE_RGMII_ID: 449 - /* RGMII_ID needs internal delay. This is enabled through 450 - * PORT5_PAD_CTRL for all ports, rather than individual port 451 - * registers 452 - */ 453 - qca8k_write(priv, reg, 454 - QCA8K_PORT_PAD_RGMII_EN | 455 - QCA8K_PORT_PAD_RGMII_TX_DELAY(QCA8K_MAX_DELAY) | 456 - QCA8K_PORT_PAD_RGMII_RX_DELAY(QCA8K_MAX_DELAY)); 457 - qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL, 458 - QCA8K_PORT_PAD_RGMII_RX_DELAY_EN); 459 - break; 460 - case PHY_INTERFACE_MODE_SGMII: 461 - qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN); 462 - break; 463 - default: 464 - pr_err("xMII mode %d not supported\n", mode); 465 - return -EINVAL; 466 - } 467 - 468 - return 0; 469 - } 470 - 471 421 static void 472 422 qca8k_port_set_status(struct qca8k_priv *priv, int port, int enable) 473 423 { ··· 591 639 qca8k_setup(struct dsa_switch *ds) 592 640 { 593 641 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; 594 - phy_interface_t phy_mode = PHY_INTERFACE_MODE_NA; 595 642 int ret, i; 596 - u32 mask; 597 643 598 644 /* Make sure that port 0 is the cpu port */ 599 645 if (!dsa_is_cpu_port(ds, 0)) { ··· 611 661 if (ret) 612 662 return ret; 613 663 614 - /* Initialize CPU port pad mode (xMII type, delays...) */ 615 - ret = of_get_phy_mode(dsa_to_port(ds, QCA8K_CPU_PORT)->dn, &phy_mode); 616 - if (ret) { 617 - pr_err("Can't find phy-mode for master device\n"); 618 - return ret; 619 - } 620 - ret = qca8k_set_pad_ctrl(priv, QCA8K_CPU_PORT, phy_mode); 621 - if (ret < 0) 622 - return ret; 623 - 624 - /* Enable CPU Port, force it to maximum bandwidth and full-duplex */ 625 - mask = QCA8K_PORT_STATUS_SPEED_1000 | QCA8K_PORT_STATUS_TXFLOW | 626 - QCA8K_PORT_STATUS_RXFLOW | QCA8K_PORT_STATUS_DUPLEX; 627 - qca8k_write(priv, QCA8K_REG_PORT_STATUS(QCA8K_CPU_PORT), mask); 664 + /* Enable CPU Port */ 628 665 qca8k_reg_set(priv, QCA8K_REG_GLOBAL_FW_CTRL0, 629 666 QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN); 630 - qca8k_port_set_status(priv, QCA8K_CPU_PORT, 1); 631 - priv->port_sts[QCA8K_CPU_PORT].enabled = 1; 632 667 633 668 /* Enable MIB counters */ 634 669 qca8k_mib_init(priv); ··· 628 693 qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i), 629 694 QCA8K_PORT_LOOKUP_MEMBER, 0); 630 695 631 - /* Disable MAC by default on all user ports */ 696 + /* Disable MAC by default on all ports */ 632 697 for (i = 1; i < QCA8K_NUM_PORTS; i++) 633 - if (dsa_is_user_port(ds, i)) 634 - qca8k_port_set_status(priv, i, 0); 698 + qca8k_port_set_status(priv, i, 0); 635 699 636 700 /* Forward all unknown frames to CPU port for Linux processing */ 637 701 qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1, ··· 647 713 QCA8K_PORT_LOOKUP_MEMBER, dsa_user_ports(ds)); 648 714 } 649 715 650 - /* Invividual user ports get connected to CPU port only */ 716 + /* Individual user ports get connected to CPU port only */ 651 717 if (dsa_is_user_port(ds, i)) { 652 718 int shift = 16 * (i % 2); 653 719 ··· 673 739 /* Flush the FDB table */ 674 740 qca8k_fdb_flush(priv); 675 741 742 + /* We don't have interrupts for link changes, so we need to poll */ 743 + ds->pcs_poll = true; 744 + 676 745 return 0; 677 746 } 678 747 679 748 static void 680 - qca8k_adjust_link(struct dsa_switch *ds, int port, struct phy_device *phy) 749 + qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode, 750 + const struct phylink_link_state *state) 751 + { 752 + struct qca8k_priv *priv = ds->priv; 753 + u32 reg, val; 754 + 755 + switch (port) { 756 + case 0: /* 1st CPU port */ 757 + if (state->interface != PHY_INTERFACE_MODE_RGMII && 758 + state->interface != PHY_INTERFACE_MODE_RGMII_ID && 759 + state->interface != PHY_INTERFACE_MODE_SGMII) 760 + return; 761 + 762 + reg = QCA8K_REG_PORT0_PAD_CTRL; 763 + break; 764 + case 1: 765 + case 2: 766 + case 3: 767 + case 4: 768 + case 5: 769 + /* Internal PHY, nothing to do */ 770 + return; 771 + case 6: /* 2nd CPU port / external PHY */ 772 + if (state->interface != PHY_INTERFACE_MODE_RGMII && 773 + state->interface != PHY_INTERFACE_MODE_RGMII_ID && 774 + state->interface != PHY_INTERFACE_MODE_SGMII && 775 + state->interface != PHY_INTERFACE_MODE_1000BASEX) 776 + return; 777 + 778 + reg = QCA8K_REG_PORT6_PAD_CTRL; 779 + break; 780 + default: 781 + dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port); 782 + return; 783 + } 784 + 785 + if (port != 6 && phylink_autoneg_inband(mode)) { 786 + dev_err(ds->dev, "%s: in-band negotiation unsupported\n", 787 + __func__); 788 + return; 789 + } 790 + 791 + switch (state->interface) { 792 + case PHY_INTERFACE_MODE_RGMII: 793 + /* RGMII mode means no delay so don't enable the delay */ 794 + qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN); 795 + break; 796 + case PHY_INTERFACE_MODE_RGMII_ID: 797 + /* RGMII_ID needs internal delay. This is enabled through 798 + * PORT5_PAD_CTRL for all ports, rather than individual port 799 + * registers 800 + */ 801 + qca8k_write(priv, reg, 802 + QCA8K_PORT_PAD_RGMII_EN | 803 + QCA8K_PORT_PAD_RGMII_TX_DELAY(QCA8K_MAX_DELAY) | 804 + QCA8K_PORT_PAD_RGMII_RX_DELAY(QCA8K_MAX_DELAY)); 805 + qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL, 806 + QCA8K_PORT_PAD_RGMII_RX_DELAY_EN); 807 + break; 808 + case PHY_INTERFACE_MODE_SGMII: 809 + case PHY_INTERFACE_MODE_1000BASEX: 810 + /* Enable SGMII on the port */ 811 + qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN); 812 + 813 + /* Enable/disable SerDes auto-negotiation as necessary */ 814 + val = qca8k_read(priv, QCA8K_REG_PWS); 815 + if (phylink_autoneg_inband(mode)) 816 + val &= ~QCA8K_PWS_SERDES_AEN_DIS; 817 + else 818 + val |= QCA8K_PWS_SERDES_AEN_DIS; 819 + qca8k_write(priv, QCA8K_REG_PWS, val); 820 + 821 + /* Configure the SGMII parameters */ 822 + val = qca8k_read(priv, QCA8K_REG_SGMII_CTRL); 823 + 824 + val |= QCA8K_SGMII_EN_PLL | QCA8K_SGMII_EN_RX | 825 + QCA8K_SGMII_EN_TX | QCA8K_SGMII_EN_SD; 826 + 827 + if (dsa_is_cpu_port(ds, port)) { 828 + /* CPU port, we're talking to the CPU MAC, be a PHY */ 829 + val &= ~QCA8K_SGMII_MODE_CTRL_MASK; 830 + val |= QCA8K_SGMII_MODE_CTRL_PHY; 831 + } else if (state->interface == PHY_INTERFACE_MODE_SGMII) { 832 + val &= ~QCA8K_SGMII_MODE_CTRL_MASK; 833 + val |= QCA8K_SGMII_MODE_CTRL_MAC; 834 + } else if (state->interface == PHY_INTERFACE_MODE_1000BASEX) { 835 + val &= ~QCA8K_SGMII_MODE_CTRL_MASK; 836 + val |= QCA8K_SGMII_MODE_CTRL_BASEX; 837 + } 838 + 839 + qca8k_write(priv, QCA8K_REG_SGMII_CTRL, val); 840 + break; 841 + default: 842 + dev_err(ds->dev, "xMII mode %s not supported for port %d\n", 843 + phy_modes(state->interface), port); 844 + return; 845 + } 846 + } 847 + 848 + static void 849 + qca8k_phylink_validate(struct dsa_switch *ds, int port, 850 + unsigned long *supported, 851 + struct phylink_link_state *state) 852 + { 853 + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 854 + 855 + switch (port) { 856 + case 0: /* 1st CPU port */ 857 + if (state->interface != PHY_INTERFACE_MODE_NA && 858 + state->interface != PHY_INTERFACE_MODE_RGMII && 859 + state->interface != PHY_INTERFACE_MODE_RGMII_ID && 860 + state->interface != PHY_INTERFACE_MODE_SGMII) 861 + goto unsupported; 862 + break; 863 + case 1: 864 + case 2: 865 + case 3: 866 + case 4: 867 + case 5: 868 + /* Internal PHY */ 869 + if (state->interface != PHY_INTERFACE_MODE_NA && 870 + state->interface != PHY_INTERFACE_MODE_GMII) 871 + goto unsupported; 872 + break; 873 + case 6: /* 2nd CPU port / external PHY */ 874 + if (state->interface != PHY_INTERFACE_MODE_NA && 875 + state->interface != PHY_INTERFACE_MODE_RGMII && 876 + state->interface != PHY_INTERFACE_MODE_RGMII_ID && 877 + state->interface != PHY_INTERFACE_MODE_SGMII && 878 + state->interface != PHY_INTERFACE_MODE_1000BASEX) 879 + goto unsupported; 880 + break; 881 + default: 882 + unsupported: 883 + linkmode_zero(supported); 884 + return; 885 + } 886 + 887 + phylink_set_port_modes(mask); 888 + phylink_set(mask, Autoneg); 889 + 890 + phylink_set(mask, 1000baseT_Full); 891 + phylink_set(mask, 10baseT_Half); 892 + phylink_set(mask, 10baseT_Full); 893 + phylink_set(mask, 100baseT_Half); 894 + phylink_set(mask, 100baseT_Full); 895 + 896 + if (state->interface == PHY_INTERFACE_MODE_1000BASEX) 897 + phylink_set(mask, 1000baseX_Full); 898 + 899 + phylink_set(mask, Pause); 900 + phylink_set(mask, Asym_Pause); 901 + 902 + linkmode_and(supported, supported, mask); 903 + linkmode_and(state->advertising, state->advertising, mask); 904 + } 905 + 906 + static int 907 + qca8k_phylink_mac_link_state(struct dsa_switch *ds, int port, 908 + struct phylink_link_state *state) 681 909 { 682 910 struct qca8k_priv *priv = ds->priv; 683 911 u32 reg; 684 912 685 - /* Force fixed-link setting for CPU port, skip others. */ 686 - if (!phy_is_pseudo_fixed_link(phy)) 687 - return; 913 + reg = qca8k_read(priv, QCA8K_REG_PORT_STATUS(port)); 688 914 689 - /* Set port speed */ 690 - switch (phy->speed) { 691 - case 10: 692 - reg = QCA8K_PORT_STATUS_SPEED_10; 915 + state->link = !!(reg & QCA8K_PORT_STATUS_LINK_UP); 916 + state->an_complete = state->link; 917 + state->an_enabled = !!(reg & QCA8K_PORT_STATUS_LINK_AUTO); 918 + state->duplex = (reg & QCA8K_PORT_STATUS_DUPLEX) ? DUPLEX_FULL : 919 + DUPLEX_HALF; 920 + 921 + switch (reg & QCA8K_PORT_STATUS_SPEED) { 922 + case QCA8K_PORT_STATUS_SPEED_10: 923 + state->speed = SPEED_10; 693 924 break; 694 - case 100: 695 - reg = QCA8K_PORT_STATUS_SPEED_100; 925 + case QCA8K_PORT_STATUS_SPEED_100: 926 + state->speed = SPEED_100; 696 927 break; 697 - case 1000: 698 - reg = QCA8K_PORT_STATUS_SPEED_1000; 928 + case QCA8K_PORT_STATUS_SPEED_1000: 929 + state->speed = SPEED_1000; 699 930 break; 700 931 default: 701 - dev_dbg(priv->dev, "port%d link speed %dMbps not supported.\n", 702 - port, phy->speed); 703 - return; 932 + state->speed = SPEED_UNKNOWN; 933 + break; 704 934 } 705 935 706 - /* Set duplex mode */ 707 - if (phy->duplex == DUPLEX_FULL) 708 - reg |= QCA8K_PORT_STATUS_DUPLEX; 936 + state->pause = MLO_PAUSE_NONE; 937 + if (reg & QCA8K_PORT_STATUS_RXFLOW) 938 + state->pause |= MLO_PAUSE_RX; 939 + if (reg & QCA8K_PORT_STATUS_TXFLOW) 940 + state->pause |= MLO_PAUSE_TX; 709 941 710 - /* Force flow control */ 711 - if (dsa_is_cpu_port(ds, port)) 712 - reg |= QCA8K_PORT_STATUS_RXFLOW | QCA8K_PORT_STATUS_TXFLOW; 942 + return 1; 943 + } 713 944 714 - /* Force link down before changing MAC options */ 945 + static void 946 + qca8k_phylink_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode, 947 + phy_interface_t interface) 948 + { 949 + struct qca8k_priv *priv = ds->priv; 950 + 715 951 qca8k_port_set_status(priv, port, 0); 952 + } 953 + 954 + static void 955 + qca8k_phylink_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode, 956 + phy_interface_t interface, struct phy_device *phydev, 957 + int speed, int duplex, bool tx_pause, bool rx_pause) 958 + { 959 + struct qca8k_priv *priv = ds->priv; 960 + u32 reg; 961 + 962 + if (phylink_autoneg_inband(mode)) { 963 + reg = QCA8K_PORT_STATUS_LINK_AUTO; 964 + } else { 965 + switch (speed) { 966 + case SPEED_10: 967 + reg = QCA8K_PORT_STATUS_SPEED_10; 968 + break; 969 + case SPEED_100: 970 + reg = QCA8K_PORT_STATUS_SPEED_100; 971 + break; 972 + case SPEED_1000: 973 + reg = QCA8K_PORT_STATUS_SPEED_1000; 974 + break; 975 + default: 976 + reg = QCA8K_PORT_STATUS_LINK_AUTO; 977 + break; 978 + } 979 + 980 + if (duplex == DUPLEX_FULL) 981 + reg |= QCA8K_PORT_STATUS_DUPLEX; 982 + 983 + if (rx_pause || dsa_is_cpu_port(ds, port)) 984 + reg |= QCA8K_PORT_STATUS_RXFLOW; 985 + 986 + if (tx_pause || dsa_is_cpu_port(ds, port)) 987 + reg |= QCA8K_PORT_STATUS_TXFLOW; 988 + } 989 + 990 + reg |= QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC; 991 + 716 992 qca8k_write(priv, QCA8K_REG_PORT_STATUS(port), reg); 717 - qca8k_port_set_status(priv, port, 1); 718 993 } 719 994 720 995 static void ··· 1080 937 { 1081 938 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; 1082 939 1083 - if (!dsa_is_user_port(ds, port)) 1084 - return 0; 1085 - 1086 940 qca8k_port_set_status(priv, port, 1); 1087 941 priv->port_sts[port].enabled = 1; 1088 942 1089 - phy_support_asym_pause(phy); 943 + if (dsa_is_user_port(ds, port)) 944 + phy_support_asym_pause(phy); 1090 945 1091 946 return 0; 1092 947 } ··· 1167 1026 static const struct dsa_switch_ops qca8k_switch_ops = { 1168 1027 .get_tag_protocol = qca8k_get_tag_protocol, 1169 1028 .setup = qca8k_setup, 1170 - .adjust_link = qca8k_adjust_link, 1171 1029 .get_strings = qca8k_get_strings, 1172 1030 .get_ethtool_stats = qca8k_get_ethtool_stats, 1173 1031 .get_sset_count = qca8k_get_sset_count, ··· 1180 1040 .port_fdb_add = qca8k_port_fdb_add, 1181 1041 .port_fdb_del = qca8k_port_fdb_del, 1182 1042 .port_fdb_dump = qca8k_port_fdb_dump, 1043 + .phylink_validate = qca8k_phylink_validate, 1044 + .phylink_mac_link_state = qca8k_phylink_mac_link_state, 1045 + .phylink_mac_config = qca8k_phylink_mac_config, 1046 + .phylink_mac_link_down = qca8k_phylink_mac_link_down, 1047 + .phylink_mac_link_up = qca8k_phylink_mac_link_up, 1183 1048 }; 1184 1049 1185 1050 static int
+13
drivers/net/dsa/qca8k.h
··· 36 36 #define QCA8K_MAX_DELAY 3 37 37 #define QCA8K_PORT_PAD_RGMII_RX_DELAY_EN BIT(24) 38 38 #define QCA8K_PORT_PAD_SGMII_EN BIT(7) 39 + #define QCA8K_REG_PWS 0x010 40 + #define QCA8K_PWS_SERDES_AEN_DIS BIT(7) 39 41 #define QCA8K_REG_MODULE_EN 0x030 40 42 #define QCA8K_MODULE_EN_MIB BIT(0) 41 43 #define QCA8K_REG_MIB 0x034 ··· 71 69 #define QCA8K_PORT_STATUS_LINK_UP BIT(8) 72 70 #define QCA8K_PORT_STATUS_LINK_AUTO BIT(9) 73 71 #define QCA8K_PORT_STATUS_LINK_PAUSE BIT(10) 72 + #define QCA8K_PORT_STATUS_FLOW_AUTO BIT(12) 74 73 #define QCA8K_REG_PORT_HDR_CTRL(_i) (0x9c + (_i * 4)) 75 74 #define QCA8K_PORT_HDR_CTRL_RX_MASK GENMASK(3, 2) 76 75 #define QCA8K_PORT_HDR_CTRL_RX_S 2 ··· 80 77 #define QCA8K_PORT_HDR_CTRL_ALL 2 81 78 #define QCA8K_PORT_HDR_CTRL_MGMT 1 82 79 #define QCA8K_PORT_HDR_CTRL_NONE 0 80 + #define QCA8K_REG_SGMII_CTRL 0x0e0 81 + #define QCA8K_SGMII_EN_PLL BIT(1) 82 + #define QCA8K_SGMII_EN_RX BIT(2) 83 + #define QCA8K_SGMII_EN_TX BIT(3) 84 + #define QCA8K_SGMII_EN_SD BIT(4) 85 + #define QCA8K_SGMII_CLK125M_DELAY BIT(7) 86 + #define QCA8K_SGMII_MODE_CTRL_MASK (BIT(22) | BIT(23)) 87 + #define QCA8K_SGMII_MODE_CTRL_BASEX (0 << 22) 88 + #define QCA8K_SGMII_MODE_CTRL_PHY (1 << 22) 89 + #define QCA8K_SGMII_MODE_CTRL_MAC (2 << 22) 83 90 84 91 /* EEE control registers */ 85 92 #define QCA8K_REG_EEE_CTRL 0x100