* [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch
@ 2026-12-04 18:25 Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant Rustam Adilov
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
This patch series adds support for the embedded switch in RTL9607C.
It is a 11 port switch controller with 4 internal PHYs corresponding
to ports 0~3, PON serdes which is for the PON port 5, 2 HSGMII serdes
for ports 6-7 but the former is muxed with USB3 and latter is muxed
with PCIE1, and also CPU NIC port somehow, and 2 main CPU NIC ports
at 9/10.
The support is done through the introduction of the realtek-mmio
interface driver which the RTL9607C switch makes use of. That is
also the only reason why it is RFC and not v1. I didn't get the
any review on that during the testing in OpenWrt at [1] PR.
The patch 1 is the tag driver which is similar to what i have
posted prior at [2] but a few things were changed like commit
message making clear what is similar and what is not and how the
reason codes are handled. And now the long awaited DSA driver is
coming with it too.
DSA driver itself is pretty basic, in a similar fashion to how
the initial RTL8365MB switch support was added in [3].
I suspect the VLAN, FDB, MDB, and all the other goodies can
be added in the future (though probably not by me as i'm very
much not experienced in advanced networking). I do very much
know that they are very similar however.
And the patch 4 fixes one thing that is needed for the c45 part
of the RTL9607C to function properly. I do hope it is not gonna
cause any issues on any existing systems. I think it could be
carved out if it turns out to be valid.
[1] https://github.com/openwrt/openwrt/pull/26969
[2] https://lore.kernel.org/all/20261119165129.26693-1-adilov@disroot.org/
[3] https://lore.kernel.org/all/20211018093804.3115191-1-alvin@pqrs.dk/
Rustam Adilov (6):
net: dsa: tag_rtl8_4: add support for RTL9607C tag variant
net: dsa: realtek: add new mmio interface for drivers
dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible
net: dsa: realtek: add support for RTL9607C switch
net: phy: realtek: use mdio.addr instead of 0 in
rtlgen_read/write_vend2
net: phy: realtek: add support for RTL9607C internal PHYs
.../devicetree/bindings/net/dsa/realtek.yaml | 59 +-
drivers/net/dsa/realtek/Kconfig | 14 +
drivers/net/dsa/realtek/Makefile | 3 +
drivers/net/dsa/realtek/realtek-mmio.c | 84 +
drivers/net/dsa/realtek/realtek-mmio.h | 29 +
drivers/net/dsa/realtek/realtek.h | 1 +
drivers/net/dsa/realtek/rtl83xx.c | 38 +-
drivers/net/dsa/realtek/rtl83xx.h | 1 +
drivers/net/dsa/realtek/rtl9607c_main.c | 1525 +++++++++++++++++
drivers/net/phy/realtek/realtek_main.c | 140 +-
include/net/dsa.h | 2 +
net/dsa/Kconfig | 3 +-
net/dsa/tag_rtl8_4.c | 171 +-
13 files changed, 2047 insertions(+), 23 deletions(-)
create mode 100644 drivers/net/dsa/realtek/realtek-mmio.c
create mode 100644 drivers/net/dsa/realtek/realtek-mmio.h
create mode 100644 drivers/net/dsa/realtek/rtl9607c_main.c
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 2/6] net: dsa: realtek: add new mmio interface for drivers Rustam Adilov
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov, Taiga Ogawa
The RTL9607C family of SoCs have an embdedded switch that utilizes
the same Realtek CPU Tag protocol 4 with 8 bytes in size but have
a bit different of a layout. And that layout is also different for
TX versus RX sides and introduces new fields not seen by RTL8_4.
The fields PORT, RX_PORTMASK, PRI, PRI_EN, KEEP, LEARN_DIS, PSEL
share the same semantics as in RTL8_4 tag. The REASON is the same
8 bit field at byte 3 but numbering seem to be very different.
Add basic necessities for this cpu tag variant to function.
Tested-by: Taiga Ogawa <zectaiga@gmail.com>
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
include/net/dsa.h | 2 +
net/dsa/Kconfig | 3 +-
net/dsa/tag_rtl8_4.c | 171 ++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 172 insertions(+), 4 deletions(-)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 09f3043d57f0..6f86c7625304 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -63,6 +63,7 @@ struct tc_action;
#define DSA_TAG_PROTO_MT7628_VALUE 35
#define DSA_TAG_PROTO_KS8995_VALUE 36
#define DSA_TAG_PROTO_LAN9645X_VALUE 37
+#define DSA_TAG_PROTO_RTL8_4AP_VALUE 38
enum dsa_tag_protocol {
DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
@@ -103,6 +104,7 @@ enum dsa_tag_protocol {
DSA_TAG_PROTO_MT7628 = DSA_TAG_PROTO_MT7628_VALUE,
DSA_TAG_PROTO_KS8995 = DSA_TAG_PROTO_KS8995_VALUE,
DSA_TAG_PROTO_LAN9645X = DSA_TAG_PROTO_LAN9645X_VALUE,
+ DSA_TAG_PROTO_RTL8_4AP = DSA_TAG_PROTO_RTL8_4AP_VALUE,
};
struct dsa_switch;
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index bdeb60af764b..efa5f1661169 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -196,7 +196,8 @@ config NET_DSA_TAG_RTL8_4
tristate "Tag driver for Realtek 8 byte protocol 4 tags"
help
Say Y or M if you want to enable support for tagging frames for Realtek
- switches with 8 byte protocol 4 tags, such as the Realtek RTL8365MB-VC.
+ switches with 8 byte protocol 4 tags, such as the Realtek RTL8365MB-VC
+ or the Realtek RTL9607C.
config NET_DSA_TAG_RZN1_A5PSW
tristate "Tag driver for Renesas RZ/N1 A5PSW switch"
diff --git a/net/dsa/tag_rtl8_4.c b/net/dsa/tag_rtl8_4.c
index 4da3beebef75..902821524ed3 100644
--- a/net/dsa/tag_rtl8_4.c
+++ b/net/dsa/tag_rtl8_4.c
@@ -4,8 +4,8 @@
*
* Copyright (C) 2021 Alvin Šipraga <alsi@bang-olufsen.dk>
*
- * NOTE: Currently only supports protocol "4" found in the RTL8365MB, hence
- * named tag_rtl8_4.
+ * NOTE: Currently only supports protocol "4" found in the RTL8365MB and RTL9607C,
+ * hence named tag_rtl8_4 and tag_rtl8_4ap.
*
* This tag has the following format:
*
@@ -74,6 +74,54 @@
* software or hardware. To avoid this issue, if the checksum is still pending,
* this tagger checksums the packet in software before adding the tag.
*
+ * This 8 byte switch tag can also be found on RTL9607C family of SoCs with
+ * identical protocol "4", but with a slightly different insides.
+ *
+ * On switch->CPU the bytes 4-7 are different and look like:
+ *
+ * |-----------------------------------+-----------------------------------|
+ * | (3) | (3) / (5) | (2) | (1) | (1) | (4) |
+ * | PRI | EXTSPA / PON_STREAM_ID |PON_STREAM_ID|L3ROUTE| ORIG | PORT |
+ * |-----------------------------------+-----------------------------------|
+ * | (1) | (15-bit) / (7-bit) |
+ * | FBI | FLOW_BASED_HASH / DST_PORT_MASK |
+ * |-----------------------------------+-----------------------------------|
+ *
+ * On CPU->Switch the bytes 3-7 are different and look like:
+ *
+ * |-----------------------------------+-----------------------------------|
+ * | (8-bit) | (8-bit) |
+ * | Protocol [0x04] | RX_PORTMASK_LOW |
+ * |-----------------------------------+-----------------------------------|
+ * | (3-bit) |(1)| (1) | (3) |(1) |(1)| (1) |(1) |(2)| (1) |1|
+ * |RX_PORTMASK_HIGH| X | PRI_EN | PRI |KEEP| X |LEARN_DIS|PSEL| X |DIRTX|X|
+ * |-----------------------------------+-----------------------------------|
+ * | (3-bit)| (2-bit) | (4-bit) | (7-bit) |
+ * | EXTSPA | PPPOE_ACT | PPPOE_IDX | PON_STREAM_ID |
+ * |-----------------------------------+-----------------------------------|
+ *
+ * With the following descriptions for new fields:
+ *
+ * field | description
+ * -----------------+-------------
+ * EXTSPA | Source Extension Port. Relevant when PORT is CPU (9, 10, 7).
+ * PON_STREAM_ID | PON Stream ID (GPON) or LLID index (EPON). Relevant when
+ * | PORT is PON (5)
+ * L3ROUTE | "This frame is an intended routed frames" as per docs
+ * ORIG | The received frame by NIC is the original packet's format
+ * | before switch lookup and switch didn't do any modifications
+ * FBI | Flow Based I...? Only relevant when REASON is FB (aka < 64)
+ * FLOW_BASED_HASH | Flow Based Hash Index.
+ * DST_PORT_MASK | Switch lookup destination port mask. bit 0: CPU, bit 1-6 are
+ * | extension ports 0~5.
+ * PSEL | PON stream id (GPON) / LLID index(EPON) selection.
+ * DIRTX | Direct TX, whatever that means? Used to be called L34KEEP.
+ * PPPOE_ACT | Egress action for PPPoE header whose Ether Type = 0x8864.
+ * | 0: intact, 1: Add PPPoE header, 2: Remove PPPoE header,
+ * | 3: remarking PPPoE switch TX mac action.
+ * PPPOE_IDX | PPPoE index pointed to an entry of the PPPoE table in the
+ * | switch. Switch TX-MAC look PPPoE table up by this index to
+ * | get PPPoE session ID.
*/
#include <linux/bitfield.h>
@@ -84,11 +132,12 @@
/* Protocols supported:
*
- * 0x04 = RTL8365MB DSA protocol
+ * 0x04 = RTL8365MB / RTL9607C DSA protocol
*/
#define RTL8_4_NAME "rtl8_4"
#define RTL8_4T_NAME "rtl8_4t"
+#define RTL8_4AP_NAME "rtl8_4ap"
#define RTL8_4_TAG_LEN 8
@@ -97,13 +146,23 @@
#define RTL8_4_REASON GENMASK(7, 0)
#define RTL8_4_REASON_FORWARD 0
#define RTL8_4_REASON_TRAP 80
+#define RTL8_4AP_REASON_FLOOD 202
+#define RTL8_4AP_REASON_LUT 204
+
+#define RTL8_4AP_RX_PORTMASK_LOW GENMASK(7, 0)
+#define RTL8_4AP_RX_PORTMASK_HIGH GENMASK(15, 13)
+#define RTL9607C_RX_PORTMASK_HIGH GENMASK(10, 8)
+#define RTL8_4AP_DIRTX BIT(1)
#define RTL8_4_LEARN_DIS BIT(5)
#define RTL8_4_KEEP BIT(7)
#define RTL8_4_TX GENMASK(3, 0)
#define RTL8_4_RX GENMASK(10, 0)
+#define RTL8_4AP_DST_PMASK GENMASK(6, 0)
+#define RTL8_4AP_DST_CPU_MASK BIT(0)
+
static void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
void *tag)
{
@@ -153,6 +212,44 @@ static struct sk_buff *rtl8_4t_tag_xmit(struct sk_buff *skb,
return skb;
}
+static void rtl8_4ap_write_tag(struct sk_buff *skb, struct net_device *dev,
+ void *tag)
+{
+ u16 pmask = dsa_xmit_port_mask(skb, dev);
+ __be16 tag16[RTL8_4_TAG_LEN / 2];
+ u16 pmask_low, pmask_high;
+
+ /* Set Realtek EtherType */
+ tag16[0] = htons(ETH_P_REALTEK);
+
+ pmask_low = FIELD_GET(RTL8_4AP_RX_PORTMASK_LOW, pmask);
+ /* Set Protocol; set low forwarding port mask */
+ tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL9607C) |
+ FIELD_PREP(RTL8_4AP_RX_PORTMASK_LOW, pmask_low));
+
+ pmask_high = FIELD_GET(RTL9607C_RX_PORTMASK_HIGH, pmask);
+ /* set high forwarding port mask, KEEP, LEARN_DIS and DIRTX */
+ tag16[2] = htons(FIELD_PREP(RTL8_4AP_RX_PORTMASK_HIGH, pmask_high) |
+ RTL8_4_LEARN_DIS | RTL8_4_KEEP | RTL8_4AP_DIRTX);
+
+ /* Zero EXTSPA, PPPOE_ACT/IDX and PON_STREAM_ID */
+ tag16[3] = 0;
+
+ memcpy(tag, tag16, RTL8_4_TAG_LEN);
+}
+
+static struct sk_buff *rtl8_4ap_tag_xmit(struct sk_buff *skb,
+ struct net_device *dev)
+{
+ skb_push(skb, RTL8_4_TAG_LEN);
+
+ dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
+
+ rtl8_4ap_write_tag(skb, dev, dsa_etype_header_pos_tx(skb));
+
+ return skb;
+}
+
static int rtl8_4_read_tag(struct sk_buff *skb, struct net_device *dev,
void *tag)
{
@@ -242,6 +339,60 @@ static struct sk_buff *rtl8_4t_tag_rcv(struct sk_buff *skb,
return skb;
}
+static int rtl8_4ap_read_tag(struct sk_buff *skb, struct net_device *dev,
+ void *tag)
+{
+ __be16 tag16[RTL8_4_TAG_LEN / 2];
+ u16 etype;
+ u8 reason;
+ u8 proto;
+ u8 port;
+ u8 dst_pmask;
+ bool is_lut;
+
+ memcpy(tag16, tag, RTL8_4_TAG_LEN);
+
+ /* Parse Realtek EtherType */
+ etype = ntohs(tag16[0]);
+ if (unlikely(etype != ETH_P_REALTEK)) {
+ dev_warn_ratelimited(&dev->dev,
+ "non-realtek ethertype 0x%04x\n", etype);
+ return -EPROTO;
+ }
+
+ /* Parse Protocol */
+ proto = FIELD_GET(RTL8_4_PROTOCOL, ntohs(tag16[1]));
+ if (unlikely(proto != RTL8_4_PROTOCOL_RTL9607C)) {
+ dev_warn_ratelimited(&dev->dev,
+ "unknown realtek protocol 0x%02x\n", proto);
+ return -EPROTO;
+ }
+
+ /* Parse REASON */
+ reason = FIELD_GET(RTL8_4_REASON, ntohs(tag16[1]));
+
+ /* Parse TX (switch->CPU) */
+ port = FIELD_GET(RTL8_4_TX, ntohs(tag16[2]));
+ skb->dev = dsa_conduit_find_user(dev, 0, port);
+ if (!skb->dev) {
+ dev_warn_ratelimited(&dev->dev,
+ "could not find user for port %d\n", port);
+ return -ENOENT;
+ }
+
+ /* Parse destination port mask */
+ dst_pmask = FIELD_GET(RTL8_4AP_DST_PMASK, ntohs(tag16[3]));
+
+ is_lut = reason == RTL8_4AP_REASON_LUT;
+ if (!is_lut && reason != RTL8_4AP_REASON_FLOOD) {
+ dev_warn_ratelimited(&dev->dev, "Unknown reason code: %d\n", reason);
+ } else if (!is_lut || (dst_pmask & RTL8_4AP_DST_CPU_MASK)) {
+ dsa_default_offload_fwd_mark(skb);
+ }
+
+ return 0;
+}
+
/* Ethertype version */
static const struct dsa_device_ops rtl8_4_netdev_ops = {
.name = "rtl8_4",
@@ -268,9 +419,23 @@ DSA_TAG_DRIVER(rtl8_4t_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL8_4T, RTL8_4T_NAME);
+/* Ethertype RTL9607C version */
+static const struct dsa_device_ops rtl8_4ap_netdev_ops = {
+ .name = RTL8_4AP_NAME,
+ .proto = DSA_TAG_PROTO_RTL8_4AP,
+ .xmit = rtl8_4ap_tag_xmit,
+ .rcv = rtl8_4ap_tag_rcv,
+ .needed_headroom = RTL8_4_TAG_LEN,
+};
+
+DSA_TAG_DRIVER(rtl8_4ap_netdev_ops);
+
+MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL8_4AP, RTL8_4AP_NAME);
+
static struct dsa_tag_driver *dsa_tag_drivers[] = {
&DSA_TAG_DRIVER_NAME(rtl8_4_netdev_ops),
&DSA_TAG_DRIVER_NAME(rtl8_4t_netdev_ops),
+ &DSA_TAG_DRIVER_NAME(rtl8_4ap_netdev_ops),
};
module_dsa_tag_drivers(dsa_tag_drivers);
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 2/6] net: dsa: realtek: add new mmio interface for drivers
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 3/6] dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible Rustam Adilov
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
Add a new interface driver for MMIO connected realtek switches.
That is platform driver just like realtek-smi but uses the syscon
to get its regmap.
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
drivers/net/dsa/realtek/Kconfig | 7 +++
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/realtek-mmio.c | 84 ++++++++++++++++++++++++++
drivers/net/dsa/realtek/realtek-mmio.h | 29 +++++++++
drivers/net/dsa/realtek/rtl83xx.c | 38 +++++++-----
drivers/net/dsa/realtek/rtl83xx.h | 1 +
6 files changed, 145 insertions(+), 15 deletions(-)
create mode 100644 drivers/net/dsa/realtek/realtek-mmio.c
create mode 100644 drivers/net/dsa/realtek/realtek-mmio.h
diff --git a/drivers/net/dsa/realtek/Kconfig b/drivers/net/dsa/realtek/Kconfig
index f1530ba0db61..aa4955fef4aa 100644
--- a/drivers/net/dsa/realtek/Kconfig
+++ b/drivers/net/dsa/realtek/Kconfig
@@ -30,6 +30,13 @@ config NET_DSA_REALTEK_SMI
Select to enable support for registering switches connected
through SMI.
+config NET_DSA_REALTEK_MMIO
+ bool "Realtek MMIO interface support"
+ depends on MFD_SYSCON
+ help
+ Select to enable support for registering switches connected
+ through MMIO.
+
config NET_DSA_REALTEK_RTL8365MB
tristate "Realtek RTL8365MB switch driver"
depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index 600a20911c1d..0d3287952b31 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -3,6 +3,7 @@ obj-$(CONFIG_NET_DSA_REALTEK) += realtek_dsa.o
realtek_dsa-y := rtl83xx.o rtl83xx-qos.o
realtek_dsa-$(CONFIG_NET_DSA_REALTEK_MDIO) += realtek-mdio.o
realtek_dsa-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o
+realtek_dsa-$(CONFIG_NET_DSA_REALTEK_MMIO) += realtek-mmio.o
obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o
rtl8366-y := rtl8366-core.o rtl8366rb.o
diff --git a/drivers/net/dsa/realtek/realtek-mmio.c b/drivers/net/dsa/realtek/realtek-mmio.c
new file mode 100644
index 000000000000..ec68c982120f
--- /dev/null
+++ b/drivers/net/dsa/realtek/realtek-mmio.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "realtek.h"
+#include "realtek-mmio.h"
+#include "rtl83xx.h"
+
+static const struct realtek_interface_info realtek_mmio_info = {
+ .syscon = true,
+};
+
+/**
+ * realtek_mmio_probe() - Probe a platform device for an MMIO-connected switch
+ * @pdev: platform_device to probe on.
+ *
+ * This function should be used as the .probe in a platform_driver. After
+ * calling the common probe function for both interfaces, it calls a common
+ * function to register the DSA switch.
+ *
+ * Context: Can sleep. Takes and releases priv->map_lock.
+ * Return: Returns 0 on success, a negative error on failure.
+ */
+int realtek_mmio_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct realtek_priv *priv;
+ int ret;
+
+ priv = rtl83xx_probe(dev, &realtek_mmio_info);
+ if (IS_ERR(priv))
+ return PTR_ERR(priv);
+
+ ret = rtl83xx_register_switch(priv);
+ if (ret) {
+ rtl83xx_remove(priv);
+ return ret;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(realtek_mmio_probe, "REALTEK_DSA");
+
+/**
+ * realtek_mmio_remove() - Remove the driver of a MMIO-connected switch
+ * @pdev: platform_device to be removed.
+ *
+ * This function should be used as the .remove in a platform_driver. First
+ * it unregisters the DSA switch and then it calls the common remove function.
+ *
+ * Context: Can sleep.
+ * Return: Nothing.
+ */
+void realtek_mmio_remove(struct platform_device *pdev)
+{
+ struct realtek_priv *priv = platform_get_drvdata(pdev);
+
+ if (!priv)
+ return;
+
+ rtl83xx_unregister_switch(priv);
+
+ rtl83xx_remove(priv);
+}
+EXPORT_SYMBOL_NS_GPL(realtek_mmio_remove, "REALTEK_DSA");
+
+/**
+ * realtek_mmio_shutdown() - Shutdown the driver of a MMIO-connected switch
+ * @pdev: platform_device shutting down.
+ *
+ * This function should be used as the .shutdown in a platform_driver. It calls
+ * the common shutdown function.
+ *
+ * Context: Can sleep.
+ * Return: Nothing.
+ */
+void realtek_mmio_shutdown(struct platform_device *pdev)
+{
+ struct realtek_priv *priv = platform_get_drvdata(pdev);
+
+ if (!priv)
+ return;
+
+ rtl83xx_shutdown(priv);
+}
+EXPORT_SYMBOL_NS_GPL(realtek_mmio_shutdown, "REALTEK_DSA");
diff --git a/drivers/net/dsa/realtek/realtek-mmio.h b/drivers/net/dsa/realtek/realtek-mmio.h
new file mode 100644
index 000000000000..e10e535f6b20
--- /dev/null
+++ b/drivers/net/dsa/realtek/realtek-mmio.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#ifndef _REALTEK_MMIO_H
+#define _REALTEK_MMIO_H
+
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_MMIO)
+
+int realtek_mmio_probe(struct platform_device *pdev);
+void realtek_mmio_remove(struct platform_device *pdev);
+void realtek_mmio_shutdown(struct platform_device *pdev);
+
+#else /* IS_ENABLED(CONFIG_NET_DSA_REALTEK_MMIO) */
+
+static inline int realtek_mmio_probe(struct platform_device *pdev)
+{
+ return -ENOENT;
+}
+
+static inline void realtek_mmio_remove(struct platform_device *pdev)
+{
+}
+
+static inline void realtek_mmio_shutdown(struct platform_device *pdev)
+{
+}
+
+#endif /* IS_ENABLED(CONFIG_NET_DSA_REALTEK_MMIO) */
+
+#endif /* _REALTEK_MMIO_H */
diff --git a/drivers/net/dsa/realtek/rtl83xx.c b/drivers/net/dsa/realtek/rtl83xx.c
index ad42c05d0a88..f8b8d16003a9 100644
--- a/drivers/net/dsa/realtek/rtl83xx.c
+++ b/drivers/net/dsa/realtek/rtl83xx.c
@@ -6,6 +6,7 @@
#include <linux/of_mdio.h>
#include <linux/if_bridge.h>
#include <linux/etherdevice.h>
+#include <linux/mfd/syscon.h>
#include "realtek.h"
#include "rtl83xx.h"
@@ -160,8 +161,6 @@ rtl83xx_probe(struct device *dev,
.reg_stride = 1,
.max_register = 0xffff,
.reg_format_endian = REGMAP_ENDIAN_BIG,
- .reg_read = interface_info->reg_read,
- .reg_write = interface_info->reg_write,
.cache_type = REGCACHE_NONE,
.lock = rtl83xx_lock,
.unlock = rtl83xx_unlock,
@@ -189,20 +188,29 @@ rtl83xx_probe(struct device *dev,
if (ret)
return ERR_PTR(ret);
- rc.lock_arg = priv;
- priv->map = devm_regmap_init(dev, NULL, priv, &rc);
- if (IS_ERR(priv->map)) {
- ret = PTR_ERR(priv->map);
- dev_err(dev, "regmap init failed: %d\n", ret);
- return ERR_PTR(ret);
- }
+ if (interface_info->syscon) {
+ priv->map = syscon_node_to_regmap(dev->of_node);
+ if (IS_ERR(priv->map))
+ return dev_err_cast_probe(dev, priv->map,
+ "regmap init failed\n");
+ } else {
+ rc.reg_read = interface_info->reg_read;
+ rc.reg_write = interface_info->reg_write;
+ rc.lock_arg = priv;
+ priv->map = devm_regmap_init(dev, NULL, priv, &rc);
+ if (IS_ERR(priv->map)) {
+ ret = PTR_ERR(priv->map);
+ ev_err(dev, "regmap init failed: %d\n", ret);
+ return ERR_PTR(ret);
+ }
- rc.disable_locking = true;
- priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc);
- if (IS_ERR(priv->map_nolock)) {
- ret = PTR_ERR(priv->map_nolock);
- dev_err(dev, "regmap init failed: %d\n", ret);
- return ERR_PTR(ret);
+ rc.disable_locking = true;
+ priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc);
+ if (IS_ERR(priv->map_nolock)) {
+ ret = PTR_ERR(priv->map_nolock);
+ dev_err(dev, "regmap init failed: %d\n", ret);
+ return ERR_PTR(ret);
+ }
}
/* Link forward and backward */
diff --git a/drivers/net/dsa/realtek/rtl83xx.h b/drivers/net/dsa/realtek/rtl83xx.h
index d86447121276..c727e772ff14 100644
--- a/drivers/net/dsa/realtek/rtl83xx.h
+++ b/drivers/net/dsa/realtek/rtl83xx.h
@@ -6,6 +6,7 @@
struct realtek_interface_info {
int (*reg_read)(void *ctx, u32 reg, u32 *val);
int (*reg_write)(void *ctx, u32 reg, u32 val);
+ bool syscon;
};
void rtl83xx_lock(void *ctx);
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 3/6] dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 2/6] net: dsa: realtek: add new mmio interface for drivers Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 4/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
RTL9607C is an SoC that have as an embedded ethernet switch
with 4 internal PHYs, 1 PON serdes, 2 HSGMII that are muxed
with USB3 and PCIe slot 1 and lastly 2/3 CPU NIC ports.
Add a compatible string for it with "-switch" to differentiate
it from SoC compatible.
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
.../devicetree/bindings/net/dsa/realtek.yaml | 59 ++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/Documentation/devicetree/bindings/net/dsa/realtek.yaml b/Documentation/devicetree/bindings/net/dsa/realtek.yaml
index b7bb60af623c..761a79bc28d9 100644
--- a/Documentation/devicetree/bindings/net/dsa/realtek.yaml
+++ b/Documentation/devicetree/bindings/net/dsa/realtek.yaml
@@ -29,7 +29,7 @@ maintainers:
description:
Realtek advertises these chips as fast/gigabit switches or unmanaged
switches. They can be controlled using different interfaces, like SMI,
- MDIO or SPI.
+ MDIO, SPI or MMIO.
The SMI "Simple Management Interface" is a two-wire protocol using
bit-banged GPIO that while it reuses the MDIO lines MCK and MDIO does
@@ -41,6 +41,9 @@ description:
The realtek-mdio driver is an MDIO driver and it must be inserted inside
an MDIO node.
+ The MMIO-connected switches use plain MMIO accessors since their registers
+ are directly mapped into the SoC's memory map.
+
The compatible string is only used to identify which (silicon) family the
switch belongs to. Roughly speaking, a family is any set of Realtek switches
whose chip identification register(s) have a common location and semantics.
@@ -53,6 +56,7 @@ properties:
enum:
- realtek,rtl8365mb
- realtek,rtl8366rb
+ - realtek,rtl9607c-switch
description: |
realtek,rtl8365mb:
Use with models RTL8363NB, RTL8363NB-VB, RTL8363SC, RTL8363SC-VB,
@@ -60,6 +64,8 @@ properties:
RTL8367SB, RTL8370MB, RTL8310SR
realtek,rtl8366rb:
Use with models RTL8366RB, RTL8366S
+ realtek,rtl9607c-switch:
+ Use with models RTL9607C, RTL8198D.
mdc-gpios:
description: GPIO line for the MDC clock line.
@@ -421,3 +427,54 @@ examples:
};
};
};
+ - |
+ #include <dt-bindings/interrupt-controller/mips-gic.h>
+ #include <dt-bindings/interrupt-controller/irq.h>
+
+ ethernet-switch@1b000000 {
+ compatible = "realtek,rtl9607c-switch", "syscon", "simple-mfd";
+ reg = <0x1b000000 0x1000000>;
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ switch_intc4: interrupt-controller {
+ interrupt-parent = <&gic>;
+ interrupts = <GIC_SHARED 15 IRQ_TYPE_LEVEL_HIGH>;
+ interrupt-controller;
+ #address-cells = <0>;
+ #interrupt-cells = <1>;
+ };
+
+ ethernet-ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ ethernet-port@0 {
+ reg = <0>;
+ label = "lan0";
+ phy-handle = <&phy0>;
+ };
+ ethernet-port@1 {
+ reg = <1>;
+ label = "lan1";
+ phy-handle = <&phy1>;
+ };
+ ethernet-port@2 {
+ reg = <2>;
+ label = "lan2";
+ phy-handle = <&phy2>;
+ };
+ ethernet-port@3 {
+ reg = <3>;
+ label = "lan3";
+ phy-handle = <&phy3>;
+ };
+ ethernet-port@9 {
+ reg = <9>;
+ ethernet = <&gmac0>;
+ phy-mode = "internal";
+ fixed-link {
+ speed = <1000>;
+ full-duplex;
+ };
+ };
+ };
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 4/6] net: dsa: realtek: add support for RTL9607C switch
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
` (2 preceding siblings ...)
2026-12-04 18:25 ` [RFC PATCH net-next 3/6] dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 5/6] net: phy: realtek: use mdio.addr instead of 0 in rtlgen_read/write_vend2 Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 6/6] net: phy: realtek: add support for RTL9607C internal PHYs Rustam Adilov
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
The RTL9607C SoC has an embedded ethernet switch connected
directly by MMIO interface. It has 4 internal PHYs, 1 PON serdes,
2 HSGMII serdes and 2/3 CPU ports.
It shares a lot of similarities with rtl8365mb ethernet switch or
more specifically family D variant.
Add the very basic DSA driver to support it with stats, internal
mdio, irqs, stp, port isolation and cpu port setup.
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
drivers/net/dsa/realtek/Kconfig | 7 +
drivers/net/dsa/realtek/Makefile | 2 +
drivers/net/dsa/realtek/realtek.h | 1 +
drivers/net/dsa/realtek/rtl9607c_main.c | 1525 +++++++++++++++++++++++
4 files changed, 1535 insertions(+)
create mode 100644 drivers/net/dsa/realtek/rtl9607c_main.c
diff --git a/drivers/net/dsa/realtek/Kconfig b/drivers/net/dsa/realtek/Kconfig
index aa4955fef4aa..d38a434ec834 100644
--- a/drivers/net/dsa/realtek/Kconfig
+++ b/drivers/net/dsa/realtek/Kconfig
@@ -57,4 +57,11 @@ config NET_DSA_REALTEK_RTL8366RB_LEDS
depends on NET_DSA_REALTEK_RTL8366RB
default NET_DSA_REALTEK_RTL8366RB
+config NET_DSA_REALTEK_RTL9607C
+ tristate "Realtek RTL9607C embedded switch driver"
+ depends on NET_DSA_REALTEK_MMIO
+ select NET_DSA_TAG_RTL8_4AP
+ help
+ Select to enable support for Realtek RTL9607C embedded switch.
+
endif
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index 0d3287952b31..c8fdb638cfc4 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -15,3 +15,5 @@ rtl8365mb-y := rtl8365mb_main.o \
rtl8365mb_vlan.o \
rtl8365mb_l2.o \
rtl8365mb_qos.o
+obj-$(CONFIG_NET_DSA_REALTEK_RTL9607C) += rtl9607c.o
+rtl9607c-y := rtl9607c_main.o
diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h
index cfe56d30f5cb..fb96e3a458ac 100644
--- a/drivers/net/dsa/realtek/realtek.h
+++ b/drivers/net/dsa/realtek/realtek.h
@@ -228,5 +228,6 @@ void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data);
extern const struct realtek_variant rtl8366rb_variant;
extern const struct realtek_variant rtl8365mb_variant;
+extern const struct realtek_variant rtl9607c_variant;
#endif /* _REALTEK_H */
diff --git a/drivers/net/dsa/realtek/rtl9607c_main.c b/drivers/net/dsa/realtek/rtl9607c_main.c
new file mode 100644
index 000000000000..5d5633c544bb
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl9607c_main.c
@@ -0,0 +1,1525 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Realtek MMIO subdriver for the Realtek RTL9607C embedded ethernet switch.
+ *
+ * The RTL9607C family of SoCs has an embedded 11 port switch controller.
+ * It includes 4 integrated PHYs for the user facing ports, 1 Serdes interface
+ * to operate with an external PON transceiver, 1 HSGMII interface that can be
+ * connected to an external PHY but is muxed with USB3, 2nd HSGMII interface
+ * that can be connected to an external PHY, or be a CPU NIC port, but is muxed
+ * with 2nd PCIE and lastly 2 primary CPU NIC ports.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/if_bridge.h>
+#include <linux/if_vlan.h>
+#include <linux/interrupt.h>
+#include <linux/irqdomain.h>
+#include <linux/mii.h>
+#include <linux/mutex.h>
+#include <linux/of_irq.h>
+#include <linux/mdio.h>
+#include <linux/phylink.h>
+#include <linux/regmap.h>
+
+#include "realtek.h"
+#include "realtek-mmio.h"
+#include "rtl83xx.h"
+
+/* Family-specific data and limits */
+#define RTL9607C_PHYADDRMAX 3
+#define RTL9607C_PAGE_SELECT 31
+#define RTL9607C_MAX_NUM_PORTS 11
+
+/* Chip identification registers */
+#define RTL9607C_CHIP_INFO_REG 0x10004
+#define RTL9607C_CHIP_INFO_EN_MASK GENMASK(31, 28)
+#define RTL9607C_CHIP_INFO_EN_VALUE 0xa
+#define RTL9607C_CHIP_INFO_VER_MASK GENMASK(20, 16)
+#define RTL9607C_CHIP_INFO_ID_MASK GENMASK(15, 0)
+
+/* Chip reset register */
+#define RTL9607C_SOFTWARE_RESET_REG 0x108
+#define RTL9607C_SOFTWARE_RESET_PONMAC_MASK BIT(11)
+#define RTL9607C_SOFTWARE_RESET_SWITCHCORE_MASK BIT(10)
+#define RTL9607C_SOFTWARE_RESET_GLOBAL_MASK BIT(9)
+#define RTL9607C_SOFTWARE_RESET_CONFIG_MASK BIT(8)
+#define RTL9607C_SOFTWARE_RESET_CHIP_MASK BIT(7)
+#define RTL9607C_SOFTWARE_RESET_GPHY_MASK BIT(6)
+#define RTL9607C_SOFTWARE_RESET_SERDES_MASK GENMASK(2, 0)
+
+/* Interrupt polarity register */
+#define RTL9607C_INTR_POLARITY_REG 0x1d000
+#define RTL9607C_INTR_POLARITY_MASK BIT(0)
+#define RTL9607C_INTR_POLARITY_HIGH 0
+#define RTL9607C_INTR_POLARITY_LOW 1
+
+/* Interrupt mask/status register - enable/check specific interrupt types */
+#define RTL9607C_INTR_IMR_REG 0x1d00c
+#define RTL9607C_INTR_IMS_REG 0x1d010
+#define RTL9607C_INTR_TOD_UPDATE_MASK BIT(20)
+#define RTL9607C_INTR_SFP_MASK BIT(19)
+#define RTL9607C_INTR_TOD_MASK BIT(18)
+#define RTL9607C_INTR_FB_EVENT_MASK BIT(17)
+#define RTL9607C_INTR_SWITCH_CRASH_MASK BIT(16)
+#define RTL9607C_INTR_SMARTCARD_MASK BIT(15)
+#define RTL9607C_INTR_THERMAL_ALARM_MASK BIT(14)
+#define RTL9607C_INTR_DYING_GASP_MASK BIT(13)
+#define RTL9607C_INTR_PTP_1_SEC_MASK BIT(12)
+#define RTL9607C_INTR_EPON_MASK BIT(11)
+#define RTL9607C_INTR_GPON_MASK BIT(10)
+#define RTL9607C_INTR_SERDES_MASK BIT(9)
+#define RTL9607C_INTR_GPHY_MASK BIT(8)
+#define RTL9607C_INTR_ACL_ACTION_MASK BIT(7)
+#define RTL9607C_INTR_DBGO_MASK BIT(6)
+#define RTL9607C_INTR_LOOP_DETECTION_MASK BIT(5)
+#define RTL9607C_INTR_SPECIAL_CONGEST_MASK BIT(4)
+#define RTL9607C_INTR_SPEED_CHANGE_MASK BIT(3)
+#define RTL9607C_INTR_LEARN_OVER_MASK BIT(2)
+#define RTL9607C_INTR_METER_EXCEEDED_MASK BIT(1)
+#define RTL9607C_INTR_LINK_CHANGE_MASK BIT(0)
+/* The define would be too big listing everything above so genmask it is */
+#define RTL9607C_INTR_ALL_MASK GENMASK(20, 0)
+
+#define RTL9607C_PORT_MASK GENMASK(10, 0)
+
+/* Per-port interrupt type status registers */
+#define RTL9607C_INTR_STATUS_REG 0x1d014
+#define RTL9607C_INTR_STAT_THERMAL_MASK GENMASK(31, 28)
+#define RTL9607C_INTR_STAT_GPHY_MASK GENMASK(20, 16)
+#define RTL9607C_INTR_STAT_PORT_LINKDOWN_MASK RTL9607C_PORT_MASK
+#define RTL9607C_INTR_STATUS1_REG 0x1d018
+#define RTL9607C_INTR_STAT_PORT_LINKUP_MASK (RTL9607C_PORT_MASK << 16)
+#define RTL9607C_INTR_STAT_PORT_SPEED_CHANGE_MASK RTL9607C_PORT_MASK
+
+/* PHY indirect access registers */
+#define RTL9607C_GPHY_INDIRECT_WRITE_DATA_REG 0x0
+#define RTL9607C_GPHY_INDIRECT_COMMAND_REG 0x4
+#define RTL9607C_INDIRECT_COMMAND_RW_MASK BIT(22)
+#define RTL9607C_INDIRECT_COMMAND_RW_READ 0
+#define RTL9607C_INDIRECT_COMMAND_RW_WRITE 1
+#define RTL9607C_INDIRECT_COMMAND_RUN BIT(21)
+#define RTL9607C_INDIRECT_COMMAND_PHYID_MASK GENMASK(20, 16)
+#define RTL9607C_INDIRECT_COMMAND_OCP_ADDR_MASK GENMASK(15, 0)
+#define RTL9607C_GPHY_INDIRECT_READ_DATA_REG 0x8
+#define RTL9607C_GPHY_STATUS_BUSY BIT(16)
+#define RTL9607C_GPHY_READ_DATA_MASK GENMASK(15, 0)
+
+#define RTL9607C_WRAP_GPHY_MISC_REG 0x114
+#define RTL9607C_PHY_PATCH_DONE_MASK BIT(0)
+
+/* The PHY OCP addresses of PHY registers 0~16, 24~31 start here */
+#define RTL9607C_PHY_OCP_ADDR_PHYREG_BASE 0xa400
+
+/* Forced MAC ability of a port */
+#define RTL9607C_FORCE_PORT_ABILITY_REG(_port) (0x1cc + (_port) * 4)
+#define RTL9607C_FORCE_PORT_ABILITY_NWAY BIT(7)
+#define RTL9607C_FORCE_PORT_ABILITY_LINK BIT(4)
+#define RTL9607C_FORCE_PORT_ABILITY_DUPLEX BIT(2)
+#define RTL9607C_FORCE_PORT_ABILITY_SPEED_MASK GENMASK(1, 0)
+#define RTL9607C_FORCE_PORT_ABILITY_SPEED_1000 2
+#define RTL9607C_ABILITY_FORCE_MODE_REG(_port) (0x238 + (_port) * 4)
+
+/* CPU tag control register */
+#define RTL9607C_CPU_TAG_INSERT_CTRL_REG 0x230f4
+#define RTL9607C_CPU_TAG_AWARE_CTRL_REG 0x230f8
+
+/* Maximum packet length register */
+#define RTL9607C_ACCEPT_MAX_LEN_REG(_port) (0x11028 + (_port) * 4)
+#define RTL9607C_ACCEPT_MAX_LEN_MASK GENMASK(13, 0)
+#define RTL9607C_MAX_PACKET_LENGTH 0x3fff
+
+/* Ethernet header, two stacked VLAN tags (802.1ad QinQ) and FCS */
+#define RTL9607C_FRAME_OVERHEAD (ETH_HLEN + 2 * VLAN_HLEN + ETH_FCS_LEN)
+
+/* Port learning limit registers */
+#define RTL9607C_LUT_PORT_LEARN_LIMIT_REG(_port) (0x17008 + ((_port) / 2) * 4)
+#define RTL9607C_LUT_PORT_LEARN_LIMIT_SHIFT(_port) (((_port) % 2) * 12)
+#define RTL9607C_LUT_PORT_LEARN_LIMIT_MASK(_port) \
+ (GENMASK(11, 0) << RTL9607C_LUT_PORT_LEARN_LIMIT_SHIFT(_port))
+
+/* Port isolation (forwarding mask) registers */
+#define RTL9607C_PORT_ISOLATION_REG(_port) (0x27000 + (_port) * 4)
+#define RTL9607C_PORT_ISOLATION_MASK RTL9607C_PORT_MASK
+
+/* MSTP port state registers - indexed by tree instance */
+#define RTL9607C_MSTI_CTRL_REG(_port) (0x1704c + (_port) * 4)
+#define RTL9607C_MSTI_CTRL_PORT_STATE_OFFSET(_msti) ((_msti) << 1)
+#define RTL9607C_MSTI_CTRL_PORT_STATE_MASK(_msti) \
+ (0x3 << RTL9607C_MSTI_CTRL_PORT_STATE_OFFSET(_msti))
+
+/* Broadcast flooding port mask */
+#define RTL9607C_UNKNOWN_BROADCAST_FLOOD_REG 0x1c028
+
+/* Unknown multicast DA flooding port mask */
+#define RTL9607C_UNKNOWN_MULTICAST_FLOOD_REG 0x1c02c
+
+/* Unknown unicast DA flooding port mask */
+#define RTL9607C_UNKNOWN_UNICAST_FLOOD_REG 0x1c030
+
+/* Per port MIB statistics */
+#define RTL9607C_STAT_PORT_MIB_BASE (0x32000)
+#define RTL9607C_STAT_PORT_MIB_REG(x) (RTL9607C_STAT_PORT_MIB_BASE + (x) * 0x80)
+/* Each define is an offset of RTL9607C_STAT_PORT_MIB */
+#define RTL9607C_IF_OUT_BROADCAST_PKTS (0x64)
+#define RTL9607C_IF_OUT_MULTICAST_PKTS (0x60)
+#define RTL9607C_IF_OUT_UCAST_PKTS (0x5c)
+#define RTL9607C_DOT1D_TP_PORT_IN_DISCARDS (0x58)
+#define RTL9607C_ETHER_STATS_TX_PKTS_1519_TO_MAX_OCTETS (0x50)
+#define RTL9607C_IF_OUT_DISCARDS (0x4c)
+#define RTL9607C_DOT3_OUT_PAUSE_FRAMES (0x48)
+#define RTL9607C_DOT3_STATS_EXCESSIVE_COLLISIONS (0x44)
+#define RTL9607C_ETHER_STATS_COLLISIONS (0x40)
+#define RTL9607C_DOT3_STATS_LATE_COLLISIONS (0x3c)
+#define RTL9607C_DOT3_STATS_DEFERRED_TRANSMISSIONS (0x38)
+#define RTL9607C_DOT3_STATS_MULTIPLE_COLLISION_FRAMES (0x34)
+#define RTL9607C_DOT3_STATS_SINGLE_COLLISION_FRAMES (0x30)
+#define RTL9607C_IF_OUT_OCTETS (0x28) /* 64 bits */
+#define RTL9607C_ETHER_STATS_TX_PKTS_1024_TO_1518_OCTETS (0x24)
+#define RTL9607C_ETHER_STATS_TX_PKTS_512_TO_1023_OCTETS (0x20)
+#define RTL9607C_ETHER_STATS_TX_PKTS_256_TO_511_OCTETS (0x1c)
+#define RTL9607C_ETHER_STATS_TX_PKTS_128_TO_255_OCTETS (0x18)
+#define RTL9607C_ETHER_STATS_TX_PKTS_65_TO_127_OCTETS (0x14)
+#define RTL9607C_ETHER_STATS_TX_PKTS_64_OCTETS (0x10)
+#define RTL9607C_ETHER_STATS_TX_OVERSIZE_PKTS (0xc)
+#define RTL9607C_ETHER_STATS_TX_UNDERSIZE_PKTS (0x8)
+#define RTL9607C_ETHER_STATS_TX_BROADCAST_PKTS (0x4)
+#define RTL9607C_ETHER_STATS_TX_MULTICAST_PKTS (0x0)
+#define STAT_RX_OFFSET (0x600)
+#define RTL9607C_ETHER_STATS_RX_PKTS_1024_TO_1518_OCTETS (STAT_RX_OFFSET + 0x54)
+#define RTL9607C_ETHER_STATS_RX_PKTS_512_TO_1023_OCTETS (STAT_RX_OFFSET + 0x50)
+#define RTL9607C_ETHER_STATS_RX_PKTS_256_TO_511_OCTETS (STAT_RX_OFFSET + 0x4c)
+#define RTL9607C_ETHER_STATS_RX_PKTS_128_TO_255_OCTETS (STAT_RX_OFFSET + 0x48)
+#define RTL9607C_ETHER_STATS_RX_PKTS_65_TO_127_OCTETS (STAT_RX_OFFSET + 0x44)
+#define RTL9607C_ETHER_STATS_RX_PKTS_64_OCTETS (STAT_RX_OFFSET + 0x40)
+#define RTL9607C_ETHER_STATS_RX_OVERSIZE_PKTS (STAT_RX_OFFSET + 0x3c)
+#define RTL9607C_ETHER_STATS_RX_UNDERSIZE_PKTS (STAT_RX_OFFSET + 0x38)
+#define RTL9607C_ETHER_STATS_RX_PKTS_1519_TO_MAX_OCTETS (STAT_RX_OFFSET + 0x30)
+#define RTL9607C_IF_IN_BROADCAST_PKTS (STAT_RX_OFFSET + 0x2c)
+#define RTL9607C_IF_IN_MULTICAST_PKTS (STAT_RX_OFFSET + 0x28)
+#define RTL9607C_ETHER_STATS_DROP_EVENTS (STAT_RX_OFFSET + 0x24)
+#define RTL9607C_IF_IN_UCAST_PKTS (STAT_RX_OFFSET + 0x20)
+#define RTL9607C_ETHER_STATS_JABBERS (STAT_RX_OFFSET + 0x1c)
+#define RTL9607C_ETHER_STATS_FRAGMENTS (STAT_RX_OFFSET + 0x18)
+#define RTL9607C_DOT3_CONTROL_IN_UNKNOWN_OPCODES (STAT_RX_OFFSET + 0x14)
+#define RTL9607C_DOT3_IN_PAUSE_FRAMES (STAT_RX_OFFSET + 0x10)
+#define RTL9607C_DOT3_STATS_SYMBOL_ERRORS (STAT_RX_OFFSET + 0xc)
+#define RTL9607C_ETHER_STATS_CRC_ALIGN_ERRORS (STAT_RX_OFFSET + 0x8)
+#define RTL9607C_IF_IN_OCTETS (STAT_RX_OFFSET + 0x0) /* 64 bits */
+#define RTL9607C_STAT_PORT_OAM_MIB(x) (RTL9607C_STAT_PORT_MIB_BASE + (x) * 0x8)
+#define STAT_OAM_OFFSET (0xc00)
+#define RTL9607C_OUT_OAM_PDU_PKTS (STAT_OAM_OFFSET + 0x0)
+#define RTL9607C_IN_OAM_PDU_PKTS (STAT_OAM_OFFSET + 0x4)
+
+#define RTL9607C_STAT_CTRL_REG 0x34000
+#define RTL9607C_STAT_PORT_RESET_REG 0x34014
+#define RTL9607C_STAT_RESET_REG 0x34018
+#define RTL9607C_STAT_RESET_STATUS_MASK BIT(3)
+#define RTL9607C_STAT_RESET_CONFIG_REG 0x3401c
+#define RTL9607C_STAT_RESET_CMD_MASK BIT(5)
+
+struct rtl9607c_mib_counter {
+ u32 offset;
+ u32 length;
+ const char *name;
+};
+
+#define MIB_DESC(_o, _l, _n) \
+{ \
+ .offset = (_o), \
+ .length = (_l), \
+ .name = (_n), \
+}
+
+static const struct rtl9607c_mib_counter rtl9607c_mib_counters[] = {
+ MIB_DESC(RTL9607C_ETHER_STATS_TX_MULTICAST_PKTS, 1, "etherStatsTxMulticastPkts"),
+ MIB_DESC(RTL9607C_ETHER_STATS_TX_BROADCAST_PKTS, 1, "etherStatsTxBroadcastPkts"),
+ MIB_DESC(RTL9607C_ETHER_STATS_TX_UNDERSIZE_PKTS, 1, "etherStatsTxUndersizePkts"),
+ MIB_DESC(RTL9607C_ETHER_STATS_TX_OVERSIZE_PKTS, 1, "etherStatsTxOversizePkts"),
+ MIB_DESC(RTL9607C_ETHER_STATS_COLLISIONS, 1, "etherStatsCollisions"),
+ MIB_DESC(RTL9607C_IF_OUT_DISCARDS, 1, "ifOutDiscards"),
+ MIB_DESC(RTL9607C_DOT1D_TP_PORT_IN_DISCARDS, 1, "dot1dTpPortInDiscards"),
+ MIB_DESC(RTL9607C_ETHER_STATS_DROP_EVENTS, 1, "etherStatsDropEvents"),
+ MIB_DESC(RTL9607C_ETHER_STATS_CRC_ALIGN_ERRORS, 1, "etherStatsCrcAlignErrors"),
+};
+
+/* Where all of the chip info struct will go */
+
+/**
+ * struct rtl9607c_chip_info - static chip-specific info
+ * @name: human-readable chip name
+ * @chip_id: chip identifier
+ * @chip_ver: chip silicon revision
+ * @port_mask: ports the chip has
+ *
+ * These data are specific to a given chip in the family of switches supported
+ * by this driver. When adding support for another chip in the family, a new
+ * chip info should be added to the rtl9607c_chip_infos array.
+ */
+struct rtl9607c_chip_info {
+ const char *name;
+ u32 chip_id;
+ u32 chip_ver;
+ u32 port_mask;
+};
+
+/* Every chip entry must set .port_mask through PORT_MASK(). PORT_MASK()
+ * fails the build when a port does not exist in the family
+ */
+#define PORT_MASK(_m) \
+ ((_m) + \
+ BUILD_BUG_ON_ZERO((_m) & ~GENMASK(RTL9607C_MAX_NUM_PORTS - 1, 0)))
+
+static const struct rtl9607c_chip_info rtl9607c_chip_infos[] = {
+ {
+ .name = "RTL9607C-VB",
+ .chip_id = 0x6831,
+ .chip_ver = 0x0001,
+ .port_mask = PORT_MASK(GENMASK(3, 0) | BIT(5) | BIT(6) | BIT(7) |
+ GENMASK(10, 9)),
+ },
+ {
+ .name = "RTL9607C-VA",
+ .chip_id = 0x6537,
+ .chip_ver = 0x0001,
+ .port_mask = PORT_MASK(GENMASK(3, 0) | BIT(5) | BIT(6) | BIT(7) |
+ GENMASK(10, 9)),
+ },
+};
+
+enum rtl9607c_stp_state {
+ RTL9607C_STP_STATE_DISABLED = 0,
+ RTL9607C_STP_STATE_BLOCKING = 1,
+ RTL9607C_STP_STATE_LEARNING = 2,
+ RTL9607C_STP_STATE_FORWARDING = 3,
+};
+
+/* Where the private per-port and driver data will go */
+
+/**
+ * struct rtl9607c - driver private data
+ * @priv: pointer to parent realtek_priv data
+ * @irq: registered IRQ or zero
+ * @chip_info: chip-specific info about the attached switch
+ * @page: phy page
+ *
+ * Private data for this driver.
+ */
+struct rtl9607c {
+ struct realtek_priv *priv;
+ int irq;
+ const struct rtl9607c_chip_info *chip_info;
+};
+
+static int rtl9607c_phy_poll_busy(struct realtek_priv *priv)
+{
+ u32 val;
+
+ return regmap_read_poll_timeout(priv->map,
+ RTL9607C_GPHY_INDIRECT_READ_DATA_REG,
+ val, !(val & RTL9607C_GPHY_STATUS_BUSY),
+ 10, 1000);
+}
+
+static int rtl9607c_phy_ocp_run_cmd(struct realtek_priv *priv, int phy,
+ u32 ocp_addr, u32 cmd_type)
+{
+ u32 val;
+ int ret;
+
+ val = RTL9607C_INDIRECT_COMMAND_RUN;
+ val |= FIELD_PREP(RTL9607C_INDIRECT_COMMAND_RW_MASK, cmd_type);
+ val |= FIELD_PREP(RTL9607C_INDIRECT_COMMAND_PHYID_MASK, phy);
+ val |= FIELD_PREP(RTL9607C_INDIRECT_COMMAND_OCP_ADDR_MASK, ocp_addr);
+ ret = regmap_write(priv->map, RTL9607C_GPHY_INDIRECT_COMMAND_REG, val);
+ if (ret)
+ return ret;
+
+ return rtl9607c_phy_poll_busy(priv);
+}
+
+static int rtl9607c_phy_ocp_read(struct realtek_priv *priv, int phy,
+ u32 ocp_addr, u16 *data)
+{
+ u32 val;
+ int ret;
+
+ mutex_lock(&priv->map_lock);
+
+ ret = rtl9607c_phy_poll_busy(priv);
+ if (ret)
+ goto out;
+
+ ret = rtl9607c_phy_ocp_run_cmd(priv, phy, ocp_addr,
+ RTL9607C_INDIRECT_COMMAND_RW_READ);
+ if (ret)
+ goto out;
+
+ /* Get PHY register data */
+ ret = regmap_read(priv->map, RTL9607C_GPHY_INDIRECT_READ_DATA_REG, &val);
+ if (ret)
+ goto out;
+
+ *data = val & RTL9607C_GPHY_READ_DATA_MASK;
+
+out:
+ mutex_unlock(&priv->map_lock);
+
+ return ret;
+}
+
+static int rtl9607c_phy_ocp_write(struct realtek_priv *priv, int phy,
+ u32 ocp_addr, u16 data)
+{
+ int ret;
+
+ mutex_lock(&priv->map_lock);
+
+ ret = rtl9607c_phy_poll_busy(priv);
+ if (ret)
+ goto out;
+
+ /* Set PHY register data */
+ ret = regmap_write(priv->map, RTL9607C_GPHY_INDIRECT_WRITE_DATA_REG, data);
+ if (ret)
+ goto out;
+
+ ret = rtl9607c_phy_ocp_run_cmd(priv, phy, ocp_addr,
+ RTL9607C_INDIRECT_COMMAND_RW_WRITE);
+ if (ret)
+ goto out;
+
+ ret = rtl9607c_phy_poll_busy(priv);
+ if (ret)
+ goto out;
+
+out:
+ mutex_unlock(&priv->map_lock);
+
+ return ret;
+}
+
+static int rtl9607c_phy_read(struct realtek_priv *priv, int phy, int regnum)
+{
+ u32 ocp_addr;
+ u16 val;
+ int ret;
+
+ if (phy > RTL9607C_PHYADDRMAX)
+ return -EINVAL;
+
+ ocp_addr = RTL9607C_PHY_OCP_ADDR_PHYREG_BASE + regnum * 2;
+
+ ret = rtl9607c_phy_ocp_read(priv, phy, ocp_addr, &val);
+ if (ret) {
+ dev_err(priv->dev,
+ "failed to read PHY%d reg %02x @ %04x, ret %pe\n", phy,
+ regnum, ocp_addr, ERR_PTR(ret));
+ return ret;
+ }
+
+ dev_dbg(priv->dev, "read PHY%d register 0x%02x @ %04x, val <- %04x\n",
+ phy, regnum, ocp_addr, val);
+
+ return val;
+}
+
+static int rtl9607c_phy_write(struct realtek_priv *priv, int phy, int regnum,
+ u16 val)
+{
+ u32 ocp_addr;
+ int ret;
+
+ if (phy > RTL9607C_PHYADDRMAX)
+ return -EINVAL;
+
+ ocp_addr = RTL9607C_PHY_OCP_ADDR_PHYREG_BASE + regnum * 2;
+
+ ret = rtl9607c_phy_ocp_write(priv, phy, ocp_addr, val);
+ if (ret) {
+ dev_err(priv->dev,
+ "failed to write PHY%d reg %02x @ %04x, ret %pe\n", phy,
+ regnum, ocp_addr, ERR_PTR(ret));
+ return ret;
+ }
+
+ dev_dbg(priv->dev, "write PHY%d register 0x%02x @ %04x, val -> %04x\n",
+ phy, regnum, ocp_addr, val);
+
+ return 0;
+}
+
+static int rtl9607c_phy_read_c45(struct realtek_priv *priv, int phy, int devad,
+ int regnum)
+{
+ u16 val;
+ int ret;
+
+ if (phy > RTL9607C_PHYADDRMAX)
+ return -EINVAL;
+
+ if (devad != MDIO_MMD_VEND2 || regnum <= MDIO_STAT2)
+ return 0;
+
+ ret = rtl9607c_phy_ocp_read(priv, phy, regnum, &val);
+ if (ret) {
+ dev_err(priv->dev,
+ "failed to read PHY%d OCP %04x, ret %d\n", phy, regnum, ret);
+ return ret;
+ }
+
+ return val;
+}
+
+static int rtl9607c_phy_write_c45(struct realtek_priv *priv, int phy,
+ int devad, int regnum, u16 val)
+{
+ int ret;
+
+ if (phy > RTL9607C_PHYADDRMAX)
+ return -EINVAL;
+
+ if (devad != MDIO_MMD_VEND2 || regnum <= MDIO_STAT2)
+ return -ENODEV;
+
+ ret = rtl9607c_phy_ocp_write(priv, phy, regnum, val);
+ if (ret) {
+ dev_err(priv->dev,
+ "failed to write PHY%d OCP %04x, ret %d\n", phy, regnum, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static enum dsa_tag_protocol
+rtl9607c_dsa_get_tag_protocol(struct dsa_switch *ds, int port,
+ enum dsa_tag_protocol mp)
+{
+ return DSA_TAG_PROTO_RTL8_4AP;
+}
+
+static void rtl9607c_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
+ struct phylink_config *config)
+{
+ switch (port) {
+ /* Ports connected to the integrated GPHYs */
+ case 0 ... 3:
+ __set_bit(PHY_INTERFACE_MODE_INTERNAL, config->supported_interfaces);
+ /*
+ * GMII is the default interface mode for phylib, so we have
+ * to support it for ports with integrated PHY.
+ */
+ __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
+
+ config->mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
+ MAC_10 | MAC_100 | MAC_1000FD;
+ break;
+ /* Main CPU NIC ports */
+ case 9:
+ case 10:
+ __set_bit(PHY_INTERFACE_MODE_INTERNAL, config->supported_interfaces);
+
+ config->mac_capabilities = MAC_1000FD;
+ break;
+ /* PON and 2 HSGMII serdes are not supported yet */
+ default:
+ break;
+ }
+}
+
+static void rtl9607c_dsa_mac_config(struct phylink_config *config,
+ unsigned int mode,
+ const struct phylink_link_state *state)
+{
+}
+
+static void rtl9607c_dsa_mac_link_down(struct phylink_config *config,
+ unsigned int mode,
+ phy_interface_t interface)
+{
+}
+
+static void rtl9607c_dsa_mac_link_up(struct phylink_config *config,
+ struct phy_device *phydev,
+ unsigned int mode,
+ phy_interface_t interface,
+ int speed, int duplex,
+ bool tx_pause, bool rx_pause)
+{
+}
+
+static int rtl9607c_port_change_mtu(struct dsa_switch *ds, int port,
+ int new_mtu)
+{
+ struct realtek_priv *priv = ds->priv;
+ int frame_size;
+
+ frame_size = new_mtu + RTL9607C_FRAME_OVERHEAD;
+
+ dev_dbg(priv->dev, "port %d changing mtu to %d (frame size: %d)\n",
+ port, new_mtu, frame_size);
+
+ return regmap_update_bits(priv->map, RTL9607C_ACCEPT_MAX_LEN_REG(port),
+ RTL9607C_ACCEPT_MAX_LEN_MASK,
+ FIELD_PREP(RTL9607C_ACCEPT_MAX_LEN_MASK,
+ frame_size));
+}
+
+static int rtl9607c_port_max_mtu(struct dsa_switch *ds, int port)
+{
+ return RTL9607C_MAX_PACKET_LENGTH - RTL9607C_FRAME_OVERHEAD;
+}
+
+static void rtl9607c_port_stp_state_set(struct dsa_switch *ds, int port,
+ u8 state)
+{
+ struct realtek_priv *priv = ds->priv;
+ enum rtl9607c_stp_state val;
+ int msti = 0;
+
+ switch (state) {
+ case BR_STATE_DISABLED:
+ val = RTL9607C_STP_STATE_DISABLED;
+ break;
+ case BR_STATE_BLOCKING:
+ case BR_STATE_LISTENING:
+ val = RTL9607C_STP_STATE_BLOCKING;
+ break;
+ case BR_STATE_LEARNING:
+ val = RTL9607C_STP_STATE_LEARNING;
+ break;
+ case BR_STATE_FORWARDING:
+ val = RTL9607C_STP_STATE_FORWARDING;
+ break;
+ default:
+ dev_err(priv->dev, "invalid STP state: %u\n", state);
+ return;
+ }
+
+ regmap_update_bits(priv->map, RTL9607C_MSTI_CTRL_REG(port),
+ RTL9607C_MSTI_CTRL_PORT_STATE_MASK(msti),
+ field_prep(RTL9607C_MSTI_CTRL_PORT_STATE_MASK(msti),
+ val));
+}
+
+/* Where VLAN functions should go in the future */
+
+static int rtl9607c_port_set_learning(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ /* Enable/disable learning by limiting the number of L2 addresses the
+ * port can learn. Realtek documentation states that a limit of zero
+ * disables learning. When enabling learning, set it 0xfff for no
+ * limitation.
+ */
+ return regmap_assign_bits(priv->map, RTL9607C_LUT_PORT_LEARN_LIMIT_REG(port),
+ RTL9607C_LUT_PORT_LEARN_LIMIT_MASK(port), enable);
+}
+
+static int rtl9607c_port_set_ucast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ /* Frames with unknown unicast DA will be flooded to a programmable
+ * port mask that by default includes all ports. Add or remove
+ * the specified port from this port mask accordingly.
+ */
+ return regmap_assign_bits(priv->map, RTL9607C_UNKNOWN_UNICAST_FLOOD_REG,
+ BIT(port), enable);
+}
+
+static int rtl9607c_port_set_mcast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ return regmap_assign_bits(priv->map, RTL9607C_UNKNOWN_MULTICAST_FLOOD_REG,
+ BIT(port), enable);
+}
+
+static int rtl9607c_port_set_bcast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ return regmap_assign_bits(priv->map, RTL9607C_UNKNOWN_BROADCAST_FLOOD_REG,
+ BIT(port), enable);
+}
+
+/* Port isolation manipulation functions.
+ *
+ * The port isolation register controls the forwarding mask of a given
+ * port. The switch will not forward packets ingressed on a given port
+ * to ports which are not enabled in its forwarding mask.
+ */
+static int rtl9607c_port_set_isolation(struct realtek_priv *priv, int port,
+ u32 mask)
+{
+ return regmap_write(priv->map, RTL9607C_PORT_ISOLATION_REG(port), mask);
+}
+
+static int rtl9607c_port_add_isolation(struct realtek_priv *priv, int port,
+ u32 mask)
+{
+ return regmap_set_bits(priv->map, RTL9607C_PORT_ISOLATION_REG(port),
+ mask);
+}
+
+static int rtl9607c_port_remove_isolation(struct realtek_priv *priv, int port,
+ u32 mask)
+{
+ return regmap_clear_bits(priv->map, RTL9607C_PORT_ISOLATION_REG(port),
+ mask);
+}
+
+static int rtl9607c_port_mib_reset(struct realtek_priv *priv, u32 pmask)
+{
+ u32 val;
+ int ret;
+
+ ret = regmap_set_bits(priv->map, RTL9607C_STAT_PORT_RESET_REG, pmask);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(priv->map, RTL9607C_STAT_RESET_CONFIG_REG,
+ RTL9607C_STAT_RESET_CMD_MASK);
+ if (ret)
+ return ret;
+
+ return regmap_read_poll_timeout(priv->map, RTL9607C_STAT_RESET_REG, val,
+ !FIELD_GET(RTL9607C_STAT_RESET_STATUS_MASK, val),
+ 10, 1000);
+}
+
+static void rtl9607c_mib_counter_read(struct realtek_priv *priv, int port, u32 offset,
+ u32 length, u64 *mibvalue)
+{
+ u32 val, reg = RTL9607C_STAT_PORT_MIB_REG(port) + offset;
+
+ regmap_read(priv->map, reg, &val);
+ *mibvalue = val;
+
+ if (length == 2) {
+ regmap_read(priv->map, reg + 4, &val);
+ *mibvalue |= (u64)val << 32;
+ }
+}
+
+static void rtl9607c_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
+{
+ struct realtek_priv *priv = ds->priv;
+ const struct rtl9607c_mib_counter *mib;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(rtl9607c_mib_counters); i++) {
+ mib = &rtl9607c_mib_counters[i];
+
+ rtl9607c_mib_counter_read(priv, port, mib->offset, mib->length, data + i);
+ }
+}
+
+static void rtl9607c_get_strings(struct dsa_switch *ds, int port, u32 stringset, u8 *data)
+{
+ if (stringset != ETH_SS_STATS)
+ return;
+
+ for (int i = 0; i < ARRAY_SIZE(rtl9607c_mib_counters); i++)
+ ethtool_puts(&data, rtl9607c_mib_counters[i].name);
+}
+
+static int rtl9607c_get_sset_count(struct dsa_switch *ds, int port, int sset)
+{
+ if (sset != ETH_SS_STATS)
+ return -EOPNOTSUPP;
+
+ return ARRAY_SIZE(rtl9607c_mib_counters);
+}
+
+static void rtl9607c_get_phy_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_phy_stats *phy_stats)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_SYMBOL_ERRORS, 1,
+ &phy_stats->SymbolErrorDuringCarrier);
+}
+
+static void rtl9607c_get_mac_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_mac_stats *mac_stats)
+{
+ struct realtek_priv *priv = ds->priv;
+ u64 data;
+
+ /* The RTL9607C exposes MIB objects, which we have to translate into
+ * IEEE 802.3 Managed Objects. This is not always completely faithful,
+ * but we try out best. See RFC 3635 for a detailed treatment of the
+ * subject.
+ */
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_UCAST_PKTS, 1,
+ &mac_stats->FramesTransmittedOK);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_SINGLE_COLLISION_FRAMES, 1,
+ &mac_stats->SingleCollisionFrames);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_MULTIPLE_COLLISION_FRAMES, 1,
+ &mac_stats->MultipleCollisionFrames);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_UCAST_PKTS, 1,
+ &mac_stats->FramesReceivedOK);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_OCTETS, 2,
+ &mac_stats->OctetsTransmittedOK);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_DEFERRED_TRANSMISSIONS, 1,
+ &mac_stats->FramesWithDeferredXmissions);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_LATE_COLLISIONS, 1,
+ &mac_stats->LateCollisions);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_EXCESSIVE_COLLISIONS, 1,
+ &mac_stats->FramesAbortedDueToXSColls);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_OCTETS, 2,
+ &mac_stats->OctetsReceivedOK);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_MULTICAST_PKTS, 1,
+ &mac_stats->MulticastFramesXmittedOK);
+ mac_stats->FramesTransmittedOK += mac_stats->MulticastFramesXmittedOK;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_BROADCAST_PKTS, 1,
+ &mac_stats->BroadcastFramesXmittedOK);
+ mac_stats->FramesTransmittedOK += mac_stats->BroadcastFramesXmittedOK;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_OUT_PAUSE_FRAMES, 1,
+ &data);
+ mac_stats->FramesTransmittedOK += data;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_DISCARDS, 1, &data);
+ mac_stats->FramesTransmittedOK -= data;
+
+ mac_stats->OctetsTransmittedOK -= 18 * mac_stats->FramesTransmittedOK;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_MULTICAST_PKTS, 1,
+ &mac_stats->MulticastFramesReceivedOK);
+ mac_stats->FramesReceivedOK += mac_stats->MulticastFramesReceivedOK;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_BROADCAST_PKTS, 1,
+ &mac_stats->BroadcastFramesReceivedOK);
+ mac_stats->FramesReceivedOK += mac_stats->BroadcastFramesReceivedOK;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_IN_PAUSE_FRAMES, 1,
+ &data);
+ mac_stats->FramesReceivedOK += data;
+
+ mac_stats->OctetsReceivedOK -= 18 * mac_stats->FramesReceivedOK;
+}
+
+static void rtl9607c_get_ctrl_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_ctrl_stats *ctrl_stats)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_CONTROL_IN_UNKNOWN_OPCODES, 1,
+ &ctrl_stats->UnsupportedOpcodesReceived);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_OUT_PAUSE_FRAMES, 1,
+ &ctrl_stats->MACControlFramesTransmitted);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_IN_PAUSE_FRAMES, 1,
+ &ctrl_stats->MACControlFramesReceived);
+}
+
+static const struct ethtool_rmon_hist_range rtl9607c_rmon_ranges[] = {
+ { 0, 64 },
+ { 65, 127 },
+ { 128, 255 },
+ { 256, 511 },
+ { 512, 1023 },
+ { 1024, 1518 },
+ { 1519, RTL9607C_MAX_PACKET_LENGTH },
+ { }
+};
+
+static void rtl9607c_get_rmon_stats(struct dsa_switch *ds, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_UNDERSIZE_PKTS, 1,
+ &rmon_stats->undersize_pkts);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_OVERSIZE_PKTS, 1,
+ &rmon_stats->oversize_pkts);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_FRAGMENTS, 1,
+ &rmon_stats->fragments);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_JABBERS, 1,
+ &rmon_stats->jabbers);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_64_OCTETS, 1,
+ &rmon_stats->hist[0]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_65_TO_127_OCTETS, 1,
+ &rmon_stats->hist[1]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_128_TO_255_OCTETS, 1,
+ &rmon_stats->hist[2]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_256_TO_511_OCTETS, 1,
+ &rmon_stats->hist[3]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_512_TO_1023_OCTETS, 1,
+ &rmon_stats->hist[4]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_1024_TO_1518_OCTETS, 1,
+ &rmon_stats->hist[5]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_RX_PKTS_1519_TO_MAX_OCTETS, 1,
+ &rmon_stats->hist[6]);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_64_OCTETS, 1,
+ &rmon_stats->hist_tx[0]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_65_TO_127_OCTETS, 1,
+ &rmon_stats->hist_tx[1]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_128_TO_255_OCTETS, 1,
+ &rmon_stats->hist_tx[2]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_256_TO_511_OCTETS, 1,
+ &rmon_stats->hist_tx[3]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_512_TO_1023_OCTETS, 1,
+ &rmon_stats->hist_tx[4]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_1024_TO_1518_OCTETS, 1,
+ &rmon_stats->hist_tx[5]);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_TX_PKTS_1519_TO_MAX_OCTETS, 1,
+ &rmon_stats->hist_tx[6]);
+
+ *ranges = rtl9607c_rmon_ranges;
+}
+
+static void rtl9607c_get_pause_stats(struct dsa_switch *ds, int port,
+ struct ethtool_pause_stats *pause_stats)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_OUT_PAUSE_FRAMES, 1,
+ &pause_stats->tx_pause_frames);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_IN_PAUSE_FRAMES, 1,
+ &pause_stats->rx_pause_frames);
+}
+
+static void rtl9607c_get_stats64(struct dsa_switch *ds, int port,
+ struct rtnl_link_stats64 *s)
+{
+ struct realtek_priv *priv = ds->priv;
+ u64 data;
+
+ /* MIB counter doesn't provide a FramesTransmittedOK but instead
+ * provide stats for Unicast, Broadcast and Multicast frames separately.
+ * To simulate a global frame counter, read Unicast and addition Multicast
+ * and Broadcast later
+ */
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_UCAST_PKTS, 1,
+ &s->rx_packets);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_MULTICAST_PKTS, 1,
+ &s->multicast);
+ s->rx_packets += s->multicast;
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_BROADCAST_PKTS, 1,
+ &data);
+ s->rx_packets += data;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_UCAST_PKTS, 1,
+ &s->tx_packets);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_MULTICAST_PKTS, 1,
+ &data);
+ s->tx_packets += data;
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_BROADCAST_PKTS, 1,
+ &data);
+ s->tx_packets += data;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_IN_OCTETS, 2,
+ &s->rx_bytes);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_OCTETS, 2,
+ &s->tx_bytes);
+
+ /* if{In,Out}Octets includes FCS - remove it */
+ s->rx_bytes -= 4 * s->rx_packets;
+ s->tx_bytes -= 4 * s->tx_packets;
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_DROP_EVENTS, 1,
+ &s->rx_dropped);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_IF_OUT_DISCARDS, 1,
+ &s->tx_dropped);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_COLLISIONS, 1,
+ &s->collisions);
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_FRAGMENTS, 1,
+ &s->rx_length_errors);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_ETHER_STATS_JABBERS, 1,
+ &data);
+ s->rx_length_errors += data;
+
+ s->rx_errors = s->rx_length_errors;
+
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_EXCESSIVE_COLLISIONS, 1,
+ &s->tx_aborted_errors);
+ rtl9607c_mib_counter_read(priv, port, RTL9607C_DOT3_STATS_LATE_COLLISIONS, 1,
+ &s->tx_window_errors);
+
+ s->tx_errors = s->tx_aborted_errors + s->tx_window_errors;
+}
+
+static int rtl9607c_get_and_clear_status_reg(struct realtek_priv *priv, u32 reg,
+ u32 *val)
+{
+ int ret;
+
+ ret = regmap_read(priv->map, reg, val);
+ if (ret)
+ return ret;
+
+ return regmap_write(priv->map, reg, *val);
+}
+
+static irqreturn_t rtl9607c_irq(int irq, void *data)
+{
+ struct realtek_priv *priv = data;
+ unsigned long line_changes = 0;
+ u32 stat;
+ int line;
+ int ret;
+
+ ret = rtl9607c_get_and_clear_status_reg(priv, RTL9607C_INTR_IMS_REG,
+ &stat);
+ if (ret)
+ goto out_error;
+
+ if (stat & (RTL9607C_INTR_LINK_CHANGE_MASK | RTL9607C_INTR_SPEED_CHANGE_MASK)) {
+ u32 linkdown_ind, linkup_ind, speed_ind, val;
+
+ ret = rtl9607c_get_and_clear_status_reg(priv, RTL9607C_INTR_STATUS_REG, &val);
+ if (ret)
+ goto out_error;
+
+ linkdown_ind = FIELD_GET(RTL9607C_INTR_STAT_PORT_LINKDOWN_MASK, val);
+
+ ret = rtl9607c_get_and_clear_status_reg(priv, RTL9607C_INTR_STATUS1_REG, &val);
+ if (ret)
+ goto out_error;
+
+ linkup_ind = FIELD_GET(RTL9607C_INTR_STAT_PORT_LINKUP_MASK, val);
+
+ speed_ind = FIELD_GET(RTL9607C_INTR_STAT_PORT_SPEED_CHANGE_MASK, val);
+
+ line_changes = linkdown_ind | linkup_ind | speed_ind;
+ }
+
+ if (!line_changes)
+ goto out_none;
+
+ for_each_set_bit(line, &line_changes, RTL9607C_MAX_NUM_PORTS) {
+ int child_irq = irq_find_mapping(priv->irqdomain, line);
+
+ if (!child_irq)
+ continue;
+
+ handle_nested_irq(child_irq);
+ }
+
+ return IRQ_HANDLED;
+
+out_error:
+ dev_err(priv->dev, "failed to read interrupt status: %pe\n",
+ ERR_PTR(ret));
+
+out_none:
+ return IRQ_NONE;
+}
+
+static struct irq_chip rtl9607c_irq_chip = {
+ .name = "rtl9607c-switch",
+ /* The hardware doesn't support masking IRQs on a per-port basis */
+};
+
+static int rtl9607c_irq_map(struct irq_domain *domain, unsigned int irq,
+ irq_hw_number_t hwirq)
+{
+ struct realtek_priv *priv = domain->host_data;
+ struct rtl9607c *mb = priv->chip_data;
+
+ irq_set_chip_data(irq, priv);
+ irq_set_chip_and_handler(irq, &rtl9607c_irq_chip, handle_simple_irq);
+ irq_set_nested_thread(irq, 1);
+ irq_set_noprobe(irq);
+ irq_set_parent(irq, mb->irq);
+
+ return 0;
+}
+
+static void rtl9607c_irq_unmap(struct irq_domain *d, unsigned int irq)
+{
+ irq_set_nested_thread(irq, 0);
+ irq_set_chip_and_handler(irq, NULL, NULL);
+ irq_set_chip_data(irq, NULL);
+}
+
+static const struct irq_domain_ops rtl9607c_irqdomain_ops = {
+ .map = rtl9607c_irq_map,
+ .unmap = rtl9607c_irq_unmap,
+ .xlate = irq_domain_xlate_onecell,
+};
+
+static int rtl9607c_irq_enable(struct realtek_priv *priv)
+{
+ return regmap_write(priv->map, RTL9607C_INTR_IMR_REG,
+ RTL9607C_INTR_LINK_CHANGE_MASK |
+ RTL9607C_INTR_SPEED_CHANGE_MASK);
+}
+
+static int rtl9607c_irq_disable(struct realtek_priv *priv)
+{
+ return regmap_write(priv->map, RTL9607C_INTR_IMR_REG, 0);
+}
+
+static void rtl9607c_irq_teardown(struct realtek_priv *priv)
+{
+ struct rtl9607c *mb = priv->chip_data;
+ struct dsa_switch *ds = &priv->ds;
+ struct dsa_port *dp;
+ int virq;
+
+ if (mb->irq) {
+ free_irq(mb->irq, priv);
+ mb->irq = 0;
+ }
+
+ if (priv->irqdomain) {
+ /* Unused ports with a linked PHY still have an active IRQ
+ * mapping that must be disposed of during teardown. Loop
+ * through all ports.
+ */
+ dsa_switch_for_each_port(dp, ds) {
+ virq = irq_find_mapping(priv->irqdomain, dp->index);
+
+ if (virq)
+ irq_dispose_mapping(virq);
+ }
+
+ irq_domain_remove(priv->irqdomain);
+ priv->irqdomain = NULL;
+ }
+}
+
+static int rtl9607c_irq_setup(struct realtek_priv *priv)
+{
+ struct rtl9607c *mb = priv->chip_data;
+ struct dsa_switch *ds = &priv->ds;
+ struct device_node *intc;
+ struct dsa_port *dp;
+ u32 irq_trig;
+ int virq;
+ int irq;
+ u32 val;
+ int ret;
+
+ intc = of_get_child_by_name(priv->dev->of_node, "interrupt-controller");
+ if (!intc) {
+ dev_err(priv->dev, "missing child interrupt-controller node\n");
+ return -EINVAL;
+ }
+
+ /* RTL9607C IRQs cascade off this one */
+ irq = of_irq_get(intc, 0);
+ if (irq <= 0) {
+ if (!irq) {
+ dev_err(priv->dev, "failed to map IRQ\n");
+ ret = -EINVAL;
+ } else {
+ ret = dev_err_probe(priv->dev, irq,
+ "failed to get parent irq\n");
+ }
+ goto out_put_node;
+ }
+
+ /* Store the irq so that we know to map and free it during teardown */
+ mb->irq = irq;
+
+ priv->irqdomain = irq_domain_create_linear(of_fwnode_handle(intc), RTL9607C_MAX_NUM_PORTS,
+ &rtl9607c_irqdomain_ops, priv);
+ if (!priv->irqdomain) {
+ dev_err(priv->dev, "failed to add irq domain\n");
+ ret = -ENOMEM;
+ goto out_put_node;
+ }
+
+ dsa_switch_for_each_available_port(dp, ds) {
+ virq = irq_create_mapping(priv->irqdomain, dp->index);
+ if (!virq) {
+ dev_err(priv->dev,
+ "failed to create irq domain mapping\n");
+ ret = -EINVAL;
+ goto out_teardown_irq;
+ }
+
+ irq_set_parent(virq, irq);
+ }
+
+ /* Configure chip interrupt signal polarity */
+ irq_trig = irq_get_trigger_type(irq);
+ switch (irq_trig) {
+ case IRQF_TRIGGER_RISING:
+ case IRQF_TRIGGER_HIGH:
+ val = RTL9607C_INTR_POLARITY_HIGH;
+ break;
+ case IRQF_TRIGGER_FALLING:
+ case IRQF_TRIGGER_LOW:
+ val = RTL9607C_INTR_POLARITY_LOW;
+ break;
+ default:
+ dev_err(priv->dev, "unsupported irq trigger type %u\n",
+ irq_trig);
+ ret = -EINVAL;
+ goto out_teardown_irq;
+ }
+
+ ret = regmap_update_bits(priv->map, RTL9607C_INTR_POLARITY_REG,
+ RTL9607C_INTR_POLARITY_MASK, val);
+ if (ret)
+ goto out_teardown_irq;
+
+ /* Disable the interrupt in case the chip has it enabled on reset */
+ ret = rtl9607c_irq_disable(priv);
+ if (ret)
+ goto out_teardown_irq;
+
+ /* Clear the interrupt status register */
+ ret = regmap_write(priv->map, RTL9607C_INTR_IMS_REG,
+ RTL9607C_INTR_ALL_MASK);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = regmap_write(priv->map, RTL9607C_INTR_STATUS_REG,
+ RTL9607C_INTR_STAT_PORT_LINKDOWN_MASK |
+ RTL9607C_INTR_STAT_GPHY_MASK);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = regmap_write(priv->map, RTL9607C_INTR_STATUS1_REG,
+ RTL9607C_INTR_STAT_PORT_LINKUP_MASK |
+ RTL9607C_INTR_STAT_PORT_SPEED_CHANGE_MASK);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = request_threaded_irq(irq, NULL, rtl9607c_irq, IRQF_ONESHOT,
+ "rtl9607c-switch", priv);
+ if (ret) {
+ dev_err(priv->dev, "failed to request irq: %pe\n",
+ ERR_PTR(ret));
+ goto out_teardown_irq;
+ }
+
+ ret = rtl9607c_irq_enable(priv);
+ if (ret)
+ goto out_teardown_irq;
+
+ of_node_put(intc);
+
+ return 0;
+
+out_teardown_irq:
+ rtl9607c_irq_teardown(priv);
+
+out_put_node:
+ mb->irq = 0;
+ of_node_put(intc);
+
+ return ret;
+}
+
+static int rtl9607c_cpu_port_setup(struct realtek_priv *priv, int port)
+{
+ u32 gmac_id;
+ int ret;
+
+ switch (port) {
+ case 9:
+ gmac_id = 0;
+ break;
+ case 10:
+ gmac_id = 1;
+ break;
+ case 7:
+ gmac_id = 2;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Force CPU port to link up at 1000/full */
+ ret = regmap_write(priv->map, RTL9607C_FORCE_PORT_ABILITY_REG(port),
+ RTL9607C_FORCE_PORT_ABILITY_LINK |
+ RTL9607C_FORCE_PORT_ABILITY_DUPLEX |
+ FIELD_PREP(RTL9607C_FORCE_PORT_ABILITY_SPEED_MASK,
+ RTL9607C_FORCE_PORT_ABILITY_SPEED_1000));
+ if (ret)
+ return ret;
+
+ ret = regmap_write(priv->map, RTL9607C_ABILITY_FORCE_MODE_REG(port),
+ 0xfff);
+ if (ret)
+ return ret;
+
+ ret = regmap_set_bits(priv->map, RTL9607C_CPU_TAG_INSERT_CTRL_REG, BIT(gmac_id));
+ if (ret)
+ return ret;
+
+ return regmap_set_bits(priv->map, RTL9607C_CPU_TAG_AWARE_CTRL_REG, BIT(gmac_id));
+}
+
+/* Where the potential chip reset function will go */
+
+static int rtl9607c_dsa_setup(struct dsa_switch *ds)
+{
+ struct realtek_priv *priv = ds->priv;
+ u32 cpu_ports = 0, user_ports = 0;
+ struct rtl9607c *mb;
+ struct dsa_port *dp;
+ int ret;
+
+ mb = priv->chip_data;
+
+ /* Set up cascading IRQs */
+ ret = rtl9607c_irq_setup(priv);
+ if (ret == -EPROBE_DEFER)
+ return ret;
+ else if (ret)
+ dev_info(priv->dev, "no interrupt support\n");
+
+ dsa_switch_for_each_available_port(dp, ds) {
+ if (!(mb->chip_info->port_mask & BIT(dp->index))) {
+ dev_err(priv->dev, "port %d does not exist on %s\n",
+ dp->index, mb->chip_info->name);
+ ret = -EINVAL;
+ goto out_teardown_irq;
+ }
+ }
+
+ /* Where QoS init will go in the future */
+
+ /* Start with all ports blocked, including unused ports */
+ dsa_switch_for_each_port(dp, ds) {
+ /* Set the initial STP state of all ports to DISABLED, otherwise
+ * ports will still forward frames to the CPU despite being
+ * administratively down by default.
+ */
+ rtl9607c_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED);
+
+ /* Start with all port completely isolated */
+ ret = rtl9607c_port_set_isolation(priv, dp->index, 0);
+ if (ret)
+ goto out_teardown_irq;
+
+ /* Disable learning */
+ ret = rtl9607c_port_set_learning(priv, dp->index, false);
+ if (ret)
+ goto out_teardown_irq;
+
+ /* Enable all types of flooding */
+ ret = rtl83xx_setup_port_flood_control(priv, dp->index);
+ if (ret)
+ goto out_teardown_irq;
+
+ if (dsa_port_is_cpu(dp))
+ cpu_ports |= BIT(dp->index);
+ }
+
+ /* Configure user ports */
+ dsa_switch_for_each_user_port(dp, ds) {
+ /* Forward only to the CPU */
+ ret = rtl9607c_port_set_isolation(priv, dp->index, cpu_ports);
+ if (ret)
+ goto out_teardown_irq;
+
+ user_ports |= BIT(dp->index);
+ }
+
+ if (cpu_ports == 0) {
+ dev_err(priv->dev, "no CPU port defined\n");
+ ret = -EINVAL;
+ goto out_teardown_irq;
+ }
+
+ /* Configure CPU ports and their tagging */
+ dsa_switch_for_each_cpu_port(dp, ds) {
+ ret = rtl9607c_cpu_port_setup(priv, dp->index);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = rtl9607c_port_set_isolation(priv, dp->index, user_ports);
+ if (ret)
+ goto out_teardown_irq;
+ }
+
+ ds->assisted_learning_on_cpu_port = true;
+
+ /* Where VLAN setup will go in the future */
+
+ /* PHY_PATCH_DONE enables PHY control via SoC. This is required for PHY
+ * access, including patching and must be set before the PHYs are probed.
+ */
+ ret = regmap_set_bits(priv->map, RTL9607C_WRAP_GPHY_MISC_REG,
+ RTL9607C_PHY_PATCH_DONE_MASK);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = rtl83xx_setup_user_mdio(ds);
+ if (ret) {
+ dev_err(priv->dev, "could not set up MDIO bus\n");
+ goto out_teardown_irq;
+ }
+
+ ret = rtl9607c_port_mib_reset(priv, user_ports | cpu_ports);
+ if (ret)
+ goto out_teardown_irq;
+
+ return 0;
+
+out_teardown_irq:
+ rtl9607c_irq_teardown(priv);
+
+ return ret;
+}
+
+static void rtl9607c_teardown(struct dsa_switch *ds)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ rtl9607c_irq_teardown(priv);
+}
+
+static int rtl9607c_get_chip_id_and_ver(struct regmap *map, u32 *id, u32 *ver)
+{
+ u32 chip_info;
+ int ret;
+
+ /* The CHIP_INFO_EN must be set to 0xa to enable chip information */
+ ret = regmap_set_bits(map, RTL9607C_CHIP_INFO_REG,
+ FIELD_PREP(RTL9607C_CHIP_INFO_EN_MASK,
+ RTL9607C_CHIP_INFO_EN_VALUE));
+ if (ret)
+ return ret;
+
+ ret = regmap_read(map, RTL9607C_CHIP_INFO_REG, &chip_info);
+ if (ret)
+ return ret;
+
+ *id = FIELD_GET(RTL9607C_CHIP_INFO_ID_MASK, chip_info);
+ *ver = FIELD_GET(RTL9607C_CHIP_INFO_VER_MASK, chip_info);
+
+ /* Reset CHIP_INFO_EN field */
+ return regmap_clear_bits(map, RTL9607C_CHIP_INFO_REG,
+ RTL9607C_CHIP_INFO_EN_MASK);
+}
+
+static int rtl9607c_detect(struct realtek_priv *priv)
+{
+ struct rtl9607c *mb = priv->chip_data;
+ u32 chip_id;
+ u32 chip_ver;
+ int ret;
+ int i;
+
+ ret = rtl9607c_get_chip_id_and_ver(priv->map, &chip_id, &chip_ver);
+ if (ret) {
+ dev_err(priv->dev, "failed to read chip id and version: %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(rtl9607c_chip_infos); i++) {
+ const struct rtl9607c_chip_info *ci = &rtl9607c_chip_infos[i];
+
+ if (ci->chip_id == chip_id && ci->chip_ver == chip_ver) {
+ mb->chip_info = ci;
+ break;
+ }
+ }
+
+ if (!mb->chip_info) {
+ dev_err(priv->dev,
+ "unrecognized switch (id=0x%04x, ver=0x%04x)", chip_id,
+ chip_ver);
+ return -ENODEV;
+ }
+
+ dev_info(priv->dev, "found an %s switch\n", mb->chip_info->name);
+
+ /* port_mask can have gaps, so count up to the highest port, not the
+ * ports set
+ */
+ priv->num_ports = fls(mb->chip_info->port_mask);
+ mb->priv = priv;
+
+ return 0;
+}
+
+static const struct phylink_mac_ops rtl9607c_phylink_mac_ops = {
+ .mac_config = rtl9607c_dsa_mac_config,
+ .mac_link_down = rtl9607c_dsa_mac_link_down,
+ .mac_link_up = rtl9607c_dsa_mac_link_up,
+};
+
+static const struct dsa_switch_ops rtl9607c_dsa_ops = {
+ .get_tag_protocol = rtl9607c_dsa_get_tag_protocol,
+ .setup = rtl9607c_dsa_setup,
+ .teardown = rtl9607c_teardown,
+ .phylink_get_caps = rtl9607c_dsa_phylink_get_caps,
+ .port_stp_state_set = rtl9607c_port_stp_state_set,
+ .get_strings = rtl9607c_get_strings,
+ .get_ethtool_stats = rtl9607c_get_ethtool_stats,
+ .get_sset_count = rtl9607c_get_sset_count,
+ .get_eth_phy_stats = rtl9607c_get_phy_stats,
+ .get_eth_mac_stats = rtl9607c_get_mac_stats,
+ .get_eth_ctrl_stats = rtl9607c_get_ctrl_stats,
+ .get_rmon_stats = rtl9607c_get_rmon_stats,
+ .get_stats64 = rtl9607c_get_stats64,
+ .get_pause_stats = rtl9607c_get_pause_stats,
+ .port_change_mtu = rtl9607c_port_change_mtu,
+ .port_max_mtu = rtl9607c_port_max_mtu,
+};
+
+static const struct realtek_ops rtl9607c_ops = {
+ .detect = rtl9607c_detect,
+ .port_add_isolation = rtl9607c_port_add_isolation,
+ .port_remove_isolation = rtl9607c_port_remove_isolation,
+ .port_set_learning = rtl9607c_port_set_learning,
+ .port_set_ucast_flood = rtl9607c_port_set_ucast_flood,
+ .port_set_mcast_flood = rtl9607c_port_set_mcast_flood,
+ .port_set_bcast_flood = rtl9607c_port_set_bcast_flood,
+ .phy_read = rtl9607c_phy_read,
+ .phy_write = rtl9607c_phy_write,
+ .phy_read_c45 = rtl9607c_phy_read_c45,
+ .phy_write_c45 = rtl9607c_phy_write_c45,
+};
+
+const struct realtek_variant rtl9607c_variant = {
+ .ds_ops = &rtl9607c_dsa_ops,
+ .ops = &rtl9607c_ops,
+ .phylink_mac_ops = &rtl9607c_phylink_mac_ops,
+ .chip_data_sz = sizeof(struct rtl9607c),
+};
+
+static const struct of_device_id rtl9607c_of_match[] = {
+ { .compatible = "realtek,rtl9607c-switch", .data = &rtl9607c_variant, },
+ { /* sentinel */ }
+};
+
+MODULE_DEVICE_TABLE(of, rtl9607c_of_match);
+
+static struct platform_driver rtl9607c_mmio_driver = {
+ .driver = {
+ .name = "rtl9607c-mmio",
+ .of_match_table = rtl9607c_of_match,
+ },
+ .probe = realtek_mmio_probe,
+ .remove = realtek_mmio_remove,
+ .shutdown = realtek_mmio_shutdown,
+};
+
+module_platform_driver(rtl9607c_mmio_driver);
+
+MODULE_AUTHOR("Rustam Adilov <adilov@disroot.org>");
+MODULE_DESCRIPTION("Driver for RTL9607C ethernet switch");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("REALTEK_DSA");
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 5/6] net: phy: realtek: use mdio.addr instead of 0 in rtlgen_read/write_vend2
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
` (3 preceding siblings ...)
2026-12-04 18:25 ` [RFC PATCH net-next 4/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 6/6] net: phy: realtek: add support for RTL9607C internal PHYs Rustam Adilov
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
Use the proper mdio.addr in rtlgen_read_vend2 and rtlgen_write_vend2
when passing addr to c45_read/write instead of hardcoded 0. That allows
mdio bus with multiple phy addresses like RTL9607C's internal mdio bus
to work properly.
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
drivers/net/phy/realtek/realtek_main.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index a39ad18382f2..80efaaddc986 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -1757,13 +1757,14 @@ static int rtlgen_read_status(struct phy_device *phydev)
static int rtlgen_read_vend2(struct phy_device *phydev, int regnum)
{
- return __mdiobus_c45_read(phydev->mdio.bus, 0, MDIO_MMD_VEND2, regnum);
+ return __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
+ MDIO_MMD_VEND2, regnum);
}
static int rtlgen_write_vend2(struct phy_device *phydev, int regnum, u16 val)
{
- return __mdiobus_c45_write(phydev->mdio.bus, 0, MDIO_MMD_VEND2, regnum,
- val);
+ return __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
+ MDIO_MMD_VEND2, regnum, val);
}
static int rtlgen_read_mmd(struct phy_device *phydev, int devnum, u16 regnum)
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH net-next 6/6] net: phy: realtek: add support for RTL9607C internal PHYs
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
` (4 preceding siblings ...)
2026-12-04 18:25 ` [RFC PATCH net-next 5/6] net: phy: realtek: use mdio.addr instead of 0 in rtlgen_read/write_vend2 Rustam Adilov
@ 2026-12-04 18:25 ` Rustam Adilov
5 siblings, 0 replies; 7+ messages in thread
From: Rustam Adilov @ 2026-12-04 18:25 UTC (permalink / raw)
To: Linus Walleij, Luiz Angelo Daros de Luca, Alvin Šipraga,
Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Heiner Kallweit, Russell King, Willem de Bruijn,
Simon Horman, netdev, devicetree, linux-kernel
Cc: Rustam Adilov
The phy id is 0x001ccac0 for RTL9607C-VA and 0x001cc981 for RTL9607C-VB.
It has the same interrupt handling as on RTL8366RB and a very similar
cable test procedure as RTL8224 PHY.
Just like with RTL8365MB-VC PHYs, RTL9607C's internal PHYs have no MMD
registers and stuff like EEE live in the switch's OCP address instead
and the C22 indirect-MMD is not implemented by this hardware.
But unlike in rtl8365mb phy driver, point the read_mmd/.write_mmd to
rtlgen_read_mmd and rtlgen_write_mmd as it handles the OCP address
conversion using the MDIO_MMD_VEND2 which the mdio bus will happily
take.
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
drivers/net/phy/realtek/realtek_main.c | 133 +++++++++++++++++++++++++
1 file changed, 133 insertions(+)
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index 80efaaddc986..5b01fb1af3fd 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -232,6 +232,10 @@
#define RTL9000A_GINMR 0x14
#define RTL9000A_GINMR_LINK_STATUS BIT(4)
+#define RTL9607C_SRAM_RTCT_FAULT(pair) (0x802b + (pair) * 4)
+#define RTL9607C_SRAM_RTCT_FAULT_MISMATCH (BIT(1) | BIT(2))
+#define RTL9607C_SRAM_RTCT_LEN(pair) (0x802d + (pair) * 4)
+
#define RTL_PHYSR MII_RESV2
#define RTL_PHYSR_DUPLEX BIT(3)
#define RTL_PHYSR_SPEEDL GENMASK(5, 4)
@@ -3175,6 +3179,109 @@ static int rtl8365mb_read_status(struct phy_device *phydev)
return genphy_read_status(phydev);
}
+static int rtl9607c_pair_len_get(struct phy_device *phydev, u32 pair)
+{
+ int ret;
+
+ ret = rtl8224_sram_read(phydev, RTL9607C_SRAM_RTCT_LEN(pair));
+ if (ret < 0)
+ return ret;
+
+ return ret * 5 / 4;
+}
+
+static int rtl9607c_cable_test_result_trans(u32 result)
+{
+ if (!(result & RTL8224_SRAM_RTCT_FAULT_DONE))
+ return -EBUSY;
+
+ if (result & RTL8224_SRAM_RTCT_FAULT_OK)
+ return ETHTOOL_A_CABLE_RESULT_CODE_OK;
+
+ if (result & RTL8224_SRAM_RTCT_FAULT_OPEN)
+ return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
+
+ if (result & RTL8224_SRAM_RTCT_FAULT_SAME_SHORT)
+ return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
+
+ if (result & RTL8224_SRAM_RTCT_FAULT_BUSY)
+ return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
+
+ if (result & RTL9607C_SRAM_RTCT_FAULT_MISMATCH)
+ return ETHTOOL_A_CABLE_RESULT_CODE_IMPEDANCE_MISMATCH;
+
+ return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
+}
+
+static int rtl9607c_cable_test_report_pair(struct phy_device *phydev, unsigned int pair)
+{
+ int fault_rslt;
+ int ret;
+
+ ret = rtl8224_sram_read(phydev, RTL9607C_SRAM_RTCT_FAULT(pair));
+ if (ret < 0)
+ return ret;
+
+ fault_rslt = rtl9607c_cable_test_result_trans(ret);
+ if (fault_rslt < 0)
+ return fault_rslt;
+
+ ret = ethnl_cable_test_result(phydev, pair, fault_rslt);
+ if (ret < 0)
+ return ret;
+
+ switch (fault_rslt) {
+ case ETHTOOL_A_CABLE_RESULT_CODE_OPEN:
+ case ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT:
+ case ETHTOOL_A_CABLE_RESULT_CODE_IMPEDANCE_MISMATCH:
+ ret = rtl9607c_pair_len_get(phydev, pair);
+ if (ret < 0)
+ return ret;
+
+ return ethnl_cable_test_fault_length(phydev, pair, ret);
+ default:
+ return 0;
+ }
+}
+
+static int rtl9607c_cable_test_report(struct phy_device *phydev, bool *finished)
+{
+ unsigned int pair;
+ int ret;
+
+ for (pair = ETHTOOL_A_CABLE_PAIR_A; pair <= ETHTOOL_A_CABLE_PAIR_D; pair++) {
+ ret = rtl9607c_cable_test_report_pair(phydev, pair);
+ if (ret == -EBUSY) {
+ *finished = false;
+ return 0;
+ }
+
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int rtl9607c_cable_test_get_status(struct phy_device *phydev, bool *finished)
+{
+ int ret;
+
+ *finished = false;
+
+ ret = phy_read_mmd(phydev, MDIO_MMD_VEND2,
+ RTL822X_VND2_C22_REG(RTL8224_MII_RTCT));
+ if (ret < 0)
+ return ret;
+
+ if (!(ret & RTL8224_MII_RTCT_DONE))
+ return 0;
+
+ *finished = true;
+
+ return rtl9607c_cable_test_report(phydev, finished);
+}
+
static struct phy_driver realtek_drvs[] = {
{
PHY_ID_MATCH_EXACT(0x00008201),
@@ -3521,6 +3628,32 @@ static struct phy_driver realtek_drvs[] = {
.soft_reset = genphy_c45_pma_soft_reset,
.suspend = genphy_c45_pma_suspend,
.resume = genphy_c45_pma_resume,
+ }, {
+ PHY_ID_MATCH_EXACT(0x001ccac0),
+ .name = "RTL9607C-VA Gigabit Ethernet",
+ .flags = PHY_POLL_CABLE_TEST,
+ /* Interrupt handling analogous to RTL8366RB */
+ .config_intr = genphy_no_config_intr,
+ .handle_interrupt = genphy_handle_interrupt_no_ack,
+ .suspend = genphy_suspend,
+ .resume = genphy_resume,
+ .read_mmd = rtlgen_read_mmd,
+ .write_mmd = rtlgen_write_mmd,
+ .cable_test_start = rtl8224_cable_test_start,
+ .cable_test_get_status = rtl9607c_cable_test_get_status,
+ }, {
+ PHY_ID_MATCH_EXACT(0x001cc981),
+ .name = "RTL9607C-VB Gigabit Ethernet",
+ .flags = PHY_POLL_CABLE_TEST,
+ /* Interrupt handling analogous to RTL8366RB */
+ .config_intr = genphy_no_config_intr,
+ .handle_interrupt = genphy_handle_interrupt_no_ack,
+ .suspend = genphy_suspend,
+ .resume = genphy_resume,
+ .read_mmd = rtlgen_read_mmd,
+ .write_mmd = rtlgen_write_mmd,
+ .cable_test_start = rtl8224_cable_test_start,
+ .cable_test_get_status = rtl9607c_cable_test_get_status,
},
};
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-04 18:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 2/6] net: dsa: realtek: add new mmio interface for drivers Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 3/6] dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 4/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 5/6] net: phy: realtek: use mdio.addr instead of 0 in rtlgen_read/write_vend2 Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 6/6] net: phy: realtek: add support for RTL9607C internal PHYs Rustam Adilov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox