2 * Mediatek MT7530 DSA Switch driver
3 * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 #include <linux/etherdevice.h>
15 #include <linux/if_bridge.h>
16 #include <linux/iopoll.h>
17 #include <linux/mdio.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_mdio.h>
23 #include <linux/of_net.h>
24 #include <linux/of_platform.h>
25 #include <linux/phy.h>
26 #include <linux/regmap.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/reset.h>
29 #include <linux/gpio/consumer.h>
34 /* String, offset, and register size in bytes if different from 4 bytes */
35 static const struct mt7530_mib_desc mt7530_mib[] = {
36 MIB_DESC(1, 0x00, "TxDrop"),
37 MIB_DESC(1, 0x04, "TxCrcErr"),
38 MIB_DESC(1, 0x08, "TxUnicast"),
39 MIB_DESC(1, 0x0c, "TxMulticast"),
40 MIB_DESC(1, 0x10, "TxBroadcast"),
41 MIB_DESC(1, 0x14, "TxCollision"),
42 MIB_DESC(1, 0x18, "TxSingleCollision"),
43 MIB_DESC(1, 0x1c, "TxMultipleCollision"),
44 MIB_DESC(1, 0x20, "TxDeferred"),
45 MIB_DESC(1, 0x24, "TxLateCollision"),
46 MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
47 MIB_DESC(1, 0x2c, "TxPause"),
48 MIB_DESC(1, 0x30, "TxPktSz64"),
49 MIB_DESC(1, 0x34, "TxPktSz65To127"),
50 MIB_DESC(1, 0x38, "TxPktSz128To255"),
51 MIB_DESC(1, 0x3c, "TxPktSz256To511"),
52 MIB_DESC(1, 0x40, "TxPktSz512To1023"),
53 MIB_DESC(1, 0x44, "Tx1024ToMax"),
54 MIB_DESC(2, 0x48, "TxBytes"),
55 MIB_DESC(1, 0x60, "RxDrop"),
56 MIB_DESC(1, 0x64, "RxFiltering"),
57 MIB_DESC(1, 0x6c, "RxMulticast"),
58 MIB_DESC(1, 0x70, "RxBroadcast"),
59 MIB_DESC(1, 0x74, "RxAlignErr"),
60 MIB_DESC(1, 0x78, "RxCrcErr"),
61 MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
62 MIB_DESC(1, 0x80, "RxFragErr"),
63 MIB_DESC(1, 0x84, "RxOverSzErr"),
64 MIB_DESC(1, 0x88, "RxJabberErr"),
65 MIB_DESC(1, 0x8c, "RxPause"),
66 MIB_DESC(1, 0x90, "RxPktSz64"),
67 MIB_DESC(1, 0x94, "RxPktSz65To127"),
68 MIB_DESC(1, 0x98, "RxPktSz128To255"),
69 MIB_DESC(1, 0x9c, "RxPktSz256To511"),
70 MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
71 MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
72 MIB_DESC(2, 0xa8, "RxBytes"),
73 MIB_DESC(1, 0xb0, "RxCtrlDrop"),
74 MIB_DESC(1, 0xb4, "RxIngressDrop"),
75 MIB_DESC(1, 0xb8, "RxArlDrop"),
79 mt7623_trgmii_write(struct mt7530_priv *priv, u32 reg, u32 val)
83 ret = regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
86 "failed to priv write register\n");
91 mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
96 ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
99 "failed to priv read register\n");
107 mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
112 val = mt7623_trgmii_read(priv, reg);
115 mt7623_trgmii_write(priv, reg, val);
119 mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
121 mt7623_trgmii_rmw(priv, reg, 0, val);
125 mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
127 mt7623_trgmii_rmw(priv, reg, val, 0);
131 core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
133 struct mii_bus *bus = priv->bus;
136 /* Write the desired MMD Devad */
137 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
141 /* Write the desired MMD register address */
142 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
146 /* Select the Function : DATA with no post increment */
147 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
151 /* Read the content of the MMD's selected register */
152 value = bus->read(bus, 0, MII_MMD_DATA);
156 dev_err(&bus->dev, "failed to read mmd register\n");
162 core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
165 struct mii_bus *bus = priv->bus;
168 /* Write the desired MMD Devad */
169 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
173 /* Write the desired MMD register address */
174 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
178 /* Select the Function : DATA with no post increment */
179 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
183 /* Write the data into MMD's selected register */
184 ret = bus->write(bus, 0, MII_MMD_DATA, data);
188 "failed to write mmd register\n");
193 core_write(struct mt7530_priv *priv, u32 reg, u32 val)
195 struct mii_bus *bus = priv->bus;
197 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
199 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
201 mutex_unlock(&bus->mdio_lock);
205 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
207 struct mii_bus *bus = priv->bus;
210 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
212 val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
215 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
217 mutex_unlock(&bus->mdio_lock);
221 core_set(struct mt7530_priv *priv, u32 reg, u32 val)
223 core_rmw(priv, reg, 0, val);
227 core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
229 core_rmw(priv, reg, val, 0);
233 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
235 struct mii_bus *bus = priv->bus;
239 page = (reg >> 6) & 0x3ff;
240 r = (reg >> 2) & 0xf;
244 /* MT7530 uses 31 as the pseudo port */
245 ret = bus->write(bus, 0x1f, 0x1f, page);
249 ret = bus->write(bus, 0x1f, r, lo);
253 ret = bus->write(bus, 0x1f, 0x10, hi);
257 "failed to write mt7530 register\n");
262 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
264 struct mii_bus *bus = priv->bus;
268 page = (reg >> 6) & 0x3ff;
269 r = (reg >> 2) & 0xf;
271 /* MT7530 uses 31 as the pseudo port */
272 ret = bus->write(bus, 0x1f, 0x1f, page);
275 "failed to read mt7530 register\n");
279 lo = bus->read(bus, 0x1f, r);
280 hi = bus->read(bus, 0x1f, 0x10);
282 return (hi << 16) | (lo & 0xffff);
286 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
288 struct mii_bus *bus = priv->bus;
290 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
292 mt7530_mii_write(priv, reg, val);
294 mutex_unlock(&bus->mdio_lock);
298 _mt7530_read(struct mt7530_dummy_poll *p)
300 struct mii_bus *bus = p->priv->bus;
303 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
305 val = mt7530_mii_read(p->priv, p->reg);
307 mutex_unlock(&bus->mdio_lock);
313 mt7530_read(struct mt7530_priv *priv, u32 reg)
315 struct mt7530_dummy_poll p;
317 INIT_MT7530_DUMMY_POLL(&p, priv, reg);
318 return _mt7530_read(&p);
322 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
325 struct mii_bus *bus = priv->bus;
328 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
330 val = mt7530_mii_read(priv, reg);
333 mt7530_mii_write(priv, reg, val);
335 mutex_unlock(&bus->mdio_lock);
339 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
341 mt7530_rmw(priv, reg, 0, val);
345 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
347 mt7530_rmw(priv, reg, val, 0);
351 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
355 struct mt7530_dummy_poll p;
357 /* Set the command operating upon the MAC address entries */
358 val = ATC_BUSY | ATC_MAT(0) | cmd;
359 mt7530_write(priv, MT7530_ATC, val);
361 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
362 ret = readx_poll_timeout(_mt7530_read, &p, val,
363 !(val & ATC_BUSY), 20, 20000);
365 dev_err(priv->dev, "reset timeout\n");
369 /* Additional sanity for read command if the specified
372 val = mt7530_read(priv, MT7530_ATC);
373 if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
383 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
388 /* Read from ARL table into an array */
389 for (i = 0; i < 3; i++) {
390 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
392 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
393 __func__, __LINE__, i, reg[i]);
396 fdb->vid = (reg[1] >> CVID) & CVID_MASK;
397 fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
398 fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
399 fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
400 fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
401 fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
402 fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
403 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
404 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
405 fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
409 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
410 u8 port_mask, const u8 *mac,
416 reg[1] |= vid & CVID_MASK;
417 reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
418 reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
419 /* STATIC_ENT indicate that entry is static wouldn't
420 * be aged out and STATIC_EMP specified as erasing an
423 reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
424 reg[1] |= mac[5] << MAC_BYTE_5;
425 reg[1] |= mac[4] << MAC_BYTE_4;
426 reg[0] |= mac[3] << MAC_BYTE_3;
427 reg[0] |= mac[2] << MAC_BYTE_2;
428 reg[0] |= mac[1] << MAC_BYTE_1;
429 reg[0] |= mac[0] << MAC_BYTE_0;
431 /* Write array into the ARL table */
432 for (i = 0; i < 3; i++)
433 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
437 mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
439 struct mt7530_priv *priv = ds->priv;
440 u32 ncpo1, ssc_delta, trgint, i;
443 case PHY_INTERFACE_MODE_RGMII:
448 case PHY_INTERFACE_MODE_TRGMII:
454 dev_err(priv->dev, "xMII mode %d not supported\n", mode);
458 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
459 P6_INTF_MODE(trgint));
461 /* Lower Tx Driving for TRGMII path */
462 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
463 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
464 TD_DM_DRVP(8) | TD_DM_DRVN(8));
466 /* Setup core clock for MT7530 */
468 /* Disable MT7530 core clock */
469 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
471 /* Disable PLL, since phy_device has not yet been created
472 * provided for phy_[read,write]_mmd_indirect is called, we
473 * provide our own core_write_mmd_indirect to complete this
476 core_write_mmd_indirect(priv,
481 /* Set core clock into 500Mhz */
482 core_write(priv, CORE_GSWPLL_GRP2,
483 RG_GSWPLL_POSDIV_500M(1) |
484 RG_GSWPLL_FBKDIV_500M(25));
487 core_write(priv, CORE_GSWPLL_GRP1,
489 RG_GSWPLL_POSDIV_200M(2) |
490 RG_GSWPLL_FBKDIV_200M(32));
492 /* Enable MT7530 core clock */
493 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
496 /* Setup the MT7530 TRGMII Tx Clock */
497 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
498 core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
499 core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
500 core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
501 core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
502 core_write(priv, CORE_PLL_GROUP4,
503 RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
504 RG_SYSPLL_BIAS_LPF_EN);
505 core_write(priv, CORE_PLL_GROUP2,
506 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
507 RG_SYSPLL_POSDIV(1));
508 core_write(priv, CORE_PLL_GROUP7,
509 RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
510 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
511 core_set(priv, CORE_TRGMII_GSW_CLK_CG,
512 REG_GSWCK_EN | REG_TRGMIICK_EN);
515 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
516 mt7530_rmw(priv, MT7530_TRGMII_RD(i),
517 RD_TAP_MASK, RD_TAP(16));
519 mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
525 mt7623_pad_clk_setup(struct dsa_switch *ds)
527 struct mt7530_priv *priv = ds->priv;
530 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
531 mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
532 TD_DM_DRVP(8) | TD_DM_DRVN(8));
534 mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
535 mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
541 mt7530_mib_reset(struct dsa_switch *ds)
543 struct mt7530_priv *priv = ds->priv;
545 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
546 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
550 mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
552 u32 mask = PMCR_TX_EN | PMCR_RX_EN;
555 mt7530_set(priv, MT7530_PMCR_P(port), mask);
557 mt7530_clear(priv, MT7530_PMCR_P(port), mask);
560 static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
562 struct mt7530_priv *priv = ds->priv;
564 return mdiobus_read_nested(priv->bus, port, regnum);
567 static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
570 struct mt7530_priv *priv = ds->priv;
572 return mdiobus_write_nested(priv->bus, port, regnum, val);
576 mt7530_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
580 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
581 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
586 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
589 struct mt7530_priv *priv = ds->priv;
590 const struct mt7530_mib_desc *mib;
594 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
595 mib = &mt7530_mib[i];
596 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
598 data[i] = mt7530_read(priv, reg);
599 if (mib->size == 2) {
600 hi = mt7530_read(priv, reg + 4);
607 mt7530_get_sset_count(struct dsa_switch *ds, int port)
609 return ARRAY_SIZE(mt7530_mib);
612 static void mt7530_adjust_link(struct dsa_switch *ds, int port,
613 struct phy_device *phydev)
615 struct mt7530_priv *priv = ds->priv;
617 if (phy_is_pseudo_fixed_link(phydev)) {
618 dev_dbg(priv->dev, "phy-mode for master device = %x\n",
621 /* Setup TX circuit incluing relevant PAD and driving */
622 mt7530_pad_clk_setup(ds, phydev->interface);
624 /* Setup RX circuit, relevant PAD and driving on the host
625 * which must be placed after the setup on the device side is
628 mt7623_pad_clk_setup(ds);
630 u16 lcl_adv = 0, rmt_adv = 0;
632 u32 mcr = PMCR_USERP_LINK | PMCR_FORCE_MODE;
634 switch (phydev->speed) {
636 mcr |= PMCR_FORCE_SPEED_1000;
639 mcr |= PMCR_FORCE_SPEED_100;
644 mcr |= PMCR_FORCE_LNK;
646 if (phydev->duplex) {
647 mcr |= PMCR_FORCE_FDX;
650 rmt_adv = LPA_PAUSE_CAP;
651 if (phydev->asym_pause)
652 rmt_adv |= LPA_PAUSE_ASYM;
654 if (phydev->advertising & ADVERTISED_Pause)
655 lcl_adv |= ADVERTISE_PAUSE_CAP;
656 if (phydev->advertising & ADVERTISED_Asym_Pause)
657 lcl_adv |= ADVERTISE_PAUSE_ASYM;
659 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
661 if (flowctrl & FLOW_CTRL_TX)
662 mcr |= PMCR_TX_FC_EN;
663 if (flowctrl & FLOW_CTRL_RX)
664 mcr |= PMCR_RX_FC_EN;
666 mt7530_write(priv, MT7530_PMCR_P(port), mcr);
671 mt7530_cpu_port_enable(struct mt7530_priv *priv,
674 /* Enable Mediatek header mode on the cpu port */
675 mt7530_write(priv, MT7530_PVC_P(port),
678 /* Setup the MAC by default for the cpu port */
679 mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
681 /* Disable auto learning on the cpu port */
682 mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
684 /* Unknown unicast frame fordwarding to the cpu port */
685 mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
687 /* CPU port gets connected to all user ports of
690 mt7530_write(priv, MT7530_PCR_P(port),
691 PCR_MATRIX(dsa_user_ports(priv->ds)));
697 mt7530_port_enable(struct dsa_switch *ds, int port,
698 struct phy_device *phy)
700 struct mt7530_priv *priv = ds->priv;
702 mutex_lock(&priv->reg_mutex);
704 /* Setup the MAC for the user port */
705 mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
707 /* Allow the user port gets connected to the cpu port and also
708 * restore the port matrix if the port is the member of a certain
711 priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
712 priv->ports[port].enable = true;
713 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
714 priv->ports[port].pm);
715 mt7530_port_set_status(priv, port, 1);
717 mutex_unlock(&priv->reg_mutex);
723 mt7530_port_disable(struct dsa_switch *ds, int port,
724 struct phy_device *phy)
726 struct mt7530_priv *priv = ds->priv;
728 mutex_lock(&priv->reg_mutex);
730 /* Clear up all port matrix which could be restored in the next
731 * enablement for the port.
733 priv->ports[port].enable = false;
734 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
736 mt7530_port_set_status(priv, port, 0);
738 mutex_unlock(&priv->reg_mutex);
742 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
744 struct mt7530_priv *priv = ds->priv;
748 case BR_STATE_DISABLED:
749 stp_state = MT7530_STP_DISABLED;
751 case BR_STATE_BLOCKING:
752 stp_state = MT7530_STP_BLOCKING;
754 case BR_STATE_LISTENING:
755 stp_state = MT7530_STP_LISTENING;
757 case BR_STATE_LEARNING:
758 stp_state = MT7530_STP_LEARNING;
760 case BR_STATE_FORWARDING:
762 stp_state = MT7530_STP_FORWARDING;
766 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
770 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
771 struct net_device *bridge)
773 struct mt7530_priv *priv = ds->priv;
774 u32 port_bitmap = BIT(MT7530_CPU_PORT);
777 mutex_lock(&priv->reg_mutex);
779 for (i = 0; i < MT7530_NUM_PORTS; i++) {
780 /* Add this port to the port matrix of the other ports in the
781 * same bridge. If the port is disabled, port matrix is kept
782 * and not being setup until the port becomes enabled.
784 if (dsa_is_user_port(ds, i) && i != port) {
785 if (dsa_to_port(ds, i)->bridge_dev != bridge)
787 if (priv->ports[i].enable)
788 mt7530_set(priv, MT7530_PCR_P(i),
789 PCR_MATRIX(BIT(port)));
790 priv->ports[i].pm |= PCR_MATRIX(BIT(port));
792 port_bitmap |= BIT(i);
796 /* Add the all other ports to this port matrix. */
797 if (priv->ports[port].enable)
798 mt7530_rmw(priv, MT7530_PCR_P(port),
799 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
800 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
802 mutex_unlock(&priv->reg_mutex);
808 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
810 struct mt7530_priv *priv = ds->priv;
811 bool all_user_ports_removed = true;
814 /* When a port is removed from the bridge, the port would be set up
815 * back to the default as is at initial boot which is a VLAN-unaware
818 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
819 MT7530_PORT_MATRIX_MODE);
820 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
821 VLAN_ATTR(MT7530_VLAN_TRANSPARENT));
823 priv->ports[port].vlan_filtering = false;
825 for (i = 0; i < MT7530_NUM_PORTS; i++) {
826 if (dsa_is_user_port(ds, i) &&
827 priv->ports[i].vlan_filtering) {
828 all_user_ports_removed = false;
833 /* CPU port also does the same thing until all user ports belonging to
834 * the CPU port get out of VLAN filtering mode.
836 if (all_user_ports_removed) {
837 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
838 PCR_MATRIX(dsa_user_ports(priv->ds)));
839 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT),
845 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
847 struct mt7530_priv *priv = ds->priv;
849 /* The real fabric path would be decided on the membership in the
850 * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS
851 * means potential VLAN can be consisting of certain subset of all
854 mt7530_rmw(priv, MT7530_PCR_P(port),
855 PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS));
857 /* Trapped into security mode allows packet forwarding through VLAN
860 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
861 MT7530_PORT_SECURITY_MODE);
863 /* Set the port as a user port which is to be able to recognize VID
864 * from incoming packets before fetching entry within the VLAN table.
866 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
867 VLAN_ATTR(MT7530_VLAN_USER));
871 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
872 struct net_device *bridge)
874 struct mt7530_priv *priv = ds->priv;
877 mutex_lock(&priv->reg_mutex);
879 for (i = 0; i < MT7530_NUM_PORTS; i++) {
880 /* Remove this port from the port matrix of the other ports
881 * in the same bridge. If the port is disabled, port matrix
882 * is kept and not being setup until the port becomes enabled.
883 * And the other port's port matrix cannot be broken when the
884 * other port is still a VLAN-aware port.
886 if (!priv->ports[i].vlan_filtering &&
887 dsa_is_user_port(ds, i) && i != port) {
888 if (dsa_to_port(ds, i)->bridge_dev != bridge)
890 if (priv->ports[i].enable)
891 mt7530_clear(priv, MT7530_PCR_P(i),
892 PCR_MATRIX(BIT(port)));
893 priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
897 /* Set the cpu port to be the only one in the port matrix of
900 if (priv->ports[port].enable)
901 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
902 PCR_MATRIX(BIT(MT7530_CPU_PORT)));
903 priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
905 mt7530_port_set_vlan_unaware(ds, port);
907 mutex_unlock(&priv->reg_mutex);
911 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
912 const unsigned char *addr, u16 vid)
914 struct mt7530_priv *priv = ds->priv;
916 u8 port_mask = BIT(port);
918 mutex_lock(&priv->reg_mutex);
919 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
920 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
921 mutex_unlock(&priv->reg_mutex);
927 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
928 const unsigned char *addr, u16 vid)
930 struct mt7530_priv *priv = ds->priv;
932 u8 port_mask = BIT(port);
934 mutex_lock(&priv->reg_mutex);
935 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
936 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
937 mutex_unlock(&priv->reg_mutex);
943 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
944 dsa_fdb_dump_cb_t *cb, void *data)
946 struct mt7530_priv *priv = ds->priv;
947 struct mt7530_fdb _fdb = { 0 };
948 int cnt = MT7530_NUM_FDB_RECORDS;
952 mutex_lock(&priv->reg_mutex);
954 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
959 if (rsp & ATC_SRCH_HIT) {
960 mt7530_fdb_read(priv, &_fdb);
961 if (_fdb.port_mask & BIT(port)) {
962 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
969 !(rsp & ATC_SRCH_END) &&
970 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
972 mutex_unlock(&priv->reg_mutex);
978 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
980 struct mt7530_dummy_poll p;
984 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
985 mt7530_write(priv, MT7530_VTCR, val);
987 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
988 ret = readx_poll_timeout(_mt7530_read, &p, val,
989 !(val & VTCR_BUSY), 20, 20000);
991 dev_err(priv->dev, "poll timeout\n");
995 val = mt7530_read(priv, MT7530_VTCR);
996 if (val & VTCR_INVALID) {
997 dev_err(priv->dev, "read VTCR invalid\n");
1005 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
1006 bool vlan_filtering)
1008 struct mt7530_priv *priv = ds->priv;
1010 priv->ports[port].vlan_filtering = vlan_filtering;
1012 if (vlan_filtering) {
1013 /* The port is being kept as VLAN-unaware port when bridge is
1014 * set up with vlan_filtering not being set, Otherwise, the
1015 * port and the corresponding CPU port is required the setup
1016 * for becoming a VLAN-aware port.
1018 mt7530_port_set_vlan_aware(ds, port);
1019 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
1026 mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
1027 const struct switchdev_obj_port_vlan *vlan)
1029 /* nothing needed */
1035 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1036 struct mt7530_hw_vlan_entry *entry)
1041 new_members = entry->old_members | BIT(entry->port) |
1042 BIT(MT7530_CPU_PORT);
1044 /* Validate the entry with independent learning, create egress tag per
1045 * VLAN and joining the port as one of the port members.
1047 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
1048 mt7530_write(priv, MT7530_VAWD1, val);
1050 /* Decide whether adding tag or not for those outgoing packets from the
1051 * port inside the VLAN.
1053 val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1054 MT7530_VLAN_EGRESS_TAG;
1055 mt7530_rmw(priv, MT7530_VAWD2,
1056 ETAG_CTRL_P_MASK(entry->port),
1057 ETAG_CTRL_P(entry->port, val));
1059 /* CPU port is always taken as a tagged port for serving more than one
1060 * VLANs across and also being applied with egress type stack mode for
1061 * that VLAN tags would be appended after hardware special tag used as
1064 mt7530_rmw(priv, MT7530_VAWD2,
1065 ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1066 ETAG_CTRL_P(MT7530_CPU_PORT,
1067 MT7530_VLAN_EGRESS_STACK));
1071 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1072 struct mt7530_hw_vlan_entry *entry)
1077 new_members = entry->old_members & ~BIT(entry->port);
1079 val = mt7530_read(priv, MT7530_VAWD1);
1080 if (!(val & VLAN_VALID)) {
1082 "Cannot be deleted due to invalid entry\n");
1086 /* If certain member apart from CPU port is still alive in the VLAN,
1087 * the entry would be kept valid. Otherwise, the entry is got to be
1090 if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1091 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1093 mt7530_write(priv, MT7530_VAWD1, val);
1095 mt7530_write(priv, MT7530_VAWD1, 0);
1096 mt7530_write(priv, MT7530_VAWD2, 0);
1101 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1102 struct mt7530_hw_vlan_entry *entry,
1103 mt7530_vlan_op vlan_op)
1108 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1110 val = mt7530_read(priv, MT7530_VAWD1);
1112 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1114 /* Manipulate entry */
1115 vlan_op(priv, entry);
1117 /* Flush result to hardware */
1118 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1122 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1123 const struct switchdev_obj_port_vlan *vlan)
1125 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1126 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1127 struct mt7530_hw_vlan_entry new_entry;
1128 struct mt7530_priv *priv = ds->priv;
1131 /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1134 if (!priv->ports[port].vlan_filtering)
1137 mutex_lock(&priv->reg_mutex);
1139 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1140 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1141 mt7530_hw_vlan_update(priv, vid, &new_entry,
1142 mt7530_hw_vlan_add);
1146 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1147 G0_PORT_VID(vlan->vid_end));
1148 priv->ports[port].pvid = vlan->vid_end;
1151 mutex_unlock(&priv->reg_mutex);
1155 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1156 const struct switchdev_obj_port_vlan *vlan)
1158 struct mt7530_hw_vlan_entry target_entry;
1159 struct mt7530_priv *priv = ds->priv;
1162 /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1165 if (!priv->ports[port].vlan_filtering)
1168 mutex_lock(&priv->reg_mutex);
1170 pvid = priv->ports[port].pvid;
1171 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1172 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1173 mt7530_hw_vlan_update(priv, vid, &target_entry,
1174 mt7530_hw_vlan_del);
1176 /* PVID is being restored to the default whenever the PVID port
1177 * is being removed from the VLAN.
1180 pvid = G0_PORT_VID_DEF;
1183 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1184 priv->ports[port].pvid = pvid;
1186 mutex_unlock(&priv->reg_mutex);
1191 static enum dsa_tag_protocol
1192 mtk_get_tag_protocol(struct dsa_switch *ds, int port)
1194 struct mt7530_priv *priv = ds->priv;
1196 if (port != MT7530_CPU_PORT) {
1198 "port not matched with tagging CPU port\n");
1199 return DSA_TAG_PROTO_NONE;
1201 return DSA_TAG_PROTO_MTK;
1206 mt7530_setup(struct dsa_switch *ds)
1208 struct mt7530_priv *priv = ds->priv;
1211 struct device_node *dn;
1212 struct mt7530_dummy_poll p;
1214 /* The parent node of master netdev which holds the common system
1215 * controller also is the container for two GMACs nodes representing
1216 * as two netdev instances.
1218 dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent;
1219 priv->ethernet = syscon_node_to_regmap(dn);
1220 if (IS_ERR(priv->ethernet))
1221 return PTR_ERR(priv->ethernet);
1223 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1224 ret = regulator_enable(priv->core_pwr);
1227 "Failed to enable core power: %d\n", ret);
1231 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1232 ret = regulator_enable(priv->io_pwr);
1234 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1239 /* Reset whole chip through gpio pin or memory-mapped registers for
1240 * different type of hardware
1243 reset_control_assert(priv->rstc);
1244 usleep_range(1000, 1100);
1245 reset_control_deassert(priv->rstc);
1247 gpiod_set_value_cansleep(priv->reset, 0);
1248 usleep_range(1000, 1100);
1249 gpiod_set_value_cansleep(priv->reset, 1);
1252 /* Waiting for MT7530 got to stable */
1253 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1254 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1257 dev_err(priv->dev, "reset timeout\n");
1261 id = mt7530_read(priv, MT7530_CREV);
1262 id >>= CHIP_NAME_SHIFT;
1263 if (id != MT7530_ID) {
1264 dev_err(priv->dev, "chip %x can't be supported\n", id);
1268 /* Reset the switch through internal reset */
1269 mt7530_write(priv, MT7530_SYS_CTRL,
1270 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1273 /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1274 val = mt7530_read(priv, MT7530_MHWTRAP);
1275 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1276 val |= MHWTRAP_MANUAL;
1277 mt7530_write(priv, MT7530_MHWTRAP, val);
1279 /* Enable and reset MIB counters */
1280 mt7530_mib_reset(ds);
1282 mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
1284 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1285 /* Disable forwarding by default on all ports */
1286 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1289 if (dsa_is_cpu_port(ds, i))
1290 mt7530_cpu_port_enable(priv, i);
1292 mt7530_port_disable(ds, i, NULL);
1295 /* Flush the FDB table */
1296 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
1303 static const struct dsa_switch_ops mt7530_switch_ops = {
1304 .get_tag_protocol = mtk_get_tag_protocol,
1305 .setup = mt7530_setup,
1306 .get_strings = mt7530_get_strings,
1307 .phy_read = mt7530_phy_read,
1308 .phy_write = mt7530_phy_write,
1309 .get_ethtool_stats = mt7530_get_ethtool_stats,
1310 .get_sset_count = mt7530_get_sset_count,
1311 .adjust_link = mt7530_adjust_link,
1312 .port_enable = mt7530_port_enable,
1313 .port_disable = mt7530_port_disable,
1314 .port_stp_state_set = mt7530_stp_state_set,
1315 .port_bridge_join = mt7530_port_bridge_join,
1316 .port_bridge_leave = mt7530_port_bridge_leave,
1317 .port_fdb_add = mt7530_port_fdb_add,
1318 .port_fdb_del = mt7530_port_fdb_del,
1319 .port_fdb_dump = mt7530_port_fdb_dump,
1320 .port_vlan_filtering = mt7530_port_vlan_filtering,
1321 .port_vlan_prepare = mt7530_port_vlan_prepare,
1322 .port_vlan_add = mt7530_port_vlan_add,
1323 .port_vlan_del = mt7530_port_vlan_del,
1327 mt7530_probe(struct mdio_device *mdiodev)
1329 struct mt7530_priv *priv;
1330 struct device_node *dn;
1332 dn = mdiodev->dev.of_node;
1334 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1338 priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
1342 /* Use medatek,mcm property to distinguish hardware type that would
1343 * casues a little bit differences on power-on sequence.
1345 priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1347 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1349 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1350 if (IS_ERR(priv->rstc)) {
1351 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1352 return PTR_ERR(priv->rstc);
1356 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1357 if (IS_ERR(priv->core_pwr))
1358 return PTR_ERR(priv->core_pwr);
1360 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1361 if (IS_ERR(priv->io_pwr))
1362 return PTR_ERR(priv->io_pwr);
1364 /* Not MCM that indicates switch works as the remote standalone
1365 * integrated circuit so the GPIO pin would be used to complete
1366 * the reset, otherwise memory-mapped register accessing used
1367 * through syscon provides in the case of MCM.
1370 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1372 if (IS_ERR(priv->reset)) {
1373 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1374 return PTR_ERR(priv->reset);
1378 priv->bus = mdiodev->bus;
1379 priv->dev = &mdiodev->dev;
1380 priv->ds->priv = priv;
1381 priv->ds->ops = &mt7530_switch_ops;
1382 mutex_init(&priv->reg_mutex);
1383 dev_set_drvdata(&mdiodev->dev, priv);
1385 return dsa_register_switch(priv->ds);
1389 mt7530_remove(struct mdio_device *mdiodev)
1391 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1394 ret = regulator_disable(priv->core_pwr);
1397 "Failed to disable core power: %d\n", ret);
1399 ret = regulator_disable(priv->io_pwr);
1401 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1404 dsa_unregister_switch(priv->ds);
1405 mutex_destroy(&priv->reg_mutex);
1408 static const struct of_device_id mt7530_of_match[] = {
1409 { .compatible = "mediatek,mt7530" },
1413 static struct mdio_driver mt7530_mdio_driver = {
1414 .probe = mt7530_probe,
1415 .remove = mt7530_remove,
1418 .of_match_table = mt7530_of_match,
1422 mdio_module_driver(mt7530_mdio_driver);
1424 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1425 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1426 MODULE_LICENSE("GPL");
1427 MODULE_ALIAS("platform:mediatek-mt7530");