Skip to content

Commit ffe10e6

Browse files
vladimirolteandavem330
authored andcommitted
net: dsa: sja1105: Add support for the SGMII port
SJA1105 switches R and S have one SerDes port with an 802.3z quasi-compatible PCS, hardwired on port 4. The other ports are still MII/RMII/RGMII. The PCS performs rate adaptation to lower link speeds; the MAC on this port is hardwired at gigabit. Only full duplex is supported. The SGMII port can be configured as part of the static config tables, as well as through a dedicated SPI address region for its pseudo-clause-22 registers. However it looks like the static configuration is not able to change some out-of-reset values (like the value of MII_BMCR), so at the end of the day, having code for it is utterly pointless. We are just going to use the pseudo-C22 interface. Because the PCS gets reset when the switch resets, we have to add even more restoration logic to sja1105_static_config_reload, otherwise the SGMII port breaks after operations such as enabling PTP timestamping which require a switch reset. >From PHYLINK perspective, the switch supports *only* SGMII (it doesn't support 1000Base-X). It also doesn't expose access to the raw config word for in-band AN in registers MII_ADV/MII_LPA. It is able to work in the following modes: - Forced speed - SGMII in-band AN slave (speed received from PHY) - SGMII in-band AN master (acting as a PHY) The latter mode is not supported by this patch. It is even unclear to me how that would be described. There is some code for it left in the patch, but 'an_master' is always passed as false. Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Reviewed-by: Russell King <rmk+kernel@armlinux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f6e94ff commit ffe10e6

5 files changed

Lines changed: 244 additions & 5 deletions

File tree

‎drivers/net/dsa/sja1105/sja1105.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct sja1105_regs {
3636
u64 port_control;
3737
u64 rgu;
3838
u64 config;
39+
u64 sgmii;
3940
u64 rmii_pll1;
4041
u64 ptp_control;
4142
u64 ptpclkval;
@@ -159,6 +160,7 @@ typedef enum {
159160
XMII_MODE_MII = 0,
160161
XMII_MODE_RMII = 1,
161162
XMII_MODE_RGMII = 2,
163+
XMII_MODE_SGMII = 3,
162164
} sja1105_phy_interface_t;
163165

164166
typedef enum {

‎drivers/net/dsa/sja1105/sja1105_clocking.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ int sja1105_clocking_setup_port(struct sja1105_private *priv, int port)
660660
case XMII_MODE_RGMII:
661661
rc = sja1105_rgmii_clocking_setup(priv, port, role);
662662
break;
663+
case XMII_MODE_SGMII:
664+
/* Nothing to do in the CGU for SGMII */
665+
rc = 0;
666+
break;
663667
default:
664668
dev_err(dev, "Invalid interface mode specified: %d\n",
665669
phy_mode);

‎drivers/net/dsa/sja1105/sja1105_main.c‎

Lines changed: 184 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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

2728
static 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+
138154
static 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 */
654761
static 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

765879
static 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

783902
static 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, 10baseT_Full);
834956
phylink_set(mask, 100baseT_Full);
835957
phylink_set(mask, 100baseT1_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, 1000baseT_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+
844999
static int
8451000
sja1105_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+
}
14361614
out:
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,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
/* Copyright 2020, NXP Semiconductors
3+
*/
4+
#ifndef _SJA1105_SGMII_H
5+
#define _SJA1105_SGMII_H
6+
7+
#define SJA1105_SGMII_PORT 4
8+
9+
/* DIGITAL_CONTROL_1 (address 1f8000h) */
10+
#define SJA1105_DC1 0x8000
11+
#define SJA1105_DC1_VS_RESET BIT(15)
12+
#define SJA1105_DC1_REMOTE_LOOPBACK BIT(14)
13+
#define SJA1105_DC1_EN_VSMMD1 BIT(13)
14+
#define SJA1105_DC1_POWER_SAVE BIT(11)
15+
#define SJA1105_DC1_CLOCK_STOP_EN BIT(10)
16+
#define SJA1105_DC1_MAC_AUTO_SW BIT(9)
17+
#define SJA1105_DC1_INIT BIT(8)
18+
#define SJA1105_DC1_TX_DISABLE BIT(4)
19+
#define SJA1105_DC1_AUTONEG_TIMER_OVRR BIT(3)
20+
#define SJA1105_DC1_BYP_POWERUP BIT(1)
21+
#define SJA1105_DC1_PHY_MODE_CONTROL BIT(0)
22+
23+
/* DIGITAL_CONTROL_2 register (address 1f80E1h) */
24+
#define SJA1105_DC2 0x80e1
25+
#define SJA1105_DC2_TX_POL_INV_DISABLE BIT(4)
26+
#define SJA1105_DC2_RX_POL_INV BIT(0)
27+
28+
/* DIGITAL_ERROR_CNT register (address 1f80E2h) */
29+
#define SJA1105_DEC 0x80e2
30+
#define SJA1105_DEC_ICG_EC_ENA BIT(4)
31+
#define SJA1105_DEC_CLEAR_ON_READ BIT(0)
32+
33+
/* AUTONEG_CONTROL register (address 1f8001h) */
34+
#define SJA1105_AC 0x8001
35+
#define SJA1105_AC_MII_CONTROL BIT(8)
36+
#define SJA1105_AC_SGMII_LINK BIT(4)
37+
#define SJA1105_AC_PHY_MODE BIT(3)
38+
#define SJA1105_AC_AUTONEG_MODE(x) (((x) << 1) & GENMASK(2, 1))
39+
#define SJA1105_AC_AUTONEG_MODE_SGMII SJA1105_AC_AUTONEG_MODE(2)
40+
41+
/* AUTONEG_INTR_STATUS register (address 1f8002h) */
42+
#define SJA1105_AIS 0x8002
43+
#define SJA1105_AIS_LINK_STATUS(x) (!!((x) & BIT(4)))
44+
#define SJA1105_AIS_SPEED(x) (((x) & GENMASK(3, 2)) >> 2)
45+
#define SJA1105_AIS_DUPLEX_MODE(x) (!!((x) & BIT(1)))
46+
#define SJA1105_AIS_COMPLETE(x) (!!((x) & BIT(0)))
47+
48+
/* DEBUG_CONTROL register (address 1f8005h) */
49+
#define SJA1105_DC 0x8005
50+
#define SJA1105_DC_SUPPRESS_LOS BIT(4)
51+
#define SJA1105_DC_RESTART_SYNC BIT(0)
52+
53+
#endif

‎drivers/net/dsa/sja1105/sja1105_spi.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ static struct sja1105_regs sja1105pqrs_regs = {
474474
/* UM10944.pdf, Table 86, ACU Register overview */
475475
.pad_mii_tx = {0x100800, 0x100802, 0x100804, 0x100806, 0x100808},
476476
.pad_mii_id = {0x100810, 0x100811, 0x100812, 0x100813, 0x100814},
477+
.sgmii = 0x1F0000,
477478
.rmii_pll1 = 0x10000A,
478479
.cgu_idiv = {0x10000B, 0x10000C, 0x10000D, 0x10000E, 0x10000F},
479480
.mac = {0x200, 0x202, 0x204, 0x206, 0x208},

0 commit comments

Comments
 (0)