1 From 110c18bfed41421edd677935dd33be5e6507ba92 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 3 Apr 2023 02:19:40 +0100
4 Subject: [PATCH 16/16] net: dsa: mt7530: introduce driver for MT7988 built-in
7 Add driver for the built-in Gigabit Ethernet switch which can be found
8 in the MediaTek MT7988 SoC.
10 The switch shares most of its design with MT7530 and MT7531, but has
11 it's registers mapped into the SoCs register space rather than being
12 connected externally or internally via MDIO.
14 Introduce a new platform driver to support that.
16 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
17 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
18 Signed-off-by: David S. Miller <davem@davemloft.net>
21 drivers/net/dsa/Kconfig | 12 +++
22 drivers/net/dsa/Makefile | 1 +
23 drivers/net/dsa/mt7530-mmio.c | 101 +++++++++++++++++++++++++
24 drivers/net/dsa/mt7530.c | 137 +++++++++++++++++++++++++++++++++-
25 drivers/net/dsa/mt7530.h | 12 +--
26 6 files changed, 255 insertions(+), 10 deletions(-)
27 create mode 100644 drivers/net/dsa/mt7530-mmio.c
31 @@ -11900,9 +11900,11 @@ MEDIATEK SWITCH DRIVER
32 M: Sean Wang <sean.wang@mediatek.com>
33 M: Landen Chao <Landen.Chao@mediatek.com>
34 M: DENG Qingfang <dqfext@gmail.com>
35 +M: Daniel Golle <daniel@makrotopia.org>
36 L: netdev@vger.kernel.org
38 F: drivers/net/dsa/mt7530-mdio.c
39 +F: drivers/net/dsa/mt7530-mmio.c
40 F: drivers/net/dsa/mt7530.*
43 --- a/drivers/net/dsa/Kconfig
44 +++ b/drivers/net/dsa/Kconfig
45 @@ -38,6 +38,7 @@ config NET_DSA_MT7530
46 select NET_DSA_TAG_MTK
47 select MEDIATEK_GE_PHY
48 imply NET_DSA_MT7530_MDIO
49 + imply NET_DSA_MT7530_MMIO
51 This enables support for the MediaTek MT7530 and MT7531 Ethernet
52 switch chips. Multi-chip module MT7530 in MT7621AT, MT7621DAT,
53 @@ -54,6 +55,17 @@ config NET_DSA_MT7530_MDIO
54 module MT7530 which can be found in the MT7621AT, MT7621DAT,
55 MT7621ST and MT7623AI SoCs.
57 +config NET_DSA_MT7530_MMIO
58 + tristate "MediaTek MT7530 MMIO interface driver"
59 + depends on NET_DSA_MT7530
60 + depends on HAS_IOMEM
62 + This enables support for the built-in Ethernet switch found
63 + in the MediaTek MT7988 SoC.
64 + The switch is a similar design as MT7531, but the switch registers
65 + are directly mapped into the SoCs register space rather than being
66 + accessible via MDIO.
68 config NET_DSA_MV88E6060
69 tristate "Marvell 88E6060 ethernet switch chip support"
70 select NET_DSA_TAG_TRAILER
71 --- a/drivers/net/dsa/Makefile
72 +++ b/drivers/net/dsa/Makefile
74 obj-$(CONFIG_NET_DSA_LANTIQ_GSWIP) += lantiq_gswip.o
75 obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o
76 obj-$(CONFIG_NET_DSA_MT7530_MDIO) += mt7530-mdio.o
77 +obj-$(CONFIG_NET_DSA_MT7530_MMIO) += mt7530-mmio.o
78 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
79 obj-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o
80 realtek-smi-objs := realtek-smi-core.o rtl8366.o rtl8366rb.o
82 +++ b/drivers/net/dsa/mt7530-mmio.c
84 +// SPDX-License-Identifier: GPL-2.0-only
86 +#include <linux/module.h>
87 +#include <linux/of_platform.h>
88 +#include <linux/regmap.h>
89 +#include <linux/regulator/consumer.h>
90 +#include <linux/reset.h>
95 +static const struct of_device_id mt7988_of_match[] = {
96 + { .compatible = "mediatek,mt7988-switch", .data = &mt753x_table[ID_MT7988], },
99 +MODULE_DEVICE_TABLE(of, mt7988_of_match);
102 +mt7988_probe(struct platform_device *pdev)
104 + static struct regmap_config *sw_regmap_config;
105 + struct mt7530_priv *priv;
106 + void __iomem *base_addr;
109 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
114 + priv->dev = &pdev->dev;
116 + ret = mt7530_probe_common(priv);
120 + priv->rstc = devm_reset_control_get(&pdev->dev, NULL);
121 + if (IS_ERR(priv->rstc)) {
122 + dev_err(&pdev->dev, "Couldn't get our reset line\n");
123 + return PTR_ERR(priv->rstc);
126 + base_addr = devm_platform_ioremap_resource(pdev, 0);
127 + if (IS_ERR(base_addr)) {
128 + dev_err(&pdev->dev, "cannot request I/O memory space\n");
132 + sw_regmap_config = devm_kzalloc(&pdev->dev, sizeof(*sw_regmap_config), GFP_KERNEL);
133 + if (!sw_regmap_config)
136 + sw_regmap_config->name = "switch";
137 + sw_regmap_config->reg_bits = 16;
138 + sw_regmap_config->val_bits = 32;
139 + sw_regmap_config->reg_stride = 4;
140 + sw_regmap_config->max_register = MT7530_CREV;
141 + priv->regmap = devm_regmap_init_mmio(&pdev->dev, base_addr, sw_regmap_config);
142 + if (IS_ERR(priv->regmap))
143 + return PTR_ERR(priv->regmap);
145 + return dsa_register_switch(priv->ds);
149 +mt7988_remove(struct platform_device *pdev)
151 + struct mt7530_priv *priv = platform_get_drvdata(pdev);
154 + mt7530_remove_common(priv);
159 +static void mt7988_shutdown(struct platform_device *pdev)
161 + struct mt7530_priv *priv = platform_get_drvdata(pdev);
166 + dsa_switch_shutdown(priv->ds);
168 + dev_set_drvdata(&pdev->dev, NULL);
171 +static struct platform_driver mt7988_platform_driver = {
172 + .probe = mt7988_probe,
173 + .remove = mt7988_remove,
174 + .shutdown = mt7988_shutdown,
176 + .name = "mt7530-mmio",
177 + .of_match_table = mt7988_of_match,
180 +module_platform_driver(mt7988_platform_driver);
182 +MODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
183 +MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch (MMIO)");
184 +MODULE_LICENSE("GPL");
185 --- a/drivers/net/dsa/mt7530.c
186 +++ b/drivers/net/dsa/mt7530.c
187 @@ -2198,6 +2198,47 @@ static const struct irq_domain_ops mt753
191 +mt7988_irq_mask(struct irq_data *d)
193 + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
195 + priv->irq_enable &= ~BIT(d->hwirq);
196 + mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
200 +mt7988_irq_unmask(struct irq_data *d)
202 + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
204 + priv->irq_enable |= BIT(d->hwirq);
205 + mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
208 +static struct irq_chip mt7988_irq_chip = {
209 + .name = KBUILD_MODNAME,
210 + .irq_mask = mt7988_irq_mask,
211 + .irq_unmask = mt7988_irq_unmask,
215 +mt7988_irq_map(struct irq_domain *domain, unsigned int irq,
216 + irq_hw_number_t hwirq)
218 + irq_set_chip_data(irq, domain->host_data);
219 + irq_set_chip_and_handler(irq, &mt7988_irq_chip, handle_simple_irq);
220 + irq_set_nested_thread(irq, true);
221 + irq_set_noprobe(irq);
226 +static const struct irq_domain_ops mt7988_irq_domain_ops = {
227 + .map = mt7988_irq_map,
228 + .xlate = irq_domain_xlate_onecell,
232 mt7530_setup_mdio_irq(struct mt7530_priv *priv)
234 struct dsa_switch *ds = priv->ds;
235 @@ -2231,8 +2272,15 @@ mt7530_setup_irq(struct mt7530_priv *pri
236 return priv->irq ? : -EINVAL;
239 - priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
240 - &mt7530_irq_domain_ops, priv);
241 + if (priv->id == ID_MT7988)
242 + priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
243 + &mt7988_irq_domain_ops,
246 + priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
247 + &mt7530_irq_domain_ops,
250 if (!priv->irq_domain) {
251 dev_err(dev, "failed to create IRQ domain\n");
253 @@ -2727,6 +2775,25 @@ static void mt7531_mac_port_get_caps(str
257 +static void mt7988_mac_port_get_caps(struct dsa_switch *ds, int port,
258 + struct phylink_config *config)
260 + phy_interface_zero(config->supported_interfaces);
263 + case 0 ... 4: /* Internal phy */
264 + __set_bit(PHY_INTERFACE_MODE_INTERNAL,
265 + config->supported_interfaces);
269 + __set_bit(PHY_INTERFACE_MODE_INTERNAL,
270 + config->supported_interfaces);
271 + config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
277 mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
279 @@ -2803,6 +2870,17 @@ static bool mt753x_is_mac_port(u32 port)
283 +mt7988_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
284 + phy_interface_t interface)
286 + if (dsa_is_cpu_port(ds, port) &&
287 + interface == PHY_INTERFACE_MODE_INTERNAL)
294 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
295 phy_interface_t interface)
297 @@ -2872,7 +2950,8 @@ mt753x_phylink_mac_config(struct dsa_swi
300 case 0 ... 4: /* Internal phy */
301 - if (state->interface != PHY_INTERFACE_MODE_GMII)
302 + if (state->interface != PHY_INTERFACE_MODE_GMII &&
303 + state->interface != PHY_INTERFACE_MODE_INTERNAL)
306 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
307 @@ -2950,7 +3029,8 @@ static void mt753x_phylink_mac_link_up(s
308 /* MT753x MAC works in 1G full duplex mode for all up-clocked
311 - if (interface == PHY_INTERFACE_MODE_TRGMII ||
312 + if (interface == PHY_INTERFACE_MODE_INTERNAL ||
313 + interface == PHY_INTERFACE_MODE_TRGMII ||
314 (phy_interface_mode_is_8023z(interface))) {
316 duplex = DUPLEX_FULL;
317 @@ -3030,6 +3110,21 @@ mt7531_cpu_port_config(struct dsa_switch
322 +mt7988_cpu_port_config(struct dsa_switch *ds, int port)
324 + struct mt7530_priv *priv = ds->priv;
326 + mt7530_write(priv, MT7530_PMCR_P(port),
327 + PMCR_CPU_PORT_SETTING(priv->id));
329 + mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED,
330 + PHY_INTERFACE_MODE_INTERNAL, NULL,
331 + SPEED_10000, DUPLEX_FULL, true, true);
336 static void mt753x_phylink_get_caps(struct dsa_switch *ds, int port,
337 struct phylink_config *config)
339 @@ -3175,6 +3270,27 @@ static int mt753x_set_mac_eee(struct dsa
343 +static int mt7988_pad_setup(struct dsa_switch *ds, phy_interface_t interface)
348 +static int mt7988_setup(struct dsa_switch *ds)
350 + struct mt7530_priv *priv = ds->priv;
352 + /* Reset the switch */
353 + reset_control_assert(priv->rstc);
354 + usleep_range(20, 50);
355 + reset_control_deassert(priv->rstc);
356 + usleep_range(20, 50);
358 + /* Reset the switch PHYs */
359 + mt7530_write(priv, MT7530_SYS_CTRL, SYS_CTRL_PHY_RST);
361 + return mt7531_setup_common(ds);
364 const struct dsa_switch_ops mt7530_switch_ops = {
365 .get_tag_protocol = mtk_get_tag_protocol,
366 .setup = mt753x_setup,
367 @@ -3243,6 +3359,17 @@ const struct mt753x_info mt753x_table[]
368 .mac_port_get_caps = mt7531_mac_port_get_caps,
369 .mac_port_config = mt7531_mac_config,
373 + .pcs_ops = &mt7530_pcs_ops,
374 + .sw_setup = mt7988_setup,
375 + .phy_read = mt7531_ind_phy_read,
376 + .phy_write = mt7531_ind_phy_write,
377 + .pad_setup = mt7988_pad_setup,
378 + .cpu_port_config = mt7988_cpu_port_config,
379 + .mac_port_get_caps = mt7988_mac_port_get_caps,
380 + .mac_port_config = mt7988_mac_config,
383 EXPORT_SYMBOL_GPL(mt753x_table);
385 --- a/drivers/net/dsa/mt7530.h
386 +++ b/drivers/net/dsa/mt7530.h
387 @@ -18,6 +18,7 @@ enum mt753x_id {
394 #define NUM_TRGMII_CTRL 5
395 @@ -54,11 +55,11 @@ enum mt753x_id {
396 #define MT7531_MIRROR_PORT_SET(x) (((x) & MIRROR_MASK) << 16)
397 #define MT7531_CPU_PMAP_MASK GENMASK(7, 0)
399 -#define MT753X_MIRROR_REG(id) (((id) == ID_MT7531) ? \
400 +#define MT753X_MIRROR_REG(id) ((((id) == ID_MT7531) || ((id) == ID_MT7988)) ? \
401 MT7531_CFC : MT7530_MFC)
402 -#define MT753X_MIRROR_EN(id) (((id) == ID_MT7531) ? \
403 +#define MT753X_MIRROR_EN(id) ((((id) == ID_MT7531) || ((id) == ID_MT7988)) ? \
404 MT7531_MIRROR_EN : MIRROR_EN)
405 -#define MT753X_MIRROR_MASK(id) (((id) == ID_MT7531) ? \
406 +#define MT753X_MIRROR_MASK(id) ((((id) == ID_MT7531) || ((id) == ID_MT7988)) ? \
407 MT7531_MIRROR_MASK : MIRROR_MASK)
409 /* Registers for BPDU and PAE frame control*/
410 @@ -327,9 +328,8 @@ enum mt7530_vlan_port_acc_frm {
412 MT7531_FORCE_RX_FC | \
414 -#define PMCR_FORCE_MODE_ID(id) (((id) == ID_MT7531) ? \
415 - MT7531_FORCE_MODE : \
417 +#define PMCR_FORCE_MODE_ID(id) ((((id) == ID_MT7531) || ((id) == ID_MT7988)) ? \
418 + MT7531_FORCE_MODE : PMCR_FORCE_MODE)
419 #define PMCR_LINK_SETTINGS_MASK (PMCR_TX_EN | PMCR_FORCE_SPEED_1000 | \
420 PMCR_RX_EN | PMCR_FORCE_SPEED_100 | \
421 PMCR_TX_FC_EN | PMCR_RX_FC_EN | \