2222#include <linux/if_ether.h>
2323#include <linux/dsa/8021q.h>
2424#include "sja1105.h"
25+ #include "sja1105_sgmii.h"
2526#include "sja1105_tas.h"
2627
2728static void sja1105_hw_reset (struct gpio_desc * gpio , unsigned int pulse_len ,
@@ -135,6 +136,21 @@ static int sja1105_init_mac_settings(struct sja1105_private *priv)
135136 return 0 ;
136137}
137138
139+ static bool sja1105_supports_sgmii (struct sja1105_private * priv , int port )
140+ {
141+ if (priv -> info -> part_no != SJA1105R_PART_NO &&
142+ priv -> info -> part_no != SJA1105S_PART_NO )
143+ return false;
144+
145+ if (port != SJA1105_SGMII_PORT )
146+ return false;
147+
148+ if (dsa_is_unused_port (priv -> ds , port ))
149+ return false;
150+
151+ return true;
152+ }
153+
138154static int sja1105_init_mii_settings (struct sja1105_private * priv ,
139155 struct sja1105_dt_port * ports )
140156{
@@ -178,12 +194,24 @@ static int sja1105_init_mii_settings(struct sja1105_private *priv,
178194 case PHY_INTERFACE_MODE_RGMII_TXID :
179195 mii -> xmii_mode [i ] = XMII_MODE_RGMII ;
180196 break ;
197+ case PHY_INTERFACE_MODE_SGMII :
198+ if (!sja1105_supports_sgmii (priv , i ))
199+ return - EINVAL ;
200+ mii -> xmii_mode [i ] = XMII_MODE_SGMII ;
201+ break ;
181202 default :
182203 dev_err (dev , "Unsupported PHY mode %s!\n" ,
183204 phy_modes (ports [i ].phy_mode ));
184205 }
185206
186- mii -> phy_mac [i ] = ports [i ].role ;
207+ /* Even though the SerDes port is able to drive SGMII autoneg
208+ * like a PHY would, from the perspective of the XMII tables,
209+ * the SGMII port should always be put in MAC mode.
210+ */
211+ if (ports [i ].phy_mode == PHY_INTERFACE_MODE_SGMII )
212+ mii -> phy_mac [i ] = XMII_MAC ;
213+ else
214+ mii -> phy_mac [i ] = ports [i ].role ;
187215 }
188216 return 0 ;
189217}
@@ -650,6 +678,85 @@ static int sja1105_parse_dt(struct sja1105_private *priv,
650678 return rc ;
651679}
652680
681+ static int sja1105_sgmii_read (struct sja1105_private * priv , int pcs_reg )
682+ {
683+ const struct sja1105_regs * regs = priv -> info -> regs ;
684+ u32 val ;
685+ int rc ;
686+
687+ rc = sja1105_xfer_u32 (priv , SPI_READ , regs -> sgmii + pcs_reg , & val ,
688+ NULL );
689+ if (rc < 0 )
690+ return rc ;
691+
692+ return val ;
693+ }
694+
695+ static int sja1105_sgmii_write (struct sja1105_private * priv , int pcs_reg ,
696+ u16 pcs_val )
697+ {
698+ const struct sja1105_regs * regs = priv -> info -> regs ;
699+ u32 val = pcs_val ;
700+ int rc ;
701+
702+ rc = sja1105_xfer_u32 (priv , SPI_WRITE , regs -> sgmii + pcs_reg , & val ,
703+ NULL );
704+ if (rc < 0 )
705+ return rc ;
706+
707+ return val ;
708+ }
709+
710+ static void sja1105_sgmii_pcs_config (struct sja1105_private * priv ,
711+ bool an_enabled , bool an_master )
712+ {
713+ u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII ;
714+
715+ /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
716+ * stop the clock during LPI mode, make the MAC reconfigure
717+ * autonomously after PCS autoneg is done, flush the internal FIFOs.
718+ */
719+ sja1105_sgmii_write (priv , SJA1105_DC1 , SJA1105_DC1_EN_VSMMD1 |
720+ SJA1105_DC1_CLOCK_STOP_EN |
721+ SJA1105_DC1_MAC_AUTO_SW |
722+ SJA1105_DC1_INIT );
723+ /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
724+ sja1105_sgmii_write (priv , SJA1105_DC2 , SJA1105_DC2_TX_POL_INV_DISABLE );
725+ /* AUTONEG_CONTROL: Use SGMII autoneg */
726+ if (an_master )
727+ ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK ;
728+ sja1105_sgmii_write (priv , SJA1105_AC , ac );
729+ /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
730+ * sja1105_sgmii_pcs_force_speed must be called later for the link
731+ * to become operational.
732+ */
733+ if (an_enabled )
734+ sja1105_sgmii_write (priv , MII_BMCR ,
735+ BMCR_ANENABLE | BMCR_ANRESTART );
736+ }
737+
738+ static void sja1105_sgmii_pcs_force_speed (struct sja1105_private * priv ,
739+ int speed )
740+ {
741+ int pcs_speed ;
742+
743+ switch (speed ) {
744+ case SPEED_1000 :
745+ pcs_speed = BMCR_SPEED1000 ;
746+ break ;
747+ case SPEED_100 :
748+ pcs_speed = BMCR_SPEED100 ;
749+ break ;
750+ case SPEED_10 :
751+ pcs_speed = BMCR_SPEED10 ;
752+ break ;
753+ default :
754+ dev_err (priv -> ds -> dev , "Invalid speed %d\n" , speed );
755+ return ;
756+ }
757+ sja1105_sgmii_write (priv , MII_BMCR , pcs_speed | BMCR_FULLDPLX );
758+ }
759+
653760/* Convert link speed from SJA1105 to ethtool encoding */
654761static int sja1105_speed [] = {
655762 [SJA1105_SPEED_AUTO ] = SPEED_UNKNOWN ,
@@ -707,8 +814,13 @@ static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
707814 * table, since this will be used for the clocking setup, and we no
708815 * longer need to store it in the static config (already told hardware
709816 * we want auto during upload phase).
817+ * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
818+ * we need to configure the PCS only (if even that).
710819 */
711- mac [port ].speed = speed ;
820+ if (sja1105_supports_sgmii (priv , port ))
821+ mac [port ].speed = SJA1105_SPEED_1000MBPS ;
822+ else
823+ mac [port ].speed = speed ;
712824
713825 /* Write to the dynamic reconfiguration tables */
714826 rc = sja1105_dynamic_config_write (priv , BLK_IDX_MAC_CONFIG , port ,
@@ -757,27 +869,34 @@ static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
757869 case PHY_INTERFACE_MODE_RGMII_RXID :
758870 case PHY_INTERFACE_MODE_RGMII_TXID :
759871 return (phy_mode != XMII_MODE_RGMII );
872+ case PHY_INTERFACE_MODE_SGMII :
873+ return (phy_mode != XMII_MODE_SGMII );
760874 default :
761875 return true;
762876 }
763877}
764878
765879static void sja1105_mac_config (struct dsa_switch * ds , int port ,
766- unsigned int link_an_mode ,
880+ unsigned int mode ,
767881 const struct phylink_link_state * state )
768882{
769883 struct sja1105_private * priv = ds -> priv ;
884+ bool is_sgmii = sja1105_supports_sgmii (priv , port );
770885
771886 if (sja1105_phy_mode_mismatch (priv , port , state -> interface )) {
772887 dev_err (ds -> dev , "Changing PHY mode to %s not supported!\n" ,
773888 phy_modes (state -> interface ));
774889 return ;
775890 }
776891
777- if (link_an_mode == MLO_AN_INBAND ) {
892+ if (phylink_autoneg_inband ( mode ) && ! is_sgmii ) {
778893 dev_err (ds -> dev , "In-band AN not supported!\n" );
779894 return ;
780895 }
896+
897+ if (is_sgmii )
898+ sja1105_sgmii_pcs_config (priv , phylink_autoneg_inband (mode ),
899+ false);
781900}
782901
783902static void sja1105_mac_link_down (struct dsa_switch * ds , int port ,
@@ -798,6 +917,9 @@ static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
798917
799918 sja1105_adjust_port_config (priv , port , speed );
800919
920+ if (sja1105_supports_sgmii (priv , port ) && !phylink_autoneg_inband (mode ))
921+ sja1105_sgmii_pcs_force_speed (priv , speed );
922+
801923 sja1105_inhibit_tx (priv , BIT (port ), false);
802924}
803925
@@ -833,14 +955,47 @@ static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
833955 phylink_set (mask , 10b aseT_Full );
834956 phylink_set (mask , 100b aseT_Full );
835957 phylink_set (mask , 100b aseT1_Full );
836- if (mii -> xmii_mode [port ] == XMII_MODE_RGMII )
958+ if (mii -> xmii_mode [port ] == XMII_MODE_RGMII ||
959+ mii -> xmii_mode [port ] == XMII_MODE_SGMII )
837960 phylink_set (mask , 1000b aseT_Full );
838961
839962 bitmap_and (supported , supported , mask , __ETHTOOL_LINK_MODE_MASK_NBITS );
840963 bitmap_and (state -> advertising , state -> advertising , mask ,
841964 __ETHTOOL_LINK_MODE_MASK_NBITS );
842965}
843966
967+ static int sja1105_mac_pcs_get_state (struct dsa_switch * ds , int port ,
968+ struct phylink_link_state * state )
969+ {
970+ struct sja1105_private * priv = ds -> priv ;
971+ int ais ;
972+
973+ /* Read the vendor-specific AUTONEG_INTR_STATUS register */
974+ ais = sja1105_sgmii_read (priv , SJA1105_AIS );
975+ if (ais < 0 )
976+ return ais ;
977+
978+ switch (SJA1105_AIS_SPEED (ais )) {
979+ case 0 :
980+ state -> speed = SPEED_10 ;
981+ break ;
982+ case 1 :
983+ state -> speed = SPEED_100 ;
984+ break ;
985+ case 2 :
986+ state -> speed = SPEED_1000 ;
987+ break ;
988+ default :
989+ dev_err (ds -> dev , "Invalid SGMII PCS speed %lu\n" ,
990+ SJA1105_AIS_SPEED (ais ));
991+ }
992+ state -> duplex = SJA1105_AIS_DUPLEX_MODE (ais );
993+ state -> an_complete = SJA1105_AIS_COMPLETE (ais );
994+ state -> link = SJA1105_AIS_LINK_STATUS (ais );
995+
996+ return 0 ;
997+ }
998+
844999static int
8451000sja1105_find_static_fdb_entry (struct sja1105_private * priv , int port ,
8461001 const struct sja1105_l2_lookup_entry * requested )
@@ -1367,6 +1522,7 @@ int sja1105_static_config_reload(struct sja1105_private *priv,
13671522 struct dsa_switch * ds = priv -> ds ;
13681523 s64 t1 , t2 , t3 , t4 ;
13691524 s64 t12 , t34 ;
1525+ u16 bmcr = 0 ;
13701526 int rc , i ;
13711527 s64 now ;
13721528
@@ -1384,6 +1540,9 @@ int sja1105_static_config_reload(struct sja1105_private *priv,
13841540 mac [i ].speed = SJA1105_SPEED_AUTO ;
13851541 }
13861542
1543+ if (sja1105_supports_sgmii (priv , SJA1105_SGMII_PORT ))
1544+ bmcr = sja1105_sgmii_read (priv , MII_BMCR );
1545+
13871546 /* No PTP operations can run right now */
13881547 mutex_lock (& priv -> ptp_data .lock );
13891548
@@ -1433,6 +1592,25 @@ int sja1105_static_config_reload(struct sja1105_private *priv,
14331592 if (rc < 0 )
14341593 goto out ;
14351594 }
1595+
1596+ if (sja1105_supports_sgmii (priv , SJA1105_SGMII_PORT )) {
1597+ bool an_enabled = !!(bmcr & BMCR_ANENABLE );
1598+
1599+ sja1105_sgmii_pcs_config (priv , an_enabled , false);
1600+
1601+ if (!an_enabled ) {
1602+ int speed = SPEED_UNKNOWN ;
1603+
1604+ if (bmcr & BMCR_SPEED1000 )
1605+ speed = SPEED_1000 ;
1606+ else if (bmcr & BMCR_SPEED100 )
1607+ speed = SPEED_100 ;
1608+ else if (bmcr & BMCR_SPEED10 )
1609+ speed = SPEED_10 ;
1610+
1611+ sja1105_sgmii_pcs_force_speed (priv , speed );
1612+ }
1613+ }
14361614out :
14371615 mutex_unlock (& priv -> mgmt_lock );
14381616
@@ -1998,6 +2176,7 @@ static const struct dsa_switch_ops sja1105_switch_ops = {
19982176 .teardown = sja1105_teardown ,
19992177 .set_ageing_time = sja1105_set_ageing_time ,
20002178 .phylink_validate = sja1105_phylink_validate ,
2179+ .phylink_mac_link_state = sja1105_mac_pcs_get_state ,
20012180 .phylink_mac_config = sja1105_mac_config ,
20022181 .phylink_mac_link_up = sja1105_mac_link_up ,
20032182 .phylink_mac_link_down = sja1105_mac_link_down ,
0 commit comments