1 From cb675afcddbbeb2bfa6596e3bc236bc026cd425f Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 3 Apr 2023 02:19:13 +0100
4 Subject: [PATCH 14/16] net: dsa: mt7530: introduce separate MDIO driver
6 Split MT7530 switch driver into a common part and a part specific
7 for MDIO connected switches and multi-chip modules.
8 Move MDIO-specific functions to newly introduced mt7530-mdio.c while
9 keeping the common parts in mt7530.c.
10 Introduce new Kconfig symbol CONFIG_NET_DSA_MT7530_MDIO which is
11 implied by CONFIG_NET_DSA_MT7530.
13 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
14 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
15 Signed-off-by: David S. Miller <davem@davemloft.net>
18 drivers/net/dsa/Kconfig | 15 +-
19 drivers/net/dsa/Makefile | 1 +
20 drivers/net/dsa/mt7530-mdio.c | 271 ++++++++++++++++++++++++++++++++++
21 drivers/net/dsa/mt7530.c | 264 +--------------------------------
22 drivers/net/dsa/mt7530.h | 6 +
23 6 files changed, 300 insertions(+), 258 deletions(-)
24 create mode 100644 drivers/net/dsa/mt7530-mdio.c
28 @@ -11902,6 +11902,7 @@ M: Landen Chao <Landen.Chao@mediatek.com
29 M: DENG Qingfang <dqfext@gmail.com>
30 L: netdev@vger.kernel.org
32 +F: drivers/net/dsa/mt7530-mdio.c
33 F: drivers/net/dsa/mt7530.*
36 --- a/drivers/net/dsa/Kconfig
37 +++ b/drivers/net/dsa/Kconfig
38 @@ -37,10 +37,22 @@ config NET_DSA_MT7530
39 tristate "MediaTek MT753x and MT7621 Ethernet switch support"
40 select NET_DSA_TAG_MTK
41 select MEDIATEK_GE_PHY
42 + imply NET_DSA_MT7530_MDIO
44 + This enables support for the MediaTek MT7530 and MT7531 Ethernet
45 + switch chips. Multi-chip module MT7530 in MT7621AT, MT7621DAT,
46 + MT7621ST and MT7623AI SoCs, and built-in switch in MT7988 SoC are
49 +config NET_DSA_MT7530_MDIO
50 + tristate "MediaTek MT7530 MDIO interface driver"
51 + depends on NET_DSA_MT7530
54 - This enables support for the MediaTek MT7530, MT7531, and MT7621
55 - Ethernet switch chips.
56 + This enables support for the MediaTek MT7530 and MT7531 switch
57 + chips which are connected via MDIO, as well as multi-chip
58 + module MT7530 which can be found in the MT7621AT, MT7621DAT,
59 + MT7621ST and MT7623AI SoCs.
61 config NET_DSA_MV88E6060
62 tristate "Marvell 88E6060 ethernet switch chip support"
63 --- a/drivers/net/dsa/Makefile
64 +++ b/drivers/net/dsa/Makefile
65 @@ -7,6 +7,7 @@ obj-$(CONFIG_FIXED_PHY) += dsa_loop_bdi
67 obj-$(CONFIG_NET_DSA_LANTIQ_GSWIP) += lantiq_gswip.o
68 obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o
69 +obj-$(CONFIG_NET_DSA_MT7530_MDIO) += mt7530-mdio.o
70 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
71 obj-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o
72 realtek-smi-objs := realtek-smi-core.o rtl8366.o rtl8366rb.o
74 +++ b/drivers/net/dsa/mt7530-mdio.c
76 +// SPDX-License-Identifier: GPL-2.0-only
78 +#include <linux/gpio/consumer.h>
79 +#include <linux/mdio.h>
80 +#include <linux/module.h>
81 +#include <linux/pcs/pcs-mtk-lynxi.h>
82 +#include <linux/of_irq.h>
83 +#include <linux/of_mdio.h>
84 +#include <linux/of_net.h>
85 +#include <linux/of_platform.h>
86 +#include <linux/regmap.h>
87 +#include <linux/reset.h>
88 +#include <linux/regulator/consumer.h>
94 +mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
96 + struct mii_bus *bus = context;
97 + u16 page, r, lo, hi;
100 + page = (reg >> 6) & 0x3ff;
101 + r = (reg >> 2) & 0xf;
105 + /* MT7530 uses 31 as the pseudo port */
106 + ret = bus->write(bus, 0x1f, 0x1f, page);
110 + ret = bus->write(bus, 0x1f, r, lo);
114 + ret = bus->write(bus, 0x1f, 0x10, hi);
119 +mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
121 + struct mii_bus *bus = context;
122 + u16 page, r, lo, hi;
125 + page = (reg >> 6) & 0x3ff;
126 + r = (reg >> 2) & 0xf;
128 + /* MT7530 uses 31 as the pseudo port */
129 + ret = bus->write(bus, 0x1f, 0x1f, page);
133 + lo = bus->read(bus, 0x1f, r);
134 + hi = bus->read(bus, 0x1f, 0x10);
136 + *val = (hi << 16) | (lo & 0xffff);
142 +mt7530_mdio_regmap_lock(void *mdio_lock)
144 + mutex_lock_nested(mdio_lock, MDIO_MUTEX_NESTED);
148 +mt7530_mdio_regmap_unlock(void *mdio_lock)
150 + mutex_unlock(mdio_lock);
153 +static const struct regmap_bus mt7530_regmap_bus = {
154 + .reg_write = mt7530_regmap_write,
155 + .reg_read = mt7530_regmap_read,
159 +mt7531_create_sgmii(struct mt7530_priv *priv)
161 + struct regmap_config *mt7531_pcs_config[2];
162 + struct phylink_pcs *pcs;
163 + struct regmap *regmap;
166 + for (i = 0; i < 2; i++) {
167 + mt7531_pcs_config[i] = devm_kzalloc(priv->dev,
168 + sizeof(struct regmap_config),
170 + if (!mt7531_pcs_config[i]) {
175 + mt7531_pcs_config[i]->name = i ? "port6" : "port5";
176 + mt7531_pcs_config[i]->reg_bits = 16;
177 + mt7531_pcs_config[i]->val_bits = 32;
178 + mt7531_pcs_config[i]->reg_stride = 4;
179 + mt7531_pcs_config[i]->reg_base = MT7531_SGMII_REG_BASE(5 + i);
180 + mt7531_pcs_config[i]->max_register = 0x17c;
181 + mt7531_pcs_config[i]->lock = mt7530_mdio_regmap_lock;
182 + mt7531_pcs_config[i]->unlock = mt7530_mdio_regmap_unlock;
183 + mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
185 + regmap = devm_regmap_init(priv->dev,
186 + &mt7530_regmap_bus, priv->bus,
187 + mt7531_pcs_config[i]);
188 + if (IS_ERR(regmap)) {
189 + ret = PTR_ERR(regmap);
192 + pcs = mtk_pcs_lynxi_create(priv->dev, regmap,
193 + MT7531_PHYA_CTRL_SIGNAL3, 0);
198 + priv->ports[5 + i].sgmii_pcs = pcs;
202 + mtk_pcs_lynxi_destroy(priv->ports[5].sgmii_pcs);
207 +static const struct of_device_id mt7530_of_match[] = {
208 + { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
209 + { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
210 + { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
211 + { /* sentinel */ },
213 +MODULE_DEVICE_TABLE(of, mt7530_of_match);
216 +mt7530_probe(struct mdio_device *mdiodev)
218 + static struct regmap_config *regmap_config;
219 + struct mt7530_priv *priv;
220 + struct device_node *dn;
223 + dn = mdiodev->dev.of_node;
225 + priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
229 + priv->bus = mdiodev->bus;
230 + priv->dev = &mdiodev->dev;
232 + ret = mt7530_probe_common(priv);
236 + /* Use medatek,mcm property to distinguish hardware type that would
237 + * cause a little bit differences on power-on sequence.
238 + * Not MCM that indicates switch works as the remote standalone
239 + * integrated circuit so the GPIO pin would be used to complete
240 + * the reset, otherwise memory-mapped register accessing used
241 + * through syscon provides in the case of MCM.
243 + priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
245 + dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
247 + priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
248 + if (IS_ERR(priv->rstc)) {
249 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
250 + return PTR_ERR(priv->rstc);
253 + priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
255 + if (IS_ERR(priv->reset)) {
256 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
257 + return PTR_ERR(priv->reset);
261 + if (priv->id == ID_MT7530) {
262 + priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
263 + if (IS_ERR(priv->core_pwr))
264 + return PTR_ERR(priv->core_pwr);
266 + priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
267 + if (IS_ERR(priv->io_pwr))
268 + return PTR_ERR(priv->io_pwr);
271 + regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
273 + if (!regmap_config)
276 + regmap_config->reg_bits = 16;
277 + regmap_config->val_bits = 32;
278 + regmap_config->reg_stride = 4;
279 + regmap_config->max_register = MT7530_CREV;
280 + regmap_config->disable_locking = true;
281 + priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
282 + priv->bus, regmap_config);
283 + if (IS_ERR(priv->regmap))
284 + return PTR_ERR(priv->regmap);
286 + if (priv->id == ID_MT7531) {
287 + ret = mt7531_create_sgmii(priv);
292 + return dsa_register_switch(priv->ds);
296 +mt7530_remove(struct mdio_device *mdiodev)
298 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
304 + ret = regulator_disable(priv->core_pwr);
307 + "Failed to disable core power: %d\n", ret);
309 + ret = regulator_disable(priv->io_pwr);
311 + dev_err(priv->dev, "Failed to disable io pwr: %d\n",
314 + mt7530_remove_common(priv);
316 + for (i = 0; i < 2; ++i)
317 + mtk_pcs_lynxi_destroy(priv->ports[5 + i].sgmii_pcs);
320 +static void mt7530_shutdown(struct mdio_device *mdiodev)
322 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
327 + dsa_switch_shutdown(priv->ds);
329 + dev_set_drvdata(&mdiodev->dev, NULL);
332 +static struct mdio_driver mt7530_mdio_driver = {
333 + .probe = mt7530_probe,
334 + .remove = mt7530_remove,
335 + .shutdown = mt7530_shutdown,
336 + .mdiodrv.driver = {
337 + .name = "mt7530-mdio",
338 + .of_match_table = mt7530_of_match,
342 +mdio_module_driver(mt7530_mdio_driver);
344 +MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
345 +MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch (MDIO)");
346 +MODULE_LICENSE("GPL");
347 --- a/drivers/net/dsa/mt7530.c
348 +++ b/drivers/net/dsa/mt7530.c
350 #include <linux/of_mdio.h>
351 #include <linux/of_net.h>
352 #include <linux/of_platform.h>
353 -#include <linux/pcs/pcs-mtk-lynxi.h>
354 #include <linux/phylink.h>
355 #include <linux/regmap.h>
356 #include <linux/regulator/consumer.h>
357 @@ -193,31 +192,6 @@ core_clear(struct mt7530_priv *priv, u32
361 -mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
363 - struct mii_bus *bus = context;
364 - u16 page, r, lo, hi;
367 - page = (reg >> 6) & 0x3ff;
368 - r = (reg >> 2) & 0xf;
372 - /* MT7530 uses 31 as the pseudo port */
373 - ret = bus->write(bus, 0x1f, 0x1f, page);
377 - ret = bus->write(bus, 0x1f, r, lo);
381 - ret = bus->write(bus, 0x1f, 0x10, hi);
386 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
389 @@ -231,29 +205,6 @@ mt7530_mii_write(struct mt7530_priv *pri
394 -mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
396 - struct mii_bus *bus = context;
397 - u16 page, r, lo, hi;
400 - page = (reg >> 6) & 0x3ff;
401 - r = (reg >> 2) & 0xf;
403 - /* MT7530 uses 31 as the pseudo port */
404 - ret = bus->write(bus, 0x1f, 0x1f, page);
408 - lo = bus->read(bus, 0x1f, r);
409 - hi = bus->read(bus, 0x1f, 0x10);
411 - *val = (hi << 16) | (lo & 0xffff);
417 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
419 @@ -3164,72 +3115,6 @@ static const struct phylink_pcs_ops mt75
420 .pcs_an_restart = mt7530_pcs_an_restart,
424 -mt7530_mdio_regmap_lock(void *mdio_lock)
426 - mutex_lock_nested(mdio_lock, MDIO_MUTEX_NESTED);
430 -mt7530_mdio_regmap_unlock(void *mdio_lock)
432 - mutex_unlock(mdio_lock);
435 -static const struct regmap_bus mt7530_regmap_bus = {
436 - .reg_write = mt7530_regmap_write,
437 - .reg_read = mt7530_regmap_read,
441 -mt7531_create_sgmii(struct mt7530_priv *priv)
443 - struct regmap_config *mt7531_pcs_config[2];
444 - struct phylink_pcs *pcs;
445 - struct regmap *regmap;
448 - for (i = 0; i < 2; i++) {
449 - mt7531_pcs_config[i] = devm_kzalloc(priv->dev,
450 - sizeof(struct regmap_config),
452 - if (!mt7531_pcs_config[i]) {
457 - mt7531_pcs_config[i]->name = i ? "port6" : "port5";
458 - mt7531_pcs_config[i]->reg_bits = 16;
459 - mt7531_pcs_config[i]->val_bits = 32;
460 - mt7531_pcs_config[i]->reg_stride = 4;
461 - mt7531_pcs_config[i]->reg_base = MT7531_SGMII_REG_BASE(5 + i);
462 - mt7531_pcs_config[i]->max_register = 0x17c;
463 - mt7531_pcs_config[i]->lock = mt7530_mdio_regmap_lock;
464 - mt7531_pcs_config[i]->unlock = mt7530_mdio_regmap_unlock;
465 - mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
467 - regmap = devm_regmap_init(priv->dev,
468 - &mt7530_regmap_bus, priv->bus,
469 - mt7531_pcs_config[i]);
470 - if (IS_ERR(regmap)) {
471 - ret = PTR_ERR(regmap);
474 - pcs = mtk_pcs_lynxi_create(priv->dev, regmap,
475 - MT7531_PHYA_CTRL_SIGNAL3, 0);
480 - priv->ports[5 + i].sgmii_pcs = pcs;
484 - mtk_pcs_lynxi_destroy(priv->ports[5].sgmii_pcs);
490 mt753x_setup(struct dsa_switch *ds)
492 @@ -3288,7 +3173,7 @@ static int mt753x_set_mac_eee(struct dsa
496 -static const struct dsa_switch_ops mt7530_switch_ops = {
497 +const struct dsa_switch_ops mt7530_switch_ops = {
498 .get_tag_protocol = mtk_get_tag_protocol,
499 .setup = mt753x_setup,
500 .get_strings = mt7530_get_strings,
501 @@ -3322,8 +3207,9 @@ static const struct dsa_switch_ops mt753
502 .get_mac_eee = mt753x_get_mac_eee,
503 .set_mac_eee = mt753x_set_mac_eee,
505 +EXPORT_SYMBOL_GPL(mt7530_switch_ops);
507 -static const struct mt753x_info mt753x_table[] = {
508 +const struct mt753x_info mt753x_table[] = {
511 .pcs_ops = &mt7530_pcs_ops,
512 @@ -3356,16 +3242,9 @@ static const struct mt753x_info mt753x_t
513 .mac_port_config = mt7531_mac_config,
516 +EXPORT_SYMBOL_GPL(mt753x_table);
518 -static const struct of_device_id mt7530_of_match[] = {
519 - { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
520 - { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
521 - { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
522 - { /* sentinel */ },
524 -MODULE_DEVICE_TABLE(of, mt7530_of_match);
528 mt7530_probe_common(struct mt7530_priv *priv)
530 struct device *dev = priv->dev;
531 @@ -3402,88 +3281,9 @@ mt7530_probe_common(struct mt7530_priv *
535 +EXPORT_SYMBOL_GPL(mt7530_probe_common);
538 -mt7530_probe(struct mdio_device *mdiodev)
540 - static struct regmap_config *regmap_config;
541 - struct mt7530_priv *priv;
542 - struct device_node *dn;
545 - dn = mdiodev->dev.of_node;
547 - priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
551 - priv->bus = mdiodev->bus;
552 - priv->dev = &mdiodev->dev;
554 - ret = mt7530_probe_common(priv);
558 - /* Use medatek,mcm property to distinguish hardware type that would
559 - * cause a little bit differences on power-on sequence.
560 - * Not MCM that indicates switch works as the remote standalone
561 - * integrated circuit so the GPIO pin would be used to complete
562 - * the reset, otherwise memory-mapped register accessing used
563 - * through syscon provides in the case of MCM.
565 - priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
567 - dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
569 - priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
570 - if (IS_ERR(priv->rstc)) {
571 - dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
572 - return PTR_ERR(priv->rstc);
575 - priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
577 - if (IS_ERR(priv->reset)) {
578 - dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
579 - return PTR_ERR(priv->reset);
583 - if (priv->id == ID_MT7530) {
584 - priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
585 - if (IS_ERR(priv->core_pwr))
586 - return PTR_ERR(priv->core_pwr);
588 - priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
589 - if (IS_ERR(priv->io_pwr))
590 - return PTR_ERR(priv->io_pwr);
593 - regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
595 - if (!regmap_config)
598 - regmap_config->reg_bits = 16;
599 - regmap_config->val_bits = 32;
600 - regmap_config->reg_stride = 4;
601 - regmap_config->max_register = MT7530_CREV;
602 - regmap_config->disable_locking = true;
603 - priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
604 - priv->bus, regmap_config);
605 - if (IS_ERR(priv->regmap))
606 - return PTR_ERR(priv->regmap);
608 - if (priv->id == ID_MT7531) {
609 - ret = mt7531_create_sgmii(priv);
614 - return dsa_register_switch(priv->ds);
619 mt7530_remove_common(struct mt7530_priv *priv)
622 @@ -3494,57 +3294,6 @@ mt7530_remove_common(struct mt7530_priv
623 mutex_destroy(&priv->reg_mutex);
627 -mt7530_remove(struct mdio_device *mdiodev)
629 - struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
635 - ret = regulator_disable(priv->core_pwr);
638 - "Failed to disable core power: %d\n", ret);
640 - ret = regulator_disable(priv->io_pwr);
642 - dev_err(priv->dev, "Failed to disable io pwr: %d\n",
645 - mt7530_remove_common(priv);
647 - for (i = 0; i < 2; ++i)
648 - mtk_pcs_lynxi_destroy(priv->ports[5 + i].sgmii_pcs);
650 - dev_set_drvdata(&mdiodev->dev, NULL);
653 -static void mt7530_shutdown(struct mdio_device *mdiodev)
655 - struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
660 - dsa_switch_shutdown(priv->ds);
662 - dev_set_drvdata(&mdiodev->dev, NULL);
665 -static struct mdio_driver mt7530_mdio_driver = {
666 - .probe = mt7530_probe,
667 - .remove = mt7530_remove,
668 - .shutdown = mt7530_shutdown,
669 - .mdiodrv.driver = {
671 - .of_match_table = mt7530_of_match,
675 -mdio_module_driver(mt7530_mdio_driver);
677 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
678 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
679 MODULE_LICENSE("GPL");
680 --- a/drivers/net/dsa/mt7530.h
681 +++ b/drivers/net/dsa/mt7530.h
682 @@ -839,4 +839,10 @@ static inline void INIT_MT7530_DUMMY_POL
686 +int mt7530_probe_common(struct mt7530_priv *priv);
687 +void mt7530_remove_common(struct mt7530_priv *priv);
689 +extern const struct dsa_switch_ops mt7530_switch_ops;
690 +extern const struct mt753x_info mt753x_table[];
692 #endif /* __MT7530_H */