Skip to contentSkip to conversation

net: dsa: MT7628 embedded switch initial support

RFC Superseded Joris Vaisvila 3 participants 10 messages Mar 14 – Mar 21

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: dsa: MT7628 embedded switch initial support

Hello,

This RFC series adds initial support for the MT7628 Embedded Switch.

The implementation provides basic functionality required to operate the switch using DSA. The hardware exposes 5 internal Fast Ethernet ports and two gigabit ports, one of which is internally connected to the CPU MAC.

Tested on MT7628NN (mistyped as mt7628AN in v1) with 5x Fast Ethernet ports in a software bridge. All ports were verified for correct communication while using VLAN-tagged and untagged traffic between two hosts.

---

v2:
- Update commit messages with more relevant information
- Change all references of "special" tag to "MT7628" tag
- Fix coding style errors
- Use dsa_xmit_port_mask instead of BIT(dp->index)
- Replace phy_read/phy_write with an MDIO bus definition
- Remove PHY init from switch driver
- Fix undocumented writes to SGC2 and FCT0
- Fix missing double-tag enable required for VLAN unaware functionality
- Fix VLAN remove logic
- Update VLAN add logic to use the first unused slot
- Add a dedicated phy driver for MT7628 FE PHYs

v1: https://lore.kernel.org/netdev/[email protected]

Joris Vaisvila (3):
net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet
PHYs
net: dsa: initial MT7628 tagging driver
net: dsa: initial support for MT7628 embedded switch

 drivers/net/dsa/Kconfig               |   7 +
 drivers/net/dsa/Makefile              |   1 +
 drivers/net/dsa/mt7628.c              | 596 ++++++++++++++++++++++++++
 drivers/net/phy/mediatek/Kconfig      |   6 +
 drivers/net/phy/mediatek/Makefile     |   1 +
 drivers/net/phy/mediatek/mtk-fe-soc.c |  55 +++
 include/net/dsa.h                     |   2 +
 net/dsa/Kconfig                       |   6 +
 net/dsa/Makefile                      |   1 +
 net/dsa/tag_mt7628.c                  |  93 ++++
 10 files changed, 768 insertions(+)
 create mode 100644 drivers/net/dsa/mt7628.c
 create mode 100644 drivers/net/phy/mediatek/mtk-fe-soc.c
 create mode 100644 net/dsa/tag_mt7628.c
Signature · 1 line
2.53.0
#1 lore raw

RFC v2 1/3 net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet PHYs

The Fast Ethernet PHYs present in the MT7628 SoCs require an undocumented bit to be set before they can establish 100mbps links.

This commit adds the Kconfig option MEDIATEK_FE_SOC_PHY and the corresponding driver mtk-fe-soc.c.

---
 drivers/net/phy/mediatek/Kconfig      |  6 +++
 drivers/net/phy/mediatek/Makefile     |  1 +
 drivers/net/phy/mediatek/mtk-fe-soc.c | 54 +++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 drivers/net/phy/mediatek/mtk-fe-soc.c
drivers/net/phy/mediatek/Kconfig +6 -0
index bb7dc876271e..62355d46cb1e 100644
@@ -36,5 +36,11 @@ config MEDIATEK_GE_SOC_PHY
 	  present in the SoCs efuse and will dynamically calibrate VCM
 	  (common-mode voltage) during startup.
 
+config MEDIATEK_FE_SOC_PHY
+	tristate "MediaTek MT7628 SoC Ethernet PHYs"
+	select PHY_PACKAGE
+	help
+	  Support for MediaTek MT7628 built-in Fast Ethernet PHYs.
+
 config MTK_NET_PHYLIB
 	tristate
drivers/net/phy/mediatek/Makefile +1 -0
index ac57ecc799fc..54cea813c7ad 100644
@@ -2,4 +2,5 @@
 obj-$(CONFIG_MEDIATEK_2P5GE_PHY)	+= mtk-2p5ge.o
 obj-$(CONFIG_MEDIATEK_GE_PHY)		+= mtk-ge.o
 obj-$(CONFIG_MEDIATEK_GE_SOC_PHY)	+= mtk-ge-soc.o
+obj-$(CONFIG_MEDIATEK_FE_SOC_PHY)	+= mtk-fe-soc.o
 obj-$(CONFIG_MTK_NET_PHYLIB)		+= mtk-phy-lib.o
drivers/net/phy/mediatek/mtk-fe-soc.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [RFC v2 1/3] net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet PHYs

Quoted 5 lines
drivers/net/phy/mediatek/Kconfig +0 -0
index bb7dc876271e..62355d46cb1e 100644

drivers/net/phy/mediatek/Kconfig config MEDIATEK_GE_SOC_PHY · line 44

Quoted 9 lines
drivers/net/phy/mediatek/Kconfig +6 -0
@@ -36,5 +36,11 @@ config MEDIATEK_GE_SOC_PHY
 	  present in the SoCs efuse and will dynamically calibrate VCM
 	  (common-mode voltage) during startup.
 
+config MEDIATEK_FE_SOC_PHY
+	tristate "MediaTek MT7628 SoC Ethernet PHYs"
+	select PHY_PACKAGE
+	help
+	  Support for MediaTek MT7628 built-in Fast Ethernet PHYs.
+

This file is sorted by tristate string.

It might be best to change:

config MEDIATEK_GE_SOC_PHY
- tristate "MediaTek SoC Ethernet PHYs"
+ tristate "MediaTek SoC Gigabit Ethernet PHYs"

And then you can add

drivers/net/phy/mediatek/Kconfig config MEDIATEK_GE_SOC_PHY · line 46

Quoted 2 lines
drivers/net/phy/mediatek/Kconfig +2 -0
+config MEDIATEK_FE_SOC_PHY
+	tristate "MediaTek SoC Fast Ethernet PHYs"

before?

Also, why do you have select PHY_PACKAGE?

Quoted 5 lines

obj-$(CONFIG_MEDIATEK_2P5GE_PHY) += mtk-2p5ge.o
obj-$(CONFIG_MEDIATEK_GE_PHY) += mtk-ge.o
obj-$(CONFIG_MEDIATEK_GE_SOC_PHY) += mtk-ge-soc.o
+obj-$(CONFIG_MEDIATEK_FE_SOC_PHY) += mtk-fe-soc.o
obj-$(CONFIG_MTK_NET_PHYLIB) += mtk-phy-lib.o

Please sort alphabetically.

drivers/net/phy/mediatek/Kconfig

Quoted 7 lines
drivers/net/phy/mediatek/Kconfig +7 -0
+static struct phy_driver mtk_soc_fe_phy_driver[] = {
+	{
+		PHY_ID_MATCH_EXACT(MTK_FPHY_ID_MT7628),
+		.name = "MediaTek MT7628 PHY",
+		.config_init = mt7628_phy_config_init,
+		.config_intr = genphy_no_config_intr,
+		.handle_interrupt = genphy_handle_interrupt_no_ack,

How do interrupts work in this device? Has this been simply cut/pasted, or have interrupts been tested using these functions?

Andrew

#3 lore raw

Re: [RFC v2 1/3] net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet PHYs

Hi Andrew, thanks for the feedback.

Quoted 15 lines · Andrew Lunn

drivers/net/phy/mediatek/Kconfig config MEDIATEK_GE_SOC_PHY · line 44

Quoted 6 lines
drivers/net/phy/mediatek/Kconfig +6 -0
+config MEDIATEK_FE_SOC_PHY
+	tristate "MediaTek MT7628 SoC Ethernet PHYs"
+	select PHY_PACKAGE
+	help
+	  Support for MediaTek MT7628 built-in Fast Ethernet PHYs.
+

This file is sorted by tristate string.

It might be best to change:

config MEDIATEK_GE_SOC_PHY

(quoted diff) +1 -1
-        tristate "MediaTek SoC Ethernet PHYs"
+        tristate "MediaTek SoC Gigabit Ethernet PHYs"
Quoted 7 lines

And then you can add

Review

Quoted 2 lines
(quoted diff) +2 -0
+config MEDIATEK_FE_SOC_PHY
+	tristate "MediaTek SoC Fast Ethernet PHYs"

before?
Also, why do you have select PHY_PACKAGE?

Will fix naming and sorting in v3. PHY_PACKAGE is not needed, remaint from testing.

Quoted 7 lines
Quoted 5 lines

obj-$(CONFIG_MEDIATEK_2P5GE_PHY) += mtk-2p5ge.o
obj-$(CONFIG_MEDIATEK_GE_PHY) += mtk-ge.o
obj-$(CONFIG_MEDIATEK_GE_SOC_PHY) += mtk-ge-soc.o
+obj-$(CONFIG_MEDIATEK_FE_SOC_PHY) += mtk-fe-soc.o
obj-$(CONFIG_MTK_NET_PHYLIB) += mtk-phy-lib.o

Please sort alphabetically.

will fix in v3.

Quoted 11 lines

drivers/net/phy/mediatek/mtk-fe-soc.c line 36

Quoted 7 lines
drivers/net/phy/mediatek/mtk-fe-soc.c new file +7 -0
+static struct phy_driver mtk_soc_fe_phy_driver[] = {
+	{
+		PHY_ID_MATCH_EXACT(MTK_FPHY_ID_MT7628),
+		.name = "MediaTek MT7628 PHY",
+		.config_init = mt7628_phy_config_init,
+		.config_intr = genphy_no_config_intr,
+		.handle_interrupt = genphy_handle_interrupt_no_ack,

How do interrupts work in this device? Has this been simply cut/pasted, or have interrupts been tested using these functions?

Quoted 1 line

Andrew

Misunderstood the function and copied from other drivers, will re-test and fix or remove in v3.

#4 lore raw

RFC v2 2/3 net: dsa: initial MT7628 tagging driver

In reply to Joris Vaisvila

Add support for the MT7628 embedded switch's tag.

The MT7628 tag is merged with the VLAN TPID field when a VLAN is appended by the switch hardware. It is not installed if the VLAN tag is already there on ingress. Due to this hardware quirk the tag cannot be trusted for port 0 if we don't know that the VLAN was added by the hardware. As a workaround for this the switch is configured to always append the port PVID tag even if the incoming packet is already tagged. The tagging driver can then trust that the tag is always accurate and the whole VLAN tag can be removed on ingress as it's only metadata for the tagger.

On egress the MT7628 tag allows precise TX, but the correct VLAN tag from tag_8021q is still appended or the switch will not forward the packet.

---
 include/net/dsa.h    |  2 +
 net/dsa/Kconfig      |  6 +++
 net/dsa/Makefile     |  1 +
 net/dsa/tag_mt7628.c | 93 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+)
 create mode 100644 net/dsa/tag_mt7628.c
include/net/dsa.h +2 -0
index 6c17446f3dcc..e93f9356b5c3 100644
@@ -58,6 +58,7 @@ struct tc_action;
 #define DSA_TAG_PROTO_YT921X_VALUE		30
 #define DSA_TAG_PROTO_MXL_GSW1XX_VALUE		31
 #define DSA_TAG_PROTO_MXL862_VALUE		32
+#define DSA_TAG_PROTO_MT7628_VALUE		33
 
 enum dsa_tag_protocol {
 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
@@ -93,6 +94,7 @@ enum dsa_tag_protocol {
 	DSA_TAG_PROTO_YT921X		= DSA_TAG_PROTO_YT921X_VALUE,
 	DSA_TAG_PROTO_MXL_GSW1XX	= DSA_TAG_PROTO_MXL_GSW1XX_VALUE,
 	DSA_TAG_PROTO_MXL862		= DSA_TAG_PROTO_MXL862_VALUE,
+	DSA_TAG_PROTO_MT7628		= DSA_TAG_PROTO_MT7628_VALUE,
 };
 
 struct dsa_switch;
net/dsa/Kconfig +6 -0
index 5ed8c704636d..4aa73bd1aa9b 100644
@@ -211,4 +211,10 @@ config NET_DSA_TAG_YT921X
 	  Say Y or M if you want to enable support for tagging frames for
 	  Motorcomm YT921x switches.
 
+config NET_DSA_TAG_MT7628
+	tristate "Tag driver for the MT7628 embedded switch"
+	help
+	  Say Y or M if you want to enable support for tagging frames for the
+	  switch embedded in the MT7628 SoC.
+
 endif
net/dsa/Makefile +1 -0
index bf7247759a64..d25ec0ab7d67 100644
@@ -42,6 +42,7 @@ obj-$(CONFIG_NET_DSA_TAG_TRAILER) += tag_trailer.o
 obj-$(CONFIG_NET_DSA_TAG_VSC73XX_8021Q) += tag_vsc73xx_8021q.o
 obj-$(CONFIG_NET_DSA_TAG_XRS700X) += tag_xrs700x.o
 obj-$(CONFIG_NET_DSA_TAG_YT921X) += tag_yt921x.o
+obj-$(CONFIG_NET_DSA_TAG_MT7628) += tag_mt7628.o
 
 # for tracing framework to find trace.h
 CFLAGS_trace.o := -I$(src)
net/dsa/tag_mt7628.c new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

Re: [RFC v2 2/3] net: dsa: initial MT7628 tagging driver

Quoted 41 lines · Joris Vaisvila

Add support for the MT7628 embedded switch's tag.

The MT7628 tag is merged with the VLAN TPID field when a VLAN is appended by the switch hardware. It is not installed if the VLAN tag is already there on ingress. Due to this hardware quirk the tag cannot be trusted for port 0 if we don't know that the VLAN was added by the hardware. As a workaround for this the switch is configured to always append the port PVID tag even if the incoming packet is already tagged. The tagging driver can then trust that the tag is always accurate and the whole VLAN tag can be removed on ingress as it's only metadata for the tagger.

On egress the MT7628 tag allows precise TX, but the correct VLAN tag from tag_8021q is still appended or the switch will not forward the packet.

---
 include/net/dsa.h    |  2 +
 net/dsa/Kconfig      |  6 +++
 net/dsa/Makefile     |  1 +
 net/dsa/tag_mt7628.c | 93 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+)
 create mode 100644 net/dsa/tag_mt7628.c

[...]

net/dsa/tag_mt7628.c new file +15 -0
+#define MT7628_TAG_TX_PORT_BIT_MASK GENMASK(5, 0)
+#define MT7628_TAG_RX_PORT_MASK GENMASK(2, 0)
+#define MT7628_TAG_LEN 4
+
+static struct sk_buff *mt7628_tag_xmit(struct sk_buff *skb,
+				       struct net_device *dev)
+{
+	struct dsa_port *dp;
+	u16 xmit_vlan;
+	u8 *tag;
+
+	dp = dsa_user_to_port(dev);
+	xmit_vlan = dsa_tag_8021q_standalone_vid(dp);
+
+	skb_push(skb, MT7628_TAG_LEN);

net/dsa/tag_mt7628.c line 53

Quoted 10 lines
net/dsa/tag_mt7628.c new file +10 -0
+	dsa_alloc_etype_header(skb, MT7628_TAG_LEN);
+
+	tag = dsa_etype_header_pos_tx(skb);
+
+	tag[0] = ETH_P_8021Q >> 8;
+	tag[1] = FIELD_PREP(MT7628_TAG_TX_PORT_BIT_MASK,
+			    dsa_xmit_port_mask(skb, dev));
+
+	tag[2] = xmit_vlan >> 8;
+	tag[3] = xmit_vlan & 0xff;

So there are basically two 16-bit fields which need endian swapping from host-byte order (little endian on MT7628) to network byte-order. Imho it would be better expressed by using something along the lines of

	__be16 *tag;
...
	tag = dsa_etype_header_pos_tx(skb);
	tag[0] = htons(ETH_P_8021Q |
		       FIELD_PREP(MT7628_TAG_TX_PORT_BIT_MASK,
				  dsa_xmit_port_mask(skb, dev)));
	tag[1] = htons(xmit_vlan);
Quoted 15 lines
net/dsa/tag_mt7628.c new file +15 -0
+	return skb;
+}
+
+static struct sk_buff *mt7628_tag_rcv(struct sk_buff *skb,
+				      struct net_device *dev)
+{
+	int src_port;
+	__be16 *phdr;
+	u16 tpid;
+
+	if (unlikely(!pskb_may_pull(skb, MT7628_TAG_LEN)))
+		return NULL;
+
+	phdr = dsa_etype_header_pos_rx(skb);
+	tpid = ntohs(*phdr);

net/dsa/tag_mt7628.c line 78

Quoted 10 lines
net/dsa/tag_mt7628.c new file +10 -0
+	skb_pull_rcsum(skb, MT7628_TAG_LEN);
+	dsa_strip_etype_header(skb, MT7628_TAG_LEN);
+
+	src_port = tpid & MT7628_TAG_RX_PORT_MASK;
+
+	skb->dev = dsa_conduit_find_user(dev, 0, src_port);
+	if (!skb->dev)
+		return NULL;
+	dsa_default_offload_fwd_mark(skb);
+	return skb;

This looks like it is semantically identical to the RX path in tag_mtk.c.

Maybe the TX-side can be added to the existing mtk_tag.c driver, so the RX-side can be shared? tag_brcm.c also defines multiple similar protocols in the same C source file.

#6 lore raw

Re: [RFC v2 2/3] net: dsa: initial MT7628 tagging driver

Hi Daniel, thanks for the feedback.

Quoted 13 lines · Daniel Golle

net/dsa/tag_mt7628.c line 53

Quoted 6 lines · Joris Vaisvila
net/dsa/tag_mt7628.c new file +6 -0
+	tag[0] = ETH_P_8021Q >> 8;
+	tag[1] = FIELD_PREP(MT7628_TAG_TX_PORT_BIT_MASK,
+			    dsa_xmit_port_mask(skb, dev));
+
+	tag[2] = xmit_vlan >> 8;
+	tag[3] = xmit_vlan & 0xff;

So there are basically two 16-bit fields which need endian swapping from host-byte order (little endian on MT7628) to network byte-order. Imho it would be better expressed by using something along the lines of

Quoted 7 lines
	__be16 *tag;
...
	tag = dsa_etype_header_pos_tx(skb);
	tag[0] = htons(ETH_P_8021Q |
		       FIELD_PREP(MT7628_TAG_TX_PORT_BIT_MASK,
				  dsa_xmit_port_mask(skb, dev)));
	tag[1] = htons(xmit_vlan);

Will use this in later revisions, thanks for the suggestion.

Quoted 25 lines
Quoted 12 lines
net/dsa/tag_mt7628.c new file +12 -0
+static struct sk_buff *mt7628_tag_rcv(struct sk_buff *skb,
+				      struct net_device *dev)
+{
+	int src_port;
+	__be16 *phdr;
+	u16 tpid;
+
+	if (unlikely(!pskb_may_pull(skb, MT7628_TAG_LEN)))
+		return NULL;
+
+	phdr = dsa_etype_header_pos_rx(skb);
+	tpid = ntohs(*phdr);

net/dsa/tag_mt7628.c line 78

Quoted 10 lines
net/dsa/tag_mt7628.c new file +10 -0
+	skb_pull_rcsum(skb, MT7628_TAG_LEN);
+	dsa_strip_etype_header(skb, MT7628_TAG_LEN);
+
+	src_port = tpid & MT7628_TAG_RX_PORT_MASK;
+
+	skb->dev = dsa_conduit_find_user(dev, 0, src_port);
+	if (!skb->dev)
+		return NULL;
+	dsa_default_offload_fwd_mark(skb);
+	return skb;

This looks like it is semantically identical to the RX path in tag_mtk.c.

Quoted 3 lines

Maybe the TX-side can be added to the existing mtk_tag.c driver, so the RX-side can be shared? tag_brcm.c also defines multiple similar protocols in the same C source file.

While the core structure is really similar, there are subtle hardware differences in the MT7628 switch that will make the RX code diverge from the MTK tag when VLAN offloading is implemented. To account for that, I'd prefer to keep these drivers separate.

I plan to implement bridge and VLAN offloading after the initial switch support lands, to keep the initial series smaller and simplify review.

#7 lore raw

RFC v2 3/3 net: dsa: initial support for MT7628 embedded switch

In reply to Joris Vaisvila

Add support for the MT7628 embedded switch.

The switch has 5 built-in 100Mbps ports (ports 0-4) and 2x 1Gbps ports. One of the 1Gbps ports (port 6) is internally attached to the SoCs CPU MAC and serves as the CPU port. The other 1Gbps port (port 5) requires an external MAC to function.

The switch hardware has a very limited 16 entry VLAN table. Configuring VLANs is the only way to control switch forwarding. Currently 7 entries are used by tag_8021q to isolate the ports. Double tag feature is enabled to force the switch to append the VLAN tag even if the incoming packet is already tagged, this simulates VLAN-unaware functionality and simplifies the tagger implementation.

---
 drivers/net/dsa/Kconfig  |   7 +
 drivers/net/dsa/Makefile |   1 +
 drivers/net/dsa/mt7628.c | 609 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/net/dsa/mt7628.c
drivers/net/dsa/Kconfig +7 -0
index 39fb8ead16b5..d07fc8dfe228 100644
@@ -70,6 +70,13 @@ config NET_DSA_MV88E6060
 	  This enables support for the Marvell 88E6060 ethernet switch
 	  chip.
 
+config NET_DSA_MT7628
+	tristate "MT7628 Embedded ethernet switch support"
+	select NET_DSA_TAG_MT7628
+	select MEDIATEK_FE_SOC_PHY
+	help
+	  This enables support for the switch in the MT7628 SoC.
+
 source "drivers/net/dsa/microchip/Kconfig"
 
 source "drivers/net/dsa/mv88e6xxx/Kconfig"
drivers/net/dsa/Makefile +1 -0
index f5a463b87ec2..22da6b680f29 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX) += vitesse-vsc73xx-core.o
 obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_PLATFORM) += vitesse-vsc73xx-platform.o
 obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_SPI) += vitesse-vsc73xx-spi.o
 obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o
+obj-$(CONFIG_NET_DSA_MT7628) += mt7628.o
 obj-y				+= b53/
 obj-y				+= hirschmann/
 obj-y				+= lantiq/
drivers/net/dsa/mt7628.c new file +609 -0

609 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

Re: [RFC v2 3/3] net: dsa: initial support for MT7628 embedded switch

Quoted 1 line · Joris Vaisvila

Add support for the MT7628 embedded switch.

Thank you for working on this, MT7628 (especially in "IoT" mode with 3 UARTs and MMC bus exposed instead of the additional switch ports) still has it's special niche somewhere in between ESP32 and "proper" Linux-running SoCs ;)

Quoted 5 lines

The switch has 5 built-in 100Mbps ports (ports 0-4) and 2x 1Gbps ports. One of the 1Gbps ports (port 6) is internally attached to the SoCs CPU MAC and serves as the CPU port. The other 1Gbps port (port 5) requires an external MAC to function.

I thought port 5 is a dead-end on MT76x8. Afaik it was only used on Ralink Rt3052 for RGMII/xMII with this switch IP, and not wired to any external pins nor to an internal MAC on all other SoCs which came after.

Quoted 10 lines

The switch hardware has a very limited 16 entry VLAN table. Configuring VLANs is the only way to control switch forwarding. Currently 7 entries are used by tag_8021q to isolate the ports. Double tag feature is enabled to force the switch to append the VLAN tag even if the incoming packet is already tagged, this simulates VLAN-unaware functionality and simplifies the tagger implementation.

drivers/net/dsa/Kconfig config NET_DSA_MV88E6060 · line 78

Quoted 6 lines
drivers/net/dsa/Kconfig +6 -0
+config NET_DSA_MT7628
+	tristate "MT7628 Embedded ethernet switch support"
+	select NET_DSA_TAG_MT7628
+	select MEDIATEK_FE_SOC_PHY
+	help
+	  This enables support for the switch in the MT7628 SoC.

And potentially many others (Rt3052, Rt3050, Rt3352, Rt5350). As support for the Rt5350 is present in mtk_eth_soc at least that sounds like it would be worth mentioning (or even naming the driver and tagger interely after rt3050 which is the origin of that IP block).

drivers/net/dsa/mt7628.c line 152

Quoted 8 lines
drivers/net/dsa/mt7628.c new file +8 -0
+static int mt7628_mii_read(struct mii_bus *bus, int port, int regnum)
+{
+	struct mt7628_esw *esw = bus->priv;
+	int ret;
+	u32 val;
+
+	ret =
+	    regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val,

Unnecessary linebreak.

Quoted 5 lines
drivers/net/dsa/mt7628.c new file +5 -0
+				     !(val & MT7628_ESW_PCR1_RD_DONE), 10,
+				     5000);
+	if (ret)
+		goto out;
+

drivers/net/dsa/mt7628.c line 167

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+	ret = regmap_write(esw->regmap, MT7628_ESW_REG_PCR0,
+			   FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_REG,
+				      regnum) |
+			   FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_ADDR,
+				      port) | MT7628_ESW_PCR0_RD_PHY_CMD);
+	if (ret)
+		goto out;
+
+	ret =
+	    regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val,

Here as well.

Quoted 9 lines
drivers/net/dsa/mt7628.c new file +9 -0
+				     (val & MT7628_ESW_PCR1_RD_DONE), 10,
+				     5000);
+out:
+	if (ret) {
+		dev_err(&bus->dev, "read failed. MDIO timeout?\n");
+		return -ETIMEDOUT;
+	}
+	return FIELD_GET(MT7628_ESW_PCR1_RD_DATA, val);
+}

drivers/net/dsa/mt7628.c line 186

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+
+static int mt7628_mii_write(struct mii_bus *bus, int port, int regnum,
+			    u16 dat)
+{
+	struct mt7628_esw *esw = bus->priv;
+	u32 val;
+	int ret;
+
+	ret =
+	    regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val,

Here as well.

Quoted 6 lines
drivers/net/dsa/mt7628.c new file +6 -0
+				     !(val & MT7628_ESW_PCR1_WT_DONE), 10,
+				     5000);
+	if (ret)
+		goto out;
+
+	ret = regmap_write(esw->regmap, MT7628_ESW_REG_PCR0,

drivers/net/dsa/mt7628.c line 202

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+			   FIELD_PREP(MT7628_ESW_PCR0_WT_NWAY_DATA, dat) |
+			   FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_REG,
+				      regnum) |
+			   FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_ADDR,
+				      port) | MT7628_ESW_PCR0_WT_PHY_CMD);
+	if (ret)
+		goto out;
+
+	ret =
+	    regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val,

And here as well.

drivers/net/dsa/mt7628.c line 335

Quoted 9 lines

[...]

drivers/net/dsa/mt7628.c new file +8 -0
+static void esw_set_vlan_id(struct mt7628_esw *esw, unsigned int vlan,
+			    unsigned int vid)
+{
+	unsigned int s = MT7628_ESW_VLANI_VID_S * (vlan % 2);
+
+	regmap_update_bits(esw->regmap, MT7628_ESW_REG_VLANI(vlan / 2),
+			   MT7628_ESW_VLANI_VID_M << s,
+			   (vid & MT7628_ESW_VLANI_VID_M) << s);

Please add helper macros for those registers and field in them

drivers/net/dsa/mt7628.c line 345

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+}
+
+static void esw_set_pvid(struct mt7628_esw *esw, unsigned int port,
+			 unsigned int pvid)
+{
+	unsigned int s = MT7628_ESW_PVIDC_PVID_S * (port % 2);
+
+	regmap_update_bits(esw->regmap, MT7628_ESW_REG_PVIDC(port / 2),
+			   MT7628_ESW_PVIDC_PVID_M << s,
+			   (pvid & MT7628_ESW_PVIDC_PVID_M) << s);

and those as well

drivers/net/dsa/mt7628.c line 355

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+}
+
+static void esw_set_vmsc(struct mt7628_esw *esw, unsigned int vlan,
+			 unsigned int msc)
+{
+	unsigned int s = MT7628_ESW_VMSC_MSC_S * (vlan % 4);
+
+	regmap_update_bits(esw->regmap, MT7628_ESW_REG_VMSC(vlan / 4),
+			   MT7628_ESW_VMSC_MSC_M << s,
+			   (msc & MT7628_ESW_VMSC_MSC_M) << s);

those too

drivers/net/dsa/mt7628.c line 365

Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10 -0
+}
+
+static void esw_set_vub(struct mt7628_esw *esw, unsigned int vlan,
+			unsigned int msc)
+{
+	unsigned int s = MT7628_ESW_VUB_S * (vlan % 4);
+
+	regmap_update_bits(esw->regmap, MT7628_ESW_REG_VUB(vlan / 4),
+			   MT7628_ESW_VUB_M << s,
+			   (msc & MT7628_ESW_VUB_M) << s);

and those.

drivers/net/dsa/mt7628.c line 446

Quoted 7 lines

[...]

drivers/net/dsa/mt7628.c new file +6 -0
+static void mt7628_phylink_get_caps(struct dsa_switch *ds, int port,
+				    struct phylink_config *config)
+{
+	config->mac_capabilities = MAC_100 | MAC_10;
+	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
+	__set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);

This looks a bit wrong.
Aren't all 5 PHYs internal? PHY_INTERFACE_INTERNAL would be best to describe them then. And doesn't the CPU port (6) connect with 1000M, if so it should have MAC_1000 set as well (as neither the Ethernet driver nor the DSA driver do anything to configure the interface that is purely cosmetic, but still)

Iff you want to support port 5 on Rt3052 (which will also require setting the correct interface mode in .mac_config) I'd expect something like this:

config->mac_capabilities = MAC_100 | MAC_10;

switch (port) {
case 0...4:

	__set_bit(PHY_INTERFACE_INTERNAL, config->supported_interfaces);
	break;
case 5:
	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_REVMII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_RMII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
	config->mac_capabilities |= MAC_1000;
	break;
case 6:
	__set_bit(PHY_INTERFACE_INTERNAL, config->supported_interfaces);
	config->mac_capabilities |= MAC_1000;
	break;
default:
	return;
}

If you don't want to deal with port 5 you should skip it here as well.

Cheers

Daniel

#9 lore raw

Re: [RFC v2 3/3] net: dsa: initial support for MT7628 embedded switch

Quoted 9 lines · Daniel Golle
Quoted 4 lines

The switch has 5 built-in 100Mbps ports (ports 0-4) and 2x 1Gbps ports. One of the 1Gbps ports (port 6) is internally attached to the SoCs CPU MAC and serves as the CPU port. The other 1Gbps port (port 5) requires an external MAC to function.

I thought port 5 is a dead-end on MT76x8. Afaik it was only used on Ralink Rt3052 for RGMII/xMII with this switch IP, and not wired to any external pins nor to an internal MAC on all other SoCs which came after.

I assumed this was true as that's what the datasheet says, but looking at the pinout it really doesn't have any pins to attach an external mac.

Quoted 7 lines

drivers/net/dsa/Kconfig config NET_DSA_MV88E6060 · line 78

Quoted 1 line
drivers/net/dsa/Kconfig +1 -0
+	  This enables support for the switch in the MT7628 SoC.

And potentially many others (Rt3052, Rt3050, Rt3352, Rt5350). As support for the Rt5350 is present in mtk_eth_soc at least that sounds like it would be worth mentioning (or even naming the driver and tagger interely after rt3050 which is the origin of that IP block).

The RT3050 does not support all the same features the RT5350 does (which I believe the MT7628 is based on). The DSA tag is now set up in a way that only the MT7628 and potentially RT5350 can support. The DSA tag does not appear to be working as described in the datasheet on these switches and I do not have any of the ralink switches to test this on. I don't think it's a good idea to assert support for hardware that has not been tested with this.

I thought it would be fitting for the driver to be called after the MT7628 as that would be the first supported SoC and add support for the ralink switches later.

Quoted 6 lines
Quoted 6 lines

[...]

drivers/net/dsa/mt7628.c new file +5 -0
+static void mt7628_phylink_get_caps(struct dsa_switch *ds, int port,
+				    struct phylink_config *config)
+{
+	config->mac_capabilities = MAC_100 | MAC_10;
+	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
Quoted 8 lines

drivers/net/dsa/mt7628.c line 446

Quoted 1 line
drivers/net/dsa/mt7628.c new file +1 -0
+	__set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);

This looks a bit wrong.
Aren't all 5 PHYs internal? PHY_INTERFACE_INTERNAL would be best to describe them then. And doesn't the CPU port (6) connect with 1000M, if so it should have MAC_1000 set as well (as neither the Ethernet driver nor the DSA driver do anything to configure the interface that is purely cosmetic, but still)

Will fix this and coding style issues in v3.

Quoted 25 lines

Iff you want to support port 5 on Rt3052 (which will also require setting the correct interface mode in .mac_config) I'd expect something like this:

config->mac_capabilities = MAC_100 | MAC_10;

switch (port) {
case 0...4:

	__set_bit(PHY_INTERFACE_INTERNAL, config->supported_interfaces);
	break;
case 5:
	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_REVMII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_RMII, config->supported_interfaces);
	__set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
	config->mac_capabilities |= MAC_1000;
	break;
case 6:
	__set_bit(PHY_INTERFACE_INTERNAL, config->supported_interfaces);
	config->mac_capabilities |= MAC_1000;
	break;
default:
	return;
}
Quoted 1 line

If you don't want to deal with port 5 you should skip it here as well.

Will skip port 5 for now, due to this driver likely not working on RT3052 as it is anyway for the aforementioned reasons.

Thanks for the review.