1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mediatek MT7530 DSA Switch driver
6 #include <linux/etherdevice.h>
7 #include <linux/if_bridge.h>
8 #include <linux/iopoll.h>
9 #include <linux/mdio.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_mdio.h>
15 #include <linux/of_net.h>
16 #include <linux/of_platform.h>
17 #include <linux/phylink.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/reset.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/driver.h>
27 static struct mt753x_pcs *pcs_to_mt753x_pcs(struct phylink_pcs *pcs)
29 return container_of(pcs, struct mt753x_pcs, pcs);
32 /* String, offset, and register size in bytes if different from 4 bytes */
33 static const struct mt7530_mib_desc mt7530_mib[] = {
34 MIB_DESC(1, 0x00, "TxDrop"),
35 MIB_DESC(1, 0x04, "TxCrcErr"),
36 MIB_DESC(1, 0x08, "TxUnicast"),
37 MIB_DESC(1, 0x0c, "TxMulticast"),
38 MIB_DESC(1, 0x10, "TxBroadcast"),
39 MIB_DESC(1, 0x14, "TxCollision"),
40 MIB_DESC(1, 0x18, "TxSingleCollision"),
41 MIB_DESC(1, 0x1c, "TxMultipleCollision"),
42 MIB_DESC(1, 0x20, "TxDeferred"),
43 MIB_DESC(1, 0x24, "TxLateCollision"),
44 MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
45 MIB_DESC(1, 0x2c, "TxPause"),
46 MIB_DESC(1, 0x30, "TxPktSz64"),
47 MIB_DESC(1, 0x34, "TxPktSz65To127"),
48 MIB_DESC(1, 0x38, "TxPktSz128To255"),
49 MIB_DESC(1, 0x3c, "TxPktSz256To511"),
50 MIB_DESC(1, 0x40, "TxPktSz512To1023"),
51 MIB_DESC(1, 0x44, "Tx1024ToMax"),
52 MIB_DESC(2, 0x48, "TxBytes"),
53 MIB_DESC(1, 0x60, "RxDrop"),
54 MIB_DESC(1, 0x64, "RxFiltering"),
55 MIB_DESC(1, 0x68, "RxUnicast"),
56 MIB_DESC(1, 0x6c, "RxMulticast"),
57 MIB_DESC(1, 0x70, "RxBroadcast"),
58 MIB_DESC(1, 0x74, "RxAlignErr"),
59 MIB_DESC(1, 0x78, "RxCrcErr"),
60 MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
61 MIB_DESC(1, 0x80, "RxFragErr"),
62 MIB_DESC(1, 0x84, "RxOverSzErr"),
63 MIB_DESC(1, 0x88, "RxJabberErr"),
64 MIB_DESC(1, 0x8c, "RxPause"),
65 MIB_DESC(1, 0x90, "RxPktSz64"),
66 MIB_DESC(1, 0x94, "RxPktSz65To127"),
67 MIB_DESC(1, 0x98, "RxPktSz128To255"),
68 MIB_DESC(1, 0x9c, "RxPktSz256To511"),
69 MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
70 MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
71 MIB_DESC(2, 0xa8, "RxBytes"),
72 MIB_DESC(1, 0xb0, "RxCtrlDrop"),
73 MIB_DESC(1, 0xb4, "RxIngressDrop"),
74 MIB_DESC(1, 0xb8, "RxArlDrop"),
78 mt7530_mutex_lock(struct mt7530_priv *priv)
81 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
85 mt7530_mutex_unlock(struct mt7530_priv *priv)
88 mutex_unlock(&priv->bus->mdio_lock);
92 core_write(struct mt7530_priv *priv, u32 reg, u32 val)
94 struct mii_bus *bus = priv->bus;
97 mt7530_mutex_lock(priv);
99 /* Write the desired MMD Devad */
100 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
101 MII_MMD_CTRL, MDIO_MMD_VEND2);
105 /* Write the desired MMD register address */
106 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
111 /* Select the Function : DATA with no post increment */
112 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
113 MII_MMD_CTRL, MDIO_MMD_VEND2 | MII_MMD_CTRL_NOINCR);
117 /* Write the data into MMD's selected register */
118 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
122 dev_err(&bus->dev, "failed to write mmd register\n");
124 mt7530_mutex_unlock(priv);
128 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
130 struct mii_bus *bus = priv->bus;
134 mt7530_mutex_lock(priv);
136 /* Write the desired MMD Devad */
137 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
138 MII_MMD_CTRL, MDIO_MMD_VEND2);
142 /* Write the desired MMD register address */
143 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
148 /* Select the Function : DATA with no post increment */
149 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
150 MII_MMD_CTRL, MDIO_MMD_VEND2 | MII_MMD_CTRL_NOINCR);
154 /* Read the content of the MMD's selected register */
155 val = bus->read(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
159 /* Write the data into MMD's selected register */
160 ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
164 dev_err(&bus->dev, "failed to write mmd register\n");
166 mt7530_mutex_unlock(priv);
170 core_set(struct mt7530_priv *priv, u32 reg, u32 val)
172 core_rmw(priv, reg, 0, val);
176 core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
178 core_rmw(priv, reg, val, 0);
182 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
186 ret = regmap_write(priv->regmap, reg, val);
190 "failed to write mt7530 register\n");
196 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
201 ret = regmap_read(priv->regmap, reg, &val);
205 "failed to read mt7530 register\n");
213 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
215 mt7530_mutex_lock(priv);
217 mt7530_mii_write(priv, reg, val);
219 mt7530_mutex_unlock(priv);
223 _mt7530_unlocked_read(struct mt7530_dummy_poll *p)
225 return mt7530_mii_read(p->priv, p->reg);
229 _mt7530_read(struct mt7530_dummy_poll *p)
233 mt7530_mutex_lock(p->priv);
235 val = mt7530_mii_read(p->priv, p->reg);
237 mt7530_mutex_unlock(p->priv);
243 mt7530_read(struct mt7530_priv *priv, u32 reg)
245 struct mt7530_dummy_poll p;
247 INIT_MT7530_DUMMY_POLL(&p, priv, reg);
248 return _mt7530_read(&p);
252 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
255 mt7530_mutex_lock(priv);
257 regmap_update_bits(priv->regmap, reg, mask, set);
259 mt7530_mutex_unlock(priv);
263 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
265 mt7530_rmw(priv, reg, val, val);
269 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
271 mt7530_rmw(priv, reg, val, 0);
275 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
279 struct mt7530_dummy_poll p;
281 /* Set the command operating upon the MAC address entries */
282 val = ATC_BUSY | ATC_MAT(0) | cmd;
283 mt7530_write(priv, MT7530_ATC, val);
285 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
286 ret = readx_poll_timeout(_mt7530_read, &p, val,
287 !(val & ATC_BUSY), 20, 20000);
289 dev_err(priv->dev, "reset timeout\n");
293 /* Additional sanity for read command if the specified
296 val = mt7530_read(priv, MT7530_ATC);
297 if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
307 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
312 /* Read from ARL table into an array */
313 for (i = 0; i < 3; i++) {
314 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
316 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
317 __func__, __LINE__, i, reg[i]);
320 fdb->vid = (reg[1] >> CVID) & CVID_MASK;
321 fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
322 fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
323 fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
324 fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
325 fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
326 fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
327 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
328 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
329 fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
333 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
334 u8 port_mask, const u8 *mac,
340 reg[1] |= vid & CVID_MASK;
342 reg[1] |= ATA2_FID(FID_BRIDGED);
343 reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
344 reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
345 /* STATIC_ENT indicate that entry is static wouldn't
346 * be aged out and STATIC_EMP specified as erasing an
349 reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
350 reg[1] |= mac[5] << MAC_BYTE_5;
351 reg[1] |= mac[4] << MAC_BYTE_4;
352 reg[0] |= mac[3] << MAC_BYTE_3;
353 reg[0] |= mac[2] << MAC_BYTE_2;
354 reg[0] |= mac[1] << MAC_BYTE_1;
355 reg[0] |= mac[0] << MAC_BYTE_0;
357 /* Write array into the ARL table */
358 for (i = 0; i < 3; i++)
359 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
362 /* Set up switch core clock for MT7530 */
363 static void mt7530_pll_setup(struct mt7530_priv *priv)
365 /* Disable core clock */
366 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
369 core_write(priv, CORE_GSWPLL_GRP1, 0);
371 /* Set core clock into 500Mhz */
372 core_write(priv, CORE_GSWPLL_GRP2,
373 RG_GSWPLL_POSDIV_500M(1) |
374 RG_GSWPLL_FBKDIV_500M(25));
377 core_write(priv, CORE_GSWPLL_GRP1,
379 RG_GSWPLL_POSDIV_200M(2) |
380 RG_GSWPLL_FBKDIV_200M(32));
384 /* Enable core clock */
385 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
388 /* If port 6 is available as a CPU port, always prefer that as the default,
389 * otherwise don't care.
391 static struct dsa_port *
392 mt753x_preferred_default_local_cpu_port(struct dsa_switch *ds)
394 struct dsa_port *cpu_dp = dsa_to_port(ds, 6);
396 if (dsa_port_is_cpu(cpu_dp))
402 /* Setup port 6 interface mode and TRGMII TX circuit */
404 mt7530_setup_port6(struct dsa_switch *ds, phy_interface_t interface)
406 struct mt7530_priv *priv = ds->priv;
407 u32 ncpo1, ssc_delta, xtal;
409 /* Disable the MT7530 TRGMII clocks */
410 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
412 if (interface == PHY_INTERFACE_MODE_RGMII) {
413 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
418 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK, P6_INTF_MODE(1));
420 xtal = mt7530_read(priv, MT753X_MTRAP) & MT7530_XTAL_MASK;
422 if (xtal == MT7530_XTAL_25MHZ)
427 if (priv->id == ID_MT7621) {
428 /* PLL frequency: 125MHz: 1.0GBit */
429 if (xtal == MT7530_XTAL_40MHZ)
431 if (xtal == MT7530_XTAL_25MHZ)
433 } else { /* PLL frequency: 250MHz: 2.0Gbit */
434 if (xtal == MT7530_XTAL_40MHZ)
436 if (xtal == MT7530_XTAL_25MHZ)
440 /* Setup the MT7530 TRGMII Tx Clock */
441 core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
442 core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
443 core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
444 core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
445 core_write(priv, CORE_PLL_GROUP4, RG_SYSPLL_DDSFBK_EN |
446 RG_SYSPLL_BIAS_EN | RG_SYSPLL_BIAS_LPF_EN);
447 core_write(priv, CORE_PLL_GROUP2, RG_SYSPLL_EN_NORMAL |
448 RG_SYSPLL_VODEN | RG_SYSPLL_POSDIV(1));
449 core_write(priv, CORE_PLL_GROUP7, RG_LCDDS_PCW_NCPO_CHG |
450 RG_LCCDS_C(3) | RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
452 /* Enable the MT7530 TRGMII clocks */
453 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
457 mt7531_pll_setup(struct mt7530_priv *priv)
459 enum mt7531_xtal_fsel xtal;
464 val = mt7530_read(priv, MT7531_CREV);
465 top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR);
466 hwstrap = mt7530_read(priv, MT753X_TRAP);
467 if ((val & CHIP_REV_M) > 0)
468 xtal = (top_sig & PAD_MCM_SMI_EN) ? MT7531_XTAL_FSEL_40MHZ :
469 MT7531_XTAL_FSEL_25MHZ;
471 xtal = (hwstrap & MT7531_XTAL25) ? MT7531_XTAL_FSEL_25MHZ :
472 MT7531_XTAL_FSEL_40MHZ;
474 /* Step 1 : Disable MT7531 COREPLL */
475 val = mt7530_read(priv, MT7531_PLLGP_EN);
477 mt7530_write(priv, MT7531_PLLGP_EN, val);
479 /* Step 2: switch to XTAL output */
480 val = mt7530_read(priv, MT7531_PLLGP_EN);
482 mt7530_write(priv, MT7531_PLLGP_EN, val);
484 val = mt7530_read(priv, MT7531_PLLGP_CR0);
485 val &= ~RG_COREPLL_EN;
486 mt7530_write(priv, MT7531_PLLGP_CR0, val);
488 /* Step 3: disable PLLGP and enable program PLLGP */
489 val = mt7530_read(priv, MT7531_PLLGP_EN);
491 mt7530_write(priv, MT7531_PLLGP_EN, val);
493 /* Step 4: program COREPLL output frequency to 500MHz */
494 val = mt7530_read(priv, MT7531_PLLGP_CR0);
495 val &= ~RG_COREPLL_POSDIV_M;
496 val |= 2 << RG_COREPLL_POSDIV_S;
497 mt7530_write(priv, MT7531_PLLGP_CR0, val);
498 usleep_range(25, 35);
501 case MT7531_XTAL_FSEL_25MHZ:
502 val = mt7530_read(priv, MT7531_PLLGP_CR0);
503 val &= ~RG_COREPLL_SDM_PCW_M;
504 val |= 0x140000 << RG_COREPLL_SDM_PCW_S;
505 mt7530_write(priv, MT7531_PLLGP_CR0, val);
507 case MT7531_XTAL_FSEL_40MHZ:
508 val = mt7530_read(priv, MT7531_PLLGP_CR0);
509 val &= ~RG_COREPLL_SDM_PCW_M;
510 val |= 0x190000 << RG_COREPLL_SDM_PCW_S;
511 mt7530_write(priv, MT7531_PLLGP_CR0, val);
515 /* Set feedback divide ratio update signal to high */
516 val = mt7530_read(priv, MT7531_PLLGP_CR0);
517 val |= RG_COREPLL_SDM_PCW_CHG;
518 mt7530_write(priv, MT7531_PLLGP_CR0, val);
519 /* Wait for at least 16 XTAL clocks */
520 usleep_range(10, 20);
522 /* Step 5: set feedback divide ratio update signal to low */
523 val = mt7530_read(priv, MT7531_PLLGP_CR0);
524 val &= ~RG_COREPLL_SDM_PCW_CHG;
525 mt7530_write(priv, MT7531_PLLGP_CR0, val);
527 /* Enable 325M clock for SGMII */
528 mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
530 /* Enable 250SSC clock for RGMII */
531 mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
533 /* Step 6: Enable MT7531 PLL */
534 val = mt7530_read(priv, MT7531_PLLGP_CR0);
535 val |= RG_COREPLL_EN;
536 mt7530_write(priv, MT7531_PLLGP_CR0, val);
538 val = mt7530_read(priv, MT7531_PLLGP_EN);
540 mt7530_write(priv, MT7531_PLLGP_EN, val);
541 usleep_range(25, 35);
545 mt7530_mib_reset(struct dsa_switch *ds)
547 struct mt7530_priv *priv = ds->priv;
549 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
550 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
553 static int mt7530_phy_read_c22(struct mt7530_priv *priv, int port, int regnum)
555 return mdiobus_read_nested(priv->bus, port, regnum);
558 static int mt7530_phy_write_c22(struct mt7530_priv *priv, int port, int regnum,
561 return mdiobus_write_nested(priv->bus, port, regnum, val);
564 static int mt7530_phy_read_c45(struct mt7530_priv *priv, int port,
565 int devad, int regnum)
567 return mdiobus_c45_read_nested(priv->bus, port, devad, regnum);
570 static int mt7530_phy_write_c45(struct mt7530_priv *priv, int port, int devad,
573 return mdiobus_c45_write_nested(priv->bus, port, devad, regnum, val);
577 mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad,
580 struct mt7530_dummy_poll p;
584 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
586 mt7530_mutex_lock(priv);
588 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
589 !(val & MT7531_PHY_ACS_ST), 20, 100000);
591 dev_err(priv->dev, "poll timeout\n");
595 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
596 MT7531_MDIO_DEV_ADDR(devad) | regnum;
597 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
599 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
600 !(val & MT7531_PHY_ACS_ST), 20, 100000);
602 dev_err(priv->dev, "poll timeout\n");
606 reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) |
607 MT7531_MDIO_DEV_ADDR(devad);
608 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
610 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
611 !(val & MT7531_PHY_ACS_ST), 20, 100000);
613 dev_err(priv->dev, "poll timeout\n");
617 ret = val & MT7531_MDIO_RW_DATA_MASK;
619 mt7530_mutex_unlock(priv);
625 mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad,
626 int regnum, u16 data)
628 struct mt7530_dummy_poll p;
632 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
634 mt7530_mutex_lock(priv);
636 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
637 !(val & MT7531_PHY_ACS_ST), 20, 100000);
639 dev_err(priv->dev, "poll timeout\n");
643 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
644 MT7531_MDIO_DEV_ADDR(devad) | regnum;
645 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
647 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
648 !(val & MT7531_PHY_ACS_ST), 20, 100000);
650 dev_err(priv->dev, "poll timeout\n");
654 reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) |
655 MT7531_MDIO_DEV_ADDR(devad) | data;
656 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
658 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
659 !(val & MT7531_PHY_ACS_ST), 20, 100000);
661 dev_err(priv->dev, "poll timeout\n");
666 mt7530_mutex_unlock(priv);
672 mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum)
674 struct mt7530_dummy_poll p;
678 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
680 mt7530_mutex_lock(priv);
682 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
683 !(val & MT7531_PHY_ACS_ST), 20, 100000);
685 dev_err(priv->dev, "poll timeout\n");
689 val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) |
690 MT7531_MDIO_REG_ADDR(regnum);
692 mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST);
694 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
695 !(val & MT7531_PHY_ACS_ST), 20, 100000);
697 dev_err(priv->dev, "poll timeout\n");
701 ret = val & MT7531_MDIO_RW_DATA_MASK;
703 mt7530_mutex_unlock(priv);
709 mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum,
712 struct mt7530_dummy_poll p;
716 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
718 mt7530_mutex_lock(priv);
720 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
721 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
723 dev_err(priv->dev, "poll timeout\n");
727 reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) |
728 MT7531_MDIO_REG_ADDR(regnum) | data;
730 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
732 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
733 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
735 dev_err(priv->dev, "poll timeout\n");
740 mt7530_mutex_unlock(priv);
746 mt753x_phy_read_c22(struct mii_bus *bus, int port, int regnum)
748 struct mt7530_priv *priv = bus->priv;
750 return priv->info->phy_read_c22(priv, port, regnum);
754 mt753x_phy_read_c45(struct mii_bus *bus, int port, int devad, int regnum)
756 struct mt7530_priv *priv = bus->priv;
758 return priv->info->phy_read_c45(priv, port, devad, regnum);
762 mt753x_phy_write_c22(struct mii_bus *bus, int port, int regnum, u16 val)
764 struct mt7530_priv *priv = bus->priv;
766 return priv->info->phy_write_c22(priv, port, regnum, val);
770 mt753x_phy_write_c45(struct mii_bus *bus, int port, int devad, int regnum,
773 struct mt7530_priv *priv = bus->priv;
775 return priv->info->phy_write_c45(priv, port, devad, regnum, val);
779 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
784 if (stringset != ETH_SS_STATS)
787 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
788 ethtool_puts(&data, mt7530_mib[i].name);
792 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
795 struct mt7530_priv *priv = ds->priv;
796 const struct mt7530_mib_desc *mib;
800 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
801 mib = &mt7530_mib[i];
802 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
804 data[i] = mt7530_read(priv, reg);
805 if (mib->size == 2) {
806 hi = mt7530_read(priv, reg + 4);
813 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
815 if (sset != ETH_SS_STATS)
818 return ARRAY_SIZE(mt7530_mib);
822 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
824 struct mt7530_priv *priv = ds->priv;
825 unsigned int secs = msecs / 1000;
826 unsigned int tmp_age_count;
827 unsigned int error = -1;
828 unsigned int age_count;
829 unsigned int age_unit;
831 /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
832 if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
835 /* iterate through all possible age_count to find the closest pair */
836 for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
837 unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
839 if (tmp_age_unit <= AGE_UNIT_MAX) {
840 unsigned int tmp_error = secs -
841 (tmp_age_count + 1) * (tmp_age_unit + 1);
843 /* found a closer pair */
844 if (error > tmp_error) {
846 age_count = tmp_age_count;
847 age_unit = tmp_age_unit;
850 /* found the exact match, so break the loop */
856 mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
861 static const char *mt7530_p5_mode_str(unsigned int mode)
873 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
875 struct mt7530_priv *priv = ds->priv;
879 mutex_lock(&priv->reg_mutex);
881 val = mt7530_read(priv, MT753X_MTRAP);
883 val &= ~MT7530_P5_PHY0_SEL & ~MT7530_P5_MAC_SEL & ~MT7530_P5_RGMII_MODE;
885 switch (priv->p5_mode) {
886 /* MUX_PHY_P0: P0 -> P5 -> SoC MAC */
888 val |= MT7530_P5_PHY0_SEL;
891 /* MUX_PHY_P4: P4 -> P5 -> SoC MAC */
893 /* Setup the MAC by default for the cpu port */
894 mt7530_write(priv, MT753X_PMCR_P(5), 0x56300);
897 /* GMAC5: P5 -> SoC MAC or external PHY */
899 val |= MT7530_P5_MAC_SEL;
903 /* Setup RGMII settings */
904 if (phy_interface_mode_is_rgmii(interface)) {
905 val |= MT7530_P5_RGMII_MODE;
907 /* P5 RGMII RX Clock Control: delay setting for 1000M */
908 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
910 /* Don't set delay in DSA mode */
911 if (!dsa_is_dsa_port(priv->ds, 5) &&
912 (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
913 interface == PHY_INTERFACE_MODE_RGMII_ID))
914 tx_delay = 4; /* n * 0.5 ns */
916 /* P5 RGMII TX Clock Control: delay x */
917 mt7530_write(priv, MT7530_P5RGMIITXCR,
918 CSR_RGMII_TXC_CFG(0x10 + tx_delay));
920 /* reduce P5 RGMII Tx driving, 8mA */
921 mt7530_write(priv, MT7530_IO_DRV_CR,
922 P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
925 mt7530_write(priv, MT753X_MTRAP, val);
927 dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, mode=%s, phy-mode=%s\n", val,
928 mt7530_p5_mode_str(priv->p5_mode), phy_modes(interface));
930 mutex_unlock(&priv->reg_mutex);
933 /* In Clause 5 of IEEE Std 802-2014, two sublayers of the data link layer (DLL)
934 * of the Open Systems Interconnection basic reference model (OSI/RM) are
935 * described; the medium access control (MAC) and logical link control (LLC)
936 * sublayers. The MAC sublayer is the one facing the physical layer.
938 * In 8.2 of IEEE Std 802.1Q-2022, the Bridge architecture is described. A
939 * Bridge component comprises a MAC Relay Entity for interconnecting the Ports
940 * of the Bridge, at least two Ports, and higher layer entities with at least a
941 * Spanning Tree Protocol Entity included.
943 * Each Bridge Port also functions as an end station and shall provide the MAC
944 * Service to an LLC Entity. Each instance of the MAC Service is provided to a
945 * distinct LLC Entity that supports protocol identification, multiplexing, and
946 * demultiplexing, for protocol data unit (PDU) transmission and reception by
947 * one or more higher layer entities.
949 * It is described in 8.13.9 of IEEE Std 802.1Q-2022 that in a Bridge, the LLC
950 * Entity associated with each Bridge Port is modeled as being directly
951 * connected to the attached Local Area Network (LAN).
953 * On the switch with CPU port architecture, CPU port functions as Management
954 * Port, and the Management Port functionality is provided by software which
955 * functions as an end station. Software is connected to an IEEE 802 LAN that is
956 * wholly contained within the system that incorporates the Bridge. Software
957 * provides access to the LLC Entity associated with each Bridge Port by the
958 * value of the source port field on the special tag on the frame received by
961 * We call frames that carry control information to determine the active
962 * topology and current extent of each Virtual Local Area Network (VLAN), i.e.,
963 * spanning tree or Shortest Path Bridging (SPB) and Multiple VLAN Registration
964 * Protocol Data Units (MVRPDUs), and frames from other link constrained
965 * protocols, such as Extensible Authentication Protocol over LAN (EAPOL) and
966 * Link Layer Discovery Protocol (LLDP), link-local frames. They are not
967 * forwarded by a Bridge. Permanently configured entries in the filtering
968 * database (FDB) ensure that such frames are discarded by the Forwarding
969 * Process. In 8.6.3 of IEEE Std 802.1Q-2022, this is described in detail:
971 * Each of the reserved MAC addresses specified in Table 8-1
972 * (01-80-C2-00-00-[00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F]) shall be
973 * permanently configured in the FDB in C-VLAN components and ERs.
975 * Each of the reserved MAC addresses specified in Table 8-2
976 * (01-80-C2-00-00-[01,02,03,04,05,06,07,08,09,0A,0E]) shall be permanently
977 * configured in the FDB in S-VLAN components.
979 * Each of the reserved MAC addresses specified in Table 8-3
980 * (01-80-C2-00-00-[01,02,04,0E]) shall be permanently configured in the FDB in
983 * The FDB entries for reserved MAC addresses shall specify filtering for all
984 * Bridge Ports and all VIDs. Management shall not provide the capability to
985 * modify or remove entries for reserved MAC addresses.
987 * The addresses in Table 8-1, Table 8-2, and Table 8-3 determine the scope of
988 * propagation of PDUs within a Bridged Network, as follows:
990 * The Nearest Bridge group address (01-80-C2-00-00-0E) is an address that no
991 * conformant Two-Port MAC Relay (TPMR) component, Service VLAN (S-VLAN)
992 * component, Customer VLAN (C-VLAN) component, or MAC Bridge can forward.
993 * PDUs transmitted using this destination address, or any other addresses
994 * that appear in Table 8-1, Table 8-2, and Table 8-3
995 * (01-80-C2-00-00-[00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F]), can
996 * therefore travel no further than those stations that can be reached via a
997 * single individual LAN from the originating station.
999 * The Nearest non-TPMR Bridge group address (01-80-C2-00-00-03), is an
1000 * address that no conformant S-VLAN component, C-VLAN component, or MAC
1001 * Bridge can forward; however, this address is relayed by a TPMR component.
1002 * PDUs using this destination address, or any of the other addresses that
1003 * appear in both Table 8-1 and Table 8-2 but not in Table 8-3
1004 * (01-80-C2-00-00-[00,03,05,06,07,08,09,0A,0B,0C,0D,0F]), will be relayed by
1005 * any TPMRs but will propagate no further than the nearest S-VLAN component,
1006 * C-VLAN component, or MAC Bridge.
1008 * The Nearest Customer Bridge group address (01-80-C2-00-00-00) is an address
1009 * that no conformant C-VLAN component, MAC Bridge can forward; however, it is
1010 * relayed by TPMR components and S-VLAN components. PDUs using this
1011 * destination address, or any of the other addresses that appear in Table 8-1
1012 * but not in either Table 8-2 or Table 8-3 (01-80-C2-00-00-[00,0B,0C,0D,0F]),
1013 * will be relayed by TPMR components and S-VLAN components but will propagate
1014 * no further than the nearest C-VLAN component or MAC Bridge.
1016 * Because the LLC Entity associated with each Bridge Port is provided via CPU
1017 * port, we must not filter these frames but forward them to CPU port.
1019 * In a Bridge, the transmission Port is majorly decided by ingress and egress
1020 * rules, FDB, and spanning tree Port State functions of the Forwarding Process.
1021 * For link-local frames, only CPU port should be designated as destination port
1022 * in the FDB, and the other functions of the Forwarding Process must not
1023 * interfere with the decision of the transmission Port. We call this process
1024 * trapping frames to CPU port.
1026 * Therefore, on the switch with CPU port architecture, link-local frames must
1027 * be trapped to CPU port, and certain link-local frames received by a Port of a
1028 * Bridge comprising a TPMR component or an S-VLAN component must be excluded
1031 * A Bridge of the switch with CPU port architecture cannot comprise a Two-Port
1032 * MAC Relay (TPMR) component as a TPMR component supports only a subset of the
1033 * functionality of a MAC Bridge. A Bridge comprising two Ports (Management Port
1034 * doesn't count) of this architecture will either function as a standard MAC
1035 * Bridge or a standard VLAN Bridge.
1037 * Therefore, a Bridge of this architecture can only comprise S-VLAN components,
1038 * C-VLAN components, or MAC Bridge components. Since there's no TPMR component,
1039 * we don't need to relay PDUs using the destination addresses specified on the
1040 * Nearest non-TPMR section, and the proportion of the Nearest Customer Bridge
1041 * section where they must be relayed by TPMR components.
1043 * One option to trap link-local frames to CPU port is to add static FDB entries
1044 * with CPU port designated as destination port. However, because that
1045 * Independent VLAN Learning (IVL) is being used on every VID, each entry only
1046 * applies to a single VLAN Identifier (VID). For a Bridge comprising a MAC
1047 * Bridge component or a C-VLAN component, there would have to be 16 times 4096
1048 * entries. This switch intellectual property can only hold a maximum of 2048
1049 * entries. Using this option, there also isn't a mechanism to prevent
1050 * link-local frames from being discarded when the spanning tree Port State of
1051 * the reception Port is discarding.
1053 * The remaining option is to utilise the BPC, RGAC1, RGAC2, RGAC3, and RGAC4
1054 * registers. Whilst this applies to every VID, it doesn't contain all of the
1055 * reserved MAC addresses without affecting the remaining Standard Group MAC
1056 * Addresses. The REV_UN frame tag utilised using the RGAC4 register covers the
1057 * remaining 01-80-C2-00-00-[04,05,06,07,08,09,0A,0B,0C,0D,0F] destination
1058 * addresses. It also includes the 01-80-C2-00-00-22 to 01-80-C2-00-00-FF
1059 * destination addresses which may be relayed by MAC Bridges or VLAN Bridges.
1060 * The latter option provides better but not complete conformance.
1062 * This switch intellectual property also does not provide a mechanism to trap
1063 * link-local frames with specific destination addresses to CPU port by Bridge,
1064 * to conform to the filtering rules for the distinct Bridge components.
1066 * Therefore, regardless of the type of the Bridge component, link-local frames
1067 * with these destination addresses will be trapped to CPU port:
1069 * 01-80-C2-00-00-[00,01,02,03,0E]
1071 * In a Bridge comprising a MAC Bridge component or a C-VLAN component:
1073 * Link-local frames with these destination addresses won't be trapped to CPU
1074 * port which won't conform to IEEE Std 802.1Q-2022:
1076 * 01-80-C2-00-00-[04,05,06,07,08,09,0A,0B,0C,0D,0F]
1078 * In a Bridge comprising an S-VLAN component:
1080 * Link-local frames with these destination addresses will be trapped to CPU
1081 * port which won't conform to IEEE Std 802.1Q-2022:
1085 * Link-local frames with these destination addresses won't be trapped to CPU
1086 * port which won't conform to IEEE Std 802.1Q-2022:
1088 * 01-80-C2-00-00-[04,05,06,07,08,09,0A]
1090 * To trap link-local frames to CPU port as conformant as this switch
1091 * intellectual property can allow, link-local frames are made to be regarded as
1092 * Bridge Protocol Data Units (BPDUs). This is because this switch intellectual
1093 * property only lets the frames regarded as BPDUs bypass the spanning tree Port
1094 * State function of the Forwarding Process.
1096 * The only remaining interference is the ingress rules. When the reception Port
1097 * has no PVID assigned on software, VLAN-untagged frames won't be allowed in.
1098 * There doesn't seem to be a mechanism on the switch intellectual property to
1099 * have link-local frames bypass this function of the Forwarding Process.
1102 mt753x_trap_frames(struct mt7530_priv *priv)
1104 /* Trap 802.1X PAE frames and BPDUs to the CPU port(s) and egress them
1107 mt7530_rmw(priv, MT753X_BPC,
1108 PAE_BPDU_FR | PAE_EG_TAG_MASK | PAE_PORT_FW_MASK |
1109 BPDU_EG_TAG_MASK | BPDU_PORT_FW_MASK,
1110 PAE_BPDU_FR | PAE_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1111 PAE_PORT_FW(TO_CPU_FW_CPU_ONLY) |
1112 BPDU_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1113 TO_CPU_FW_CPU_ONLY);
1115 /* Trap frames with :01 and :02 MAC DAs to the CPU port(s) and egress
1116 * them VLAN-untagged.
1118 mt7530_rmw(priv, MT753X_RGAC1,
1119 R02_BPDU_FR | R02_EG_TAG_MASK | R02_PORT_FW_MASK |
1120 R01_BPDU_FR | R01_EG_TAG_MASK | R01_PORT_FW_MASK,
1121 R02_BPDU_FR | R02_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1122 R02_PORT_FW(TO_CPU_FW_CPU_ONLY) | R01_BPDU_FR |
1123 R01_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1124 TO_CPU_FW_CPU_ONLY);
1126 /* Trap frames with :03 and :0E MAC DAs to the CPU port(s) and egress
1127 * them VLAN-untagged.
1129 mt7530_rmw(priv, MT753X_RGAC2,
1130 R0E_BPDU_FR | R0E_EG_TAG_MASK | R0E_PORT_FW_MASK |
1131 R03_BPDU_FR | R03_EG_TAG_MASK | R03_PORT_FW_MASK,
1132 R0E_BPDU_FR | R0E_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1133 R0E_PORT_FW(TO_CPU_FW_CPU_ONLY) | R03_BPDU_FR |
1134 R03_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
1135 TO_CPU_FW_CPU_ONLY);
1139 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
1141 struct mt7530_priv *priv = ds->priv;
1143 /* Enable Mediatek header mode on the cpu port */
1144 mt7530_write(priv, MT7530_PVC_P(port),
1147 /* Enable flooding on the CPU port */
1148 mt7530_set(priv, MT753X_MFC, BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) |
1149 UNU_FFP(BIT(port)));
1151 /* Add the CPU port to the CPU port bitmap for MT7531 and the switch on
1152 * the MT7988 SoC. Trapped frames will be forwarded to the CPU port that
1153 * is affine to the inbound user port.
1155 if (priv->id == ID_MT7531 || priv->id == ID_MT7988)
1156 mt7530_set(priv, MT7531_CFC, MT7531_CPU_PMAP(BIT(port)));
1158 /* CPU port gets connected to all user ports of
1161 mt7530_write(priv, MT7530_PCR_P(port),
1162 PCR_MATRIX(dsa_user_ports(priv->ds)));
1164 /* Set to fallback mode for independent VLAN learning */
1165 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1166 MT7530_PORT_FALLBACK_MODE);
1170 mt7530_port_enable(struct dsa_switch *ds, int port,
1171 struct phy_device *phy)
1173 struct dsa_port *dp = dsa_to_port(ds, port);
1174 struct mt7530_priv *priv = ds->priv;
1176 mutex_lock(&priv->reg_mutex);
1178 /* Allow the user port gets connected to the cpu port and also
1179 * restore the port matrix if the port is the member of a certain
1182 if (dsa_port_is_user(dp)) {
1183 struct dsa_port *cpu_dp = dp->cpu_dp;
1185 priv->ports[port].pm |= PCR_MATRIX(BIT(cpu_dp->index));
1187 priv->ports[port].enable = true;
1188 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1189 priv->ports[port].pm);
1191 mutex_unlock(&priv->reg_mutex);
1193 if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
1197 mt7530_clear(priv, MT753X_MTRAP, MT7530_P5_DIS);
1199 mt7530_clear(priv, MT753X_MTRAP, MT7530_P6_DIS);
1205 mt7530_port_disable(struct dsa_switch *ds, int port)
1207 struct mt7530_priv *priv = ds->priv;
1209 mutex_lock(&priv->reg_mutex);
1211 /* Clear up all port matrix which could be restored in the next
1212 * enablement for the port.
1214 priv->ports[port].enable = false;
1215 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1218 mutex_unlock(&priv->reg_mutex);
1220 if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
1223 /* Do not set MT7530_P5_DIS when port 5 is being used for PHY muxing. */
1224 if (port == 5 && priv->p5_mode == GMAC5)
1225 mt7530_set(priv, MT753X_MTRAP, MT7530_P5_DIS);
1227 mt7530_set(priv, MT753X_MTRAP, MT7530_P6_DIS);
1231 mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1233 struct mt7530_priv *priv = ds->priv;
1237 /* When a new MTU is set, DSA always set the CPU port's MTU to the
1238 * largest MTU of the user ports. Because the switch only has a global
1239 * RX length register, only allowing CPU port here is enough.
1241 if (!dsa_is_cpu_port(ds, port))
1244 mt7530_mutex_lock(priv);
1246 val = mt7530_mii_read(priv, MT7530_GMACCR);
1247 val &= ~MAX_RX_PKT_LEN_MASK;
1249 /* RX length also includes Ethernet header, MTK tag, and FCS length */
1250 length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
1251 if (length <= 1522) {
1252 val |= MAX_RX_PKT_LEN_1522;
1253 } else if (length <= 1536) {
1254 val |= MAX_RX_PKT_LEN_1536;
1255 } else if (length <= 1552) {
1256 val |= MAX_RX_PKT_LEN_1552;
1258 val &= ~MAX_RX_JUMBO_MASK;
1259 val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
1260 val |= MAX_RX_PKT_LEN_JUMBO;
1263 mt7530_mii_write(priv, MT7530_GMACCR, val);
1265 mt7530_mutex_unlock(priv);
1271 mt7530_port_max_mtu(struct dsa_switch *ds, int port)
1273 return MT7530_MAX_MTU;
1277 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1279 struct mt7530_priv *priv = ds->priv;
1283 case BR_STATE_DISABLED:
1284 stp_state = MT7530_STP_DISABLED;
1286 case BR_STATE_BLOCKING:
1287 stp_state = MT7530_STP_BLOCKING;
1289 case BR_STATE_LISTENING:
1290 stp_state = MT7530_STP_LISTENING;
1292 case BR_STATE_LEARNING:
1293 stp_state = MT7530_STP_LEARNING;
1295 case BR_STATE_FORWARDING:
1297 stp_state = MT7530_STP_FORWARDING;
1301 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK(FID_BRIDGED),
1302 FID_PST(FID_BRIDGED, stp_state));
1306 mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1307 struct switchdev_brport_flags flags,
1308 struct netlink_ext_ack *extack)
1310 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1318 mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
1319 struct switchdev_brport_flags flags,
1320 struct netlink_ext_ack *extack)
1322 struct mt7530_priv *priv = ds->priv;
1324 if (flags.mask & BR_LEARNING)
1325 mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
1326 flags.val & BR_LEARNING ? 0 : SA_DIS);
1328 if (flags.mask & BR_FLOOD)
1329 mt7530_rmw(priv, MT753X_MFC, UNU_FFP(BIT(port)),
1330 flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
1332 if (flags.mask & BR_MCAST_FLOOD)
1333 mt7530_rmw(priv, MT753X_MFC, UNM_FFP(BIT(port)),
1334 flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
1336 if (flags.mask & BR_BCAST_FLOOD)
1337 mt7530_rmw(priv, MT753X_MFC, BC_FFP(BIT(port)),
1338 flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
1344 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1345 struct dsa_bridge bridge, bool *tx_fwd_offload,
1346 struct netlink_ext_ack *extack)
1348 struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1349 struct dsa_port *cpu_dp = dp->cpu_dp;
1350 u32 port_bitmap = BIT(cpu_dp->index);
1351 struct mt7530_priv *priv = ds->priv;
1353 mutex_lock(&priv->reg_mutex);
1355 dsa_switch_for_each_user_port(other_dp, ds) {
1356 int other_port = other_dp->index;
1361 /* Add this port to the port matrix of the other ports in the
1362 * same bridge. If the port is disabled, port matrix is kept
1363 * and not being setup until the port becomes enabled.
1365 if (!dsa_port_offloads_bridge(other_dp, &bridge))
1368 if (priv->ports[other_port].enable)
1369 mt7530_set(priv, MT7530_PCR_P(other_port),
1370 PCR_MATRIX(BIT(port)));
1371 priv->ports[other_port].pm |= PCR_MATRIX(BIT(port));
1373 port_bitmap |= BIT(other_port);
1376 /* Add the all other ports to this port matrix. */
1377 if (priv->ports[port].enable)
1378 mt7530_rmw(priv, MT7530_PCR_P(port),
1379 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1380 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1382 /* Set to fallback mode for independent VLAN learning */
1383 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1384 MT7530_PORT_FALLBACK_MODE);
1386 mutex_unlock(&priv->reg_mutex);
1392 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1394 struct mt7530_priv *priv = ds->priv;
1395 bool all_user_ports_removed = true;
1398 /* This is called after .port_bridge_leave when leaving a VLAN-aware
1399 * bridge. Don't set standalone ports to fallback mode.
1401 if (dsa_port_bridge_dev_get(dsa_to_port(ds, port)))
1402 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1403 MT7530_PORT_FALLBACK_MODE);
1405 mt7530_rmw(priv, MT7530_PVC_P(port),
1406 VLAN_ATTR_MASK | PVC_EG_TAG_MASK | ACC_FRM_MASK,
1407 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1408 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT) |
1409 MT7530_VLAN_ACC_ALL);
1412 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1415 for (i = 0; i < priv->ds->num_ports; i++) {
1416 if (dsa_is_user_port(ds, i) &&
1417 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1418 all_user_ports_removed = false;
1423 /* CPU port also does the same thing until all user ports belonging to
1424 * the CPU port get out of VLAN filtering mode.
1426 if (all_user_ports_removed) {
1427 struct dsa_port *dp = dsa_to_port(ds, port);
1428 struct dsa_port *cpu_dp = dp->cpu_dp;
1430 mt7530_write(priv, MT7530_PCR_P(cpu_dp->index),
1431 PCR_MATRIX(dsa_user_ports(priv->ds)));
1432 mt7530_write(priv, MT7530_PVC_P(cpu_dp->index), PORT_SPEC_TAG
1433 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1438 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1440 struct mt7530_priv *priv = ds->priv;
1442 /* Trapped into security mode allows packet forwarding through VLAN
1445 if (dsa_is_user_port(ds, port)) {
1446 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1447 MT7530_PORT_SECURITY_MODE);
1448 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1449 G0_PORT_VID(priv->ports[port].pvid));
1451 /* Only accept tagged frames if PVID is not set */
1452 if (!priv->ports[port].pvid)
1453 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1454 MT7530_VLAN_ACC_TAGGED);
1456 /* Set the port as a user port which is to be able to recognize
1457 * VID from incoming packets before fetching entry within the
1460 mt7530_rmw(priv, MT7530_PVC_P(port),
1461 VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1462 VLAN_ATTR(MT7530_VLAN_USER) |
1463 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
1465 /* Also set CPU ports to the "user" VLAN port attribute, to
1466 * allow VLAN classification, but keep the EG_TAG attribute as
1467 * "consistent" (i.o.w. don't change its value) for packets
1468 * received by the switch from the CPU, so that tagged packets
1469 * are forwarded to user ports as tagged, and untagged as
1472 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
1473 VLAN_ATTR(MT7530_VLAN_USER));
1478 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1479 struct dsa_bridge bridge)
1481 struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1482 struct dsa_port *cpu_dp = dp->cpu_dp;
1483 struct mt7530_priv *priv = ds->priv;
1485 mutex_lock(&priv->reg_mutex);
1487 dsa_switch_for_each_user_port(other_dp, ds) {
1488 int other_port = other_dp->index;
1493 /* Remove this port from the port matrix of the other ports
1494 * in the same bridge. If the port is disabled, port matrix
1495 * is kept and not being setup until the port becomes enabled.
1497 if (!dsa_port_offloads_bridge(other_dp, &bridge))
1500 if (priv->ports[other_port].enable)
1501 mt7530_clear(priv, MT7530_PCR_P(other_port),
1502 PCR_MATRIX(BIT(port)));
1503 priv->ports[other_port].pm &= ~PCR_MATRIX(BIT(port));
1506 /* Set the cpu port to be the only one in the port matrix of
1509 if (priv->ports[port].enable)
1510 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1511 PCR_MATRIX(BIT(cpu_dp->index)));
1512 priv->ports[port].pm = PCR_MATRIX(BIT(cpu_dp->index));
1514 /* When a port is removed from the bridge, the port would be set up
1515 * back to the default as is at initial boot which is a VLAN-unaware
1518 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1519 MT7530_PORT_MATRIX_MODE);
1521 mutex_unlock(&priv->reg_mutex);
1525 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1526 const unsigned char *addr, u16 vid,
1529 struct mt7530_priv *priv = ds->priv;
1531 u8 port_mask = BIT(port);
1533 mutex_lock(&priv->reg_mutex);
1534 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1535 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1536 mutex_unlock(&priv->reg_mutex);
1542 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1543 const unsigned char *addr, u16 vid,
1546 struct mt7530_priv *priv = ds->priv;
1548 u8 port_mask = BIT(port);
1550 mutex_lock(&priv->reg_mutex);
1551 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
1552 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1553 mutex_unlock(&priv->reg_mutex);
1559 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1560 dsa_fdb_dump_cb_t *cb, void *data)
1562 struct mt7530_priv *priv = ds->priv;
1563 struct mt7530_fdb _fdb = { 0 };
1564 int cnt = MT7530_NUM_FDB_RECORDS;
1568 mutex_lock(&priv->reg_mutex);
1570 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1575 if (rsp & ATC_SRCH_HIT) {
1576 mt7530_fdb_read(priv, &_fdb);
1577 if (_fdb.port_mask & BIT(port)) {
1578 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1585 !(rsp & ATC_SRCH_END) &&
1586 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1588 mutex_unlock(&priv->reg_mutex);
1594 mt7530_port_mdb_add(struct dsa_switch *ds, int port,
1595 const struct switchdev_obj_port_mdb *mdb,
1598 struct mt7530_priv *priv = ds->priv;
1599 const u8 *addr = mdb->addr;
1604 mutex_lock(&priv->reg_mutex);
1606 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1607 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1608 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1611 port_mask |= BIT(port);
1612 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1613 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1615 mutex_unlock(&priv->reg_mutex);
1621 mt7530_port_mdb_del(struct dsa_switch *ds, int port,
1622 const struct switchdev_obj_port_mdb *mdb,
1625 struct mt7530_priv *priv = ds->priv;
1626 const u8 *addr = mdb->addr;
1631 mutex_lock(&priv->reg_mutex);
1633 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1634 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1635 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1638 port_mask &= ~BIT(port);
1639 mt7530_fdb_write(priv, vid, port_mask, addr, -1,
1640 port_mask ? STATIC_ENT : STATIC_EMP);
1641 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1643 mutex_unlock(&priv->reg_mutex);
1649 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1651 struct mt7530_dummy_poll p;
1655 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1656 mt7530_write(priv, MT7530_VTCR, val);
1658 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1659 ret = readx_poll_timeout(_mt7530_read, &p, val,
1660 !(val & VTCR_BUSY), 20, 20000);
1662 dev_err(priv->dev, "poll timeout\n");
1666 val = mt7530_read(priv, MT7530_VTCR);
1667 if (val & VTCR_INVALID) {
1668 dev_err(priv->dev, "read VTCR invalid\n");
1676 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1677 struct netlink_ext_ack *extack)
1679 struct dsa_port *dp = dsa_to_port(ds, port);
1680 struct dsa_port *cpu_dp = dp->cpu_dp;
1682 if (vlan_filtering) {
1683 /* The port is being kept as VLAN-unaware port when bridge is
1684 * set up with vlan_filtering not being set, Otherwise, the
1685 * port and the corresponding CPU port is required the setup
1686 * for becoming a VLAN-aware port.
1688 mt7530_port_set_vlan_aware(ds, port);
1689 mt7530_port_set_vlan_aware(ds, cpu_dp->index);
1691 mt7530_port_set_vlan_unaware(ds, port);
1698 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1699 struct mt7530_hw_vlan_entry *entry)
1701 struct dsa_port *dp = dsa_to_port(priv->ds, entry->port);
1705 new_members = entry->old_members | BIT(entry->port);
1707 /* Validate the entry with independent learning, create egress tag per
1708 * VLAN and joining the port as one of the port members.
1710 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | FID(FID_BRIDGED) |
1712 mt7530_write(priv, MT7530_VAWD1, val);
1714 /* Decide whether adding tag or not for those outgoing packets from the
1715 * port inside the VLAN.
1716 * CPU port is always taken as a tagged port for serving more than one
1717 * VLANs across and also being applied with egress type stack mode for
1718 * that VLAN tags would be appended after hardware special tag used as
1721 if (dsa_port_is_cpu(dp))
1722 val = MT7530_VLAN_EGRESS_STACK;
1723 else if (entry->untagged)
1724 val = MT7530_VLAN_EGRESS_UNTAG;
1726 val = MT7530_VLAN_EGRESS_TAG;
1727 mt7530_rmw(priv, MT7530_VAWD2,
1728 ETAG_CTRL_P_MASK(entry->port),
1729 ETAG_CTRL_P(entry->port, val));
1733 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1734 struct mt7530_hw_vlan_entry *entry)
1739 new_members = entry->old_members & ~BIT(entry->port);
1741 val = mt7530_read(priv, MT7530_VAWD1);
1742 if (!(val & VLAN_VALID)) {
1744 "Cannot be deleted due to invalid entry\n");
1749 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1751 mt7530_write(priv, MT7530_VAWD1, val);
1753 mt7530_write(priv, MT7530_VAWD1, 0);
1754 mt7530_write(priv, MT7530_VAWD2, 0);
1759 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1760 struct mt7530_hw_vlan_entry *entry,
1761 mt7530_vlan_op vlan_op)
1766 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1768 val = mt7530_read(priv, MT7530_VAWD1);
1770 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1772 /* Manipulate entry */
1773 vlan_op(priv, entry);
1775 /* Flush result to hardware */
1776 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1780 mt7530_setup_vlan0(struct mt7530_priv *priv)
1784 /* Validate the entry with independent learning, keep the original
1785 * ingress tag attribute.
1787 val = IVL_MAC | EG_CON | PORT_MEM(MT7530_ALL_MEMBERS) | FID(FID_BRIDGED) |
1789 mt7530_write(priv, MT7530_VAWD1, val);
1791 return mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, 0);
1795 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1796 const struct switchdev_obj_port_vlan *vlan,
1797 struct netlink_ext_ack *extack)
1799 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1800 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1801 struct mt7530_hw_vlan_entry new_entry;
1802 struct mt7530_priv *priv = ds->priv;
1804 mutex_lock(&priv->reg_mutex);
1806 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1807 mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
1810 priv->ports[port].pvid = vlan->vid;
1812 /* Accept all frames if PVID is set */
1813 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1814 MT7530_VLAN_ACC_ALL);
1816 /* Only configure PVID if VLAN filtering is enabled */
1817 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1818 mt7530_rmw(priv, MT7530_PPBV1_P(port),
1820 G0_PORT_VID(vlan->vid));
1821 } else if (vlan->vid && priv->ports[port].pvid == vlan->vid) {
1822 /* This VLAN is overwritten without PVID, so unset it */
1823 priv->ports[port].pvid = G0_PORT_VID_DEF;
1825 /* Only accept tagged frames if the port is VLAN-aware */
1826 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1827 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1828 MT7530_VLAN_ACC_TAGGED);
1830 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1834 mutex_unlock(&priv->reg_mutex);
1840 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1841 const struct switchdev_obj_port_vlan *vlan)
1843 struct mt7530_hw_vlan_entry target_entry;
1844 struct mt7530_priv *priv = ds->priv;
1846 mutex_lock(&priv->reg_mutex);
1848 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1849 mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
1850 mt7530_hw_vlan_del);
1852 /* PVID is being restored to the default whenever the PVID port
1853 * is being removed from the VLAN.
1855 if (priv->ports[port].pvid == vlan->vid) {
1856 priv->ports[port].pvid = G0_PORT_VID_DEF;
1858 /* Only accept tagged frames if the port is VLAN-aware */
1859 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1860 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1861 MT7530_VLAN_ACC_TAGGED);
1863 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1868 mutex_unlock(&priv->reg_mutex);
1873 static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
1874 struct dsa_mall_mirror_tc_entry *mirror,
1875 bool ingress, struct netlink_ext_ack *extack)
1877 struct mt7530_priv *priv = ds->priv;
1881 /* Check for existent entry */
1882 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1885 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1887 /* MT7530 only supports one monitor port */
1888 monitor_port = MT753X_MIRROR_PORT_GET(priv->id, val);
1889 if (val & MT753X_MIRROR_EN(priv->id) &&
1890 monitor_port != mirror->to_local_port)
1893 val |= MT753X_MIRROR_EN(priv->id);
1894 val &= ~MT753X_MIRROR_PORT_MASK(priv->id);
1895 val |= MT753X_MIRROR_PORT_SET(priv->id, mirror->to_local_port);
1896 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1898 val = mt7530_read(priv, MT7530_PCR_P(port));
1901 priv->mirror_rx |= BIT(port);
1904 priv->mirror_tx |= BIT(port);
1906 mt7530_write(priv, MT7530_PCR_P(port), val);
1911 static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
1912 struct dsa_mall_mirror_tc_entry *mirror)
1914 struct mt7530_priv *priv = ds->priv;
1917 val = mt7530_read(priv, MT7530_PCR_P(port));
1918 if (mirror->ingress) {
1919 val &= ~PORT_RX_MIR;
1920 priv->mirror_rx &= ~BIT(port);
1922 val &= ~PORT_TX_MIR;
1923 priv->mirror_tx &= ~BIT(port);
1925 mt7530_write(priv, MT7530_PCR_P(port), val);
1927 if (!priv->mirror_rx && !priv->mirror_tx) {
1928 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1929 val &= ~MT753X_MIRROR_EN(priv->id);
1930 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1934 static enum dsa_tag_protocol
1935 mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1936 enum dsa_tag_protocol mp)
1938 return DSA_TAG_PROTO_MTK;
1941 #ifdef CONFIG_GPIOLIB
1943 mt7530_gpio_to_bit(unsigned int offset)
1945 /* Map GPIO offset to register bit
1946 * [ 2: 0] port 0 LED 0..2 as GPIO 0..2
1947 * [ 6: 4] port 1 LED 0..2 as GPIO 3..5
1948 * [10: 8] port 2 LED 0..2 as GPIO 6..8
1949 * [14:12] port 3 LED 0..2 as GPIO 9..11
1950 * [18:16] port 4 LED 0..2 as GPIO 12..14
1952 return BIT(offset + offset / 3);
1956 mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
1958 struct mt7530_priv *priv = gpiochip_get_data(gc);
1959 u32 bit = mt7530_gpio_to_bit(offset);
1961 return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
1965 mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
1967 struct mt7530_priv *priv = gpiochip_get_data(gc);
1968 u32 bit = mt7530_gpio_to_bit(offset);
1971 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1973 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1977 mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
1979 struct mt7530_priv *priv = gpiochip_get_data(gc);
1980 u32 bit = mt7530_gpio_to_bit(offset);
1982 return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
1983 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1987 mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
1989 struct mt7530_priv *priv = gpiochip_get_data(gc);
1990 u32 bit = mt7530_gpio_to_bit(offset);
1992 mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
1993 mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
1999 mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
2001 struct mt7530_priv *priv = gpiochip_get_data(gc);
2002 u32 bit = mt7530_gpio_to_bit(offset);
2004 mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
2007 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
2009 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
2011 mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
2017 mt7530_setup_gpio(struct mt7530_priv *priv)
2019 struct device *dev = priv->dev;
2020 struct gpio_chip *gc;
2022 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
2026 mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
2027 mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
2028 mt7530_write(priv, MT7530_LED_IO_MODE, 0);
2030 gc->label = "mt7530";
2032 gc->owner = THIS_MODULE;
2033 gc->get_direction = mt7530_gpio_get_direction;
2034 gc->direction_input = mt7530_gpio_direction_input;
2035 gc->direction_output = mt7530_gpio_direction_output;
2036 gc->get = mt7530_gpio_get;
2037 gc->set = mt7530_gpio_set;
2040 gc->can_sleep = true;
2042 return devm_gpiochip_add_data(dev, gc, priv);
2044 #endif /* CONFIG_GPIOLIB */
2047 mt7530_irq_thread_fn(int irq, void *dev_id)
2049 struct mt7530_priv *priv = dev_id;
2050 bool handled = false;
2054 mt7530_mutex_lock(priv);
2055 val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
2056 mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
2057 mt7530_mutex_unlock(priv);
2059 for (p = 0; p < MT7530_NUM_PHYS; p++) {
2063 irq = irq_find_mapping(priv->irq_domain, p);
2064 handle_nested_irq(irq);
2069 return IRQ_RETVAL(handled);
2073 mt7530_irq_mask(struct irq_data *d)
2075 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2077 priv->irq_enable &= ~BIT(d->hwirq);
2081 mt7530_irq_unmask(struct irq_data *d)
2083 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2085 priv->irq_enable |= BIT(d->hwirq);
2089 mt7530_irq_bus_lock(struct irq_data *d)
2091 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2093 mt7530_mutex_lock(priv);
2097 mt7530_irq_bus_sync_unlock(struct irq_data *d)
2099 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2101 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
2102 mt7530_mutex_unlock(priv);
2105 static struct irq_chip mt7530_irq_chip = {
2106 .name = KBUILD_MODNAME,
2107 .irq_mask = mt7530_irq_mask,
2108 .irq_unmask = mt7530_irq_unmask,
2109 .irq_bus_lock = mt7530_irq_bus_lock,
2110 .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
2114 mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
2115 irq_hw_number_t hwirq)
2117 irq_set_chip_data(irq, domain->host_data);
2118 irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
2119 irq_set_nested_thread(irq, true);
2120 irq_set_noprobe(irq);
2125 static const struct irq_domain_ops mt7530_irq_domain_ops = {
2126 .map = mt7530_irq_map,
2127 .xlate = irq_domain_xlate_onecell,
2131 mt7988_irq_mask(struct irq_data *d)
2133 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2135 priv->irq_enable &= ~BIT(d->hwirq);
2136 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
2140 mt7988_irq_unmask(struct irq_data *d)
2142 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
2144 priv->irq_enable |= BIT(d->hwirq);
2145 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
2148 static struct irq_chip mt7988_irq_chip = {
2149 .name = KBUILD_MODNAME,
2150 .irq_mask = mt7988_irq_mask,
2151 .irq_unmask = mt7988_irq_unmask,
2155 mt7988_irq_map(struct irq_domain *domain, unsigned int irq,
2156 irq_hw_number_t hwirq)
2158 irq_set_chip_data(irq, domain->host_data);
2159 irq_set_chip_and_handler(irq, &mt7988_irq_chip, handle_simple_irq);
2160 irq_set_nested_thread(irq, true);
2161 irq_set_noprobe(irq);
2166 static const struct irq_domain_ops mt7988_irq_domain_ops = {
2167 .map = mt7988_irq_map,
2168 .xlate = irq_domain_xlate_onecell,
2172 mt7530_setup_mdio_irq(struct mt7530_priv *priv)
2174 struct dsa_switch *ds = priv->ds;
2177 for (p = 0; p < MT7530_NUM_PHYS; p++) {
2178 if (BIT(p) & ds->phys_mii_mask) {
2181 irq = irq_create_mapping(priv->irq_domain, p);
2182 ds->user_mii_bus->irq[p] = irq;
2188 mt7530_setup_irq(struct mt7530_priv *priv)
2190 struct device *dev = priv->dev;
2191 struct device_node *np = dev->of_node;
2194 if (!of_property_read_bool(np, "interrupt-controller")) {
2195 dev_info(dev, "no interrupt support\n");
2199 priv->irq = of_irq_get(np, 0);
2200 if (priv->irq <= 0) {
2201 dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
2202 return priv->irq ? : -EINVAL;
2205 if (priv->id == ID_MT7988)
2206 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
2207 &mt7988_irq_domain_ops,
2210 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
2211 &mt7530_irq_domain_ops,
2214 if (!priv->irq_domain) {
2215 dev_err(dev, "failed to create IRQ domain\n");
2219 /* This register must be set for MT7530 to properly fire interrupts */
2220 if (priv->id == ID_MT7530 || priv->id == ID_MT7621)
2221 mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
2223 ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
2224 IRQF_ONESHOT, KBUILD_MODNAME, priv);
2226 irq_domain_remove(priv->irq_domain);
2227 dev_err(dev, "failed to request IRQ: %d\n", ret);
2235 mt7530_free_mdio_irq(struct mt7530_priv *priv)
2239 for (p = 0; p < MT7530_NUM_PHYS; p++) {
2240 if (BIT(p) & priv->ds->phys_mii_mask) {
2243 irq = irq_find_mapping(priv->irq_domain, p);
2244 irq_dispose_mapping(irq);
2250 mt7530_free_irq_common(struct mt7530_priv *priv)
2252 free_irq(priv->irq, priv);
2253 irq_domain_remove(priv->irq_domain);
2257 mt7530_free_irq(struct mt7530_priv *priv)
2259 struct device_node *mnp, *np = priv->dev->of_node;
2261 mnp = of_get_child_by_name(np, "mdio");
2263 mt7530_free_mdio_irq(priv);
2266 mt7530_free_irq_common(priv);
2270 mt7530_setup_mdio(struct mt7530_priv *priv)
2272 struct device_node *mnp, *np = priv->dev->of_node;
2273 struct dsa_switch *ds = priv->ds;
2274 struct device *dev = priv->dev;
2275 struct mii_bus *bus;
2279 mnp = of_get_child_by_name(np, "mdio");
2281 if (mnp && !of_device_is_available(mnp))
2284 bus = devm_mdiobus_alloc(dev);
2291 ds->user_mii_bus = bus;
2294 bus->name = KBUILD_MODNAME "-mii";
2295 snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
2296 bus->read = mt753x_phy_read_c22;
2297 bus->write = mt753x_phy_write_c22;
2298 bus->read_c45 = mt753x_phy_read_c45;
2299 bus->write_c45 = mt753x_phy_write_c45;
2301 bus->phy_mask = ~ds->phys_mii_mask;
2303 if (priv->irq && !mnp)
2304 mt7530_setup_mdio_irq(priv);
2306 ret = devm_of_mdiobus_register(dev, bus, mnp);
2308 dev_err(dev, "failed to register MDIO bus: %d\n", ret);
2309 if (priv->irq && !mnp)
2310 mt7530_free_mdio_irq(priv);
2319 mt7530_setup(struct dsa_switch *ds)
2321 struct mt7530_priv *priv = ds->priv;
2322 struct device_node *dn = NULL;
2323 struct device_node *phy_node;
2324 struct device_node *mac_np;
2325 struct mt7530_dummy_poll p;
2326 phy_interface_t interface;
2327 struct dsa_port *cpu_dp;
2331 /* The parent node of conduit netdev which holds the common system
2332 * controller also is the container for two GMACs nodes representing
2333 * as two netdev instances.
2335 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
2336 dn = cpu_dp->conduit->dev.of_node->parent;
2337 /* It doesn't matter which CPU port is found first,
2338 * their conduits should share the same parent OF node
2344 dev_err(ds->dev, "parent OF node of DSA conduit not found");
2348 ds->assisted_learning_on_cpu_port = true;
2349 ds->mtu_enforcement_ingress = true;
2351 if (priv->id == ID_MT7530) {
2352 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
2353 ret = regulator_enable(priv->core_pwr);
2356 "Failed to enable core power: %d\n", ret);
2360 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
2361 ret = regulator_enable(priv->io_pwr);
2363 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
2369 /* Reset whole chip through gpio pin or memory-mapped registers for
2370 * different type of hardware
2373 reset_control_assert(priv->rstc);
2374 usleep_range(5000, 5100);
2375 reset_control_deassert(priv->rstc);
2377 gpiod_set_value_cansleep(priv->reset, 0);
2378 usleep_range(5000, 5100);
2379 gpiod_set_value_cansleep(priv->reset, 1);
2382 /* Waiting for MT7530 got to stable */
2383 INIT_MT7530_DUMMY_POLL(&p, priv, MT753X_TRAP);
2384 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2387 dev_err(priv->dev, "reset timeout\n");
2391 id = mt7530_read(priv, MT7530_CREV);
2392 id >>= CHIP_NAME_SHIFT;
2393 if (id != MT7530_ID) {
2394 dev_err(priv->dev, "chip %x can't be supported\n", id);
2398 if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_20MHZ) {
2400 "MT7530 with a 20MHz XTAL is not supported!\n");
2404 /* Reset the switch through internal reset */
2405 mt7530_write(priv, MT7530_SYS_CTRL,
2406 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2409 /* Lower Tx driving for TRGMII path */
2410 for (i = 0; i < NUM_TRGMII_CTRL; i++)
2411 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
2412 TD_DM_DRVP(8) | TD_DM_DRVN(8));
2414 for (i = 0; i < NUM_TRGMII_CTRL; i++)
2415 mt7530_rmw(priv, MT7530_TRGMII_RD(i),
2416 RD_TAP_MASK, RD_TAP(16));
2418 /* Allow modifying the trap and directly access PHY registers via the
2419 * MDIO bus the switch is on.
2421 mt7530_rmw(priv, MT753X_MTRAP, MT7530_CHG_TRAP |
2422 MT7530_PHY_INDIRECT_ACCESS, MT7530_CHG_TRAP);
2424 if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_40MHZ)
2425 mt7530_pll_setup(priv);
2427 mt753x_trap_frames(priv);
2429 /* Enable and reset MIB counters */
2430 mt7530_mib_reset(ds);
2432 for (i = 0; i < priv->ds->num_ports; i++) {
2433 /* Clear link settings and enable force mode to force link down
2434 * on all ports until they're enabled later.
2436 mt7530_rmw(priv, MT753X_PMCR_P(i), PMCR_LINK_SETTINGS_MASK |
2437 MT7530_FORCE_MODE, MT7530_FORCE_MODE);
2439 /* Disable forwarding by default on all ports */
2440 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2443 /* Disable learning by default on all ports */
2444 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2446 if (dsa_is_cpu_port(ds, i)) {
2447 mt753x_cpu_port_enable(ds, i);
2449 mt7530_port_disable(ds, i);
2451 /* Set default PVID to 0 on all user ports */
2452 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2455 /* Enable consistent egress tag */
2456 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2457 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2460 /* Allow mirroring frames received on the local port (monitor port). */
2461 mt7530_set(priv, MT753X_AGC, LOCAL_EN);
2463 /* Setup VLAN ID 0 for VLAN-unaware bridges */
2464 ret = mt7530_setup_vlan0(priv);
2468 /* Check for PHY muxing on port 5 */
2469 if (dsa_is_unused_port(ds, 5)) {
2470 /* Scan the ethernet nodes. Look for GMAC1, lookup the used PHY.
2471 * Set priv->p5_mode to the appropriate value if PHY muxing is
2474 for_each_child_of_node(dn, mac_np) {
2475 if (!of_device_is_compatible(mac_np,
2476 "mediatek,eth-mac"))
2479 ret = of_property_read_u32(mac_np, "reg", &id);
2480 if (ret < 0 || id != 1)
2483 phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
2487 if (phy_node->parent == priv->dev->of_node->parent ||
2488 phy_node->parent->parent == priv->dev->of_node) {
2489 ret = of_get_phy_mode(mac_np, &interface);
2490 if (ret && ret != -ENODEV) {
2491 of_node_put(mac_np);
2492 of_node_put(phy_node);
2495 id = of_mdio_parse_addr(ds->dev, phy_node);
2497 priv->p5_mode = MUX_PHY_P0;
2499 priv->p5_mode = MUX_PHY_P4;
2501 of_node_put(mac_np);
2502 of_node_put(phy_node);
2506 if (priv->p5_mode == MUX_PHY_P0 ||
2507 priv->p5_mode == MUX_PHY_P4) {
2508 mt7530_clear(priv, MT753X_MTRAP, MT7530_P5_DIS);
2509 mt7530_setup_port5(ds, interface);
2513 #ifdef CONFIG_GPIOLIB
2514 if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
2515 ret = mt7530_setup_gpio(priv);
2519 #endif /* CONFIG_GPIOLIB */
2521 /* Flush the FDB table */
2522 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2530 mt7531_setup_common(struct dsa_switch *ds)
2532 struct mt7530_priv *priv = ds->priv;
2535 mt753x_trap_frames(priv);
2537 /* Enable and reset MIB counters */
2538 mt7530_mib_reset(ds);
2540 /* Disable flooding on all ports */
2541 mt7530_clear(priv, MT753X_MFC, BC_FFP_MASK | UNM_FFP_MASK |
2544 for (i = 0; i < priv->ds->num_ports; i++) {
2545 /* Clear link settings and enable force mode to force link down
2546 * on all ports until they're enabled later.
2548 mt7530_rmw(priv, MT753X_PMCR_P(i), PMCR_LINK_SETTINGS_MASK |
2549 MT7531_FORCE_MODE_MASK, MT7531_FORCE_MODE_MASK);
2551 /* Disable forwarding by default on all ports */
2552 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2555 /* Disable learning by default on all ports */
2556 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2558 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
2560 if (dsa_is_cpu_port(ds, i)) {
2561 mt753x_cpu_port_enable(ds, i);
2563 mt7530_port_disable(ds, i);
2565 /* Set default PVID to 0 on all user ports */
2566 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2570 /* Enable consistent egress tag */
2571 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2572 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2575 /* Allow mirroring frames received on the local port (monitor port). */
2576 mt7530_set(priv, MT753X_AGC, LOCAL_EN);
2578 /* Flush the FDB table */
2579 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2587 mt7531_setup(struct dsa_switch *ds)
2589 struct mt7530_priv *priv = ds->priv;
2590 struct mt7530_dummy_poll p;
2594 /* Reset whole chip through gpio pin or memory-mapped registers for
2595 * different type of hardware
2598 reset_control_assert(priv->rstc);
2599 usleep_range(5000, 5100);
2600 reset_control_deassert(priv->rstc);
2602 gpiod_set_value_cansleep(priv->reset, 0);
2603 usleep_range(5000, 5100);
2604 gpiod_set_value_cansleep(priv->reset, 1);
2607 /* Waiting for MT7530 got to stable */
2608 INIT_MT7530_DUMMY_POLL(&p, priv, MT753X_TRAP);
2609 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2612 dev_err(priv->dev, "reset timeout\n");
2616 id = mt7530_read(priv, MT7531_CREV);
2617 id >>= CHIP_NAME_SHIFT;
2619 if (id != MT7531_ID) {
2620 dev_err(priv->dev, "chip %x can't be supported\n", id);
2624 /* MT7531AE has got two SGMII units. One for port 5, one for port 6.
2625 * MT7531BE has got only one SGMII unit which is for port 6.
2627 val = mt7530_read(priv, MT7531_TOP_SIG_SR);
2628 priv->p5_sgmii = !!(val & PAD_DUAL_SGMII_EN);
2630 /* Force link down on all ports before internal reset */
2631 for (i = 0; i < priv->ds->num_ports; i++)
2632 mt7530_write(priv, MT753X_PMCR_P(i), MT7531_FORCE_MODE_LNK);
2634 /* Reset the switch through internal reset */
2635 mt7530_write(priv, MT7530_SYS_CTRL, SYS_CTRL_SW_RST | SYS_CTRL_REG_RST);
2637 if (!priv->p5_sgmii) {
2638 mt7531_pll_setup(priv);
2640 /* Unlike MT7531BE, the GPIO 6-12 pins are not used for RGMII on
2641 * MT7531AE. Set the GPIO 11-12 pins to function as MDC and MDIO
2642 * to expose the MDIO bus of the switch.
2644 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
2645 MT7531_EXT_P_MDC_11);
2646 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
2647 MT7531_EXT_P_MDIO_12);
2650 mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
2651 MT7531_GPIO0_INTERRUPT);
2653 /* Enable Energy-Efficient Ethernet (EEE) and PHY core PLL, since
2654 * phy_device has not yet been created provided for
2655 * phy_[read,write]_mmd_indirect is called, we provide our own
2656 * mt7531_ind_mmd_phy_[read,write] to complete this function.
2658 val = mt7531_ind_c45_phy_read(priv,
2659 MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
2660 MDIO_MMD_VEND2, CORE_PLL_GROUP4);
2661 val |= MT7531_RG_SYSPLL_DMY2 | MT7531_PHY_PLL_BYPASS_MODE;
2662 val &= ~MT7531_PHY_PLL_OFF;
2663 mt7531_ind_c45_phy_write(priv,
2664 MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
2665 MDIO_MMD_VEND2, CORE_PLL_GROUP4, val);
2667 /* Disable EEE advertisement on the switch PHYs. */
2668 for (i = MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr);
2669 i < MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr) + MT7530_NUM_PHYS;
2671 mt7531_ind_c45_phy_write(priv, i, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
2675 ret = mt7531_setup_common(ds);
2679 /* Setup VLAN ID 0 for VLAN-unaware bridges */
2680 ret = mt7530_setup_vlan0(priv);
2684 ds->assisted_learning_on_cpu_port = true;
2685 ds->mtu_enforcement_ingress = true;
2690 static void mt7530_mac_port_get_caps(struct dsa_switch *ds, int port,
2691 struct phylink_config *config)
2693 config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
2696 /* Ports which are connected to switch PHYs. There is no MII pinout. */
2698 __set_bit(PHY_INTERFACE_MODE_GMII,
2699 config->supported_interfaces);
2702 /* Port 5 supports rgmii with delays, mii, and gmii. */
2704 phy_interface_set_rgmii(config->supported_interfaces);
2705 __set_bit(PHY_INTERFACE_MODE_MII,
2706 config->supported_interfaces);
2707 __set_bit(PHY_INTERFACE_MODE_GMII,
2708 config->supported_interfaces);
2711 /* Port 6 supports rgmii and trgmii. */
2713 __set_bit(PHY_INTERFACE_MODE_RGMII,
2714 config->supported_interfaces);
2715 __set_bit(PHY_INTERFACE_MODE_TRGMII,
2716 config->supported_interfaces);
2721 static void mt7531_mac_port_get_caps(struct dsa_switch *ds, int port,
2722 struct phylink_config *config)
2724 struct mt7530_priv *priv = ds->priv;
2726 config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
2729 /* Ports which are connected to switch PHYs. There is no MII pinout. */
2731 __set_bit(PHY_INTERFACE_MODE_GMII,
2732 config->supported_interfaces);
2735 /* Port 5 supports rgmii with delays on MT7531BE, sgmii/802.3z on
2739 if (!priv->p5_sgmii) {
2740 phy_interface_set_rgmii(config->supported_interfaces);
2745 /* Port 6 supports sgmii/802.3z. */
2747 __set_bit(PHY_INTERFACE_MODE_SGMII,
2748 config->supported_interfaces);
2749 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
2750 config->supported_interfaces);
2751 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
2752 config->supported_interfaces);
2754 config->mac_capabilities |= MAC_2500FD;
2759 static void mt7988_mac_port_get_caps(struct dsa_switch *ds, int port,
2760 struct phylink_config *config)
2763 /* Ports which are connected to switch PHYs. There is no MII pinout. */
2765 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
2766 config->supported_interfaces);
2768 config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
2771 /* Port 6 is connected to SoC's XGMII MAC. There is no MII pinout. */
2773 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
2774 config->supported_interfaces);
2776 config->mac_capabilities |= MAC_10000FD;
2782 mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2783 phy_interface_t interface)
2785 struct mt7530_priv *priv = ds->priv;
2788 mt7530_setup_port5(priv->ds, interface);
2790 mt7530_setup_port6(priv->ds, interface);
2793 static void mt7531_rgmii_setup(struct mt7530_priv *priv,
2794 phy_interface_t interface,
2795 struct phy_device *phydev)
2799 val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2801 val &= ~GP_MODE_MASK;
2802 val |= GP_MODE(MT7531_GP_MODE_RGMII);
2803 val &= ~CLK_SKEW_IN_MASK;
2804 val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2805 val &= ~CLK_SKEW_OUT_MASK;
2806 val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2807 val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2809 /* Do not adjust rgmii delay when vendor phy driver presents. */
2810 if (!phydev || phy_driver_is_genphy(phydev)) {
2811 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2812 switch (interface) {
2813 case PHY_INTERFACE_MODE_RGMII:
2814 val |= TXCLK_NO_REVERSE;
2815 val |= RXCLK_NO_DELAY;
2817 case PHY_INTERFACE_MODE_RGMII_RXID:
2818 val |= TXCLK_NO_REVERSE;
2820 case PHY_INTERFACE_MODE_RGMII_TXID:
2821 val |= RXCLK_NO_DELAY;
2823 case PHY_INTERFACE_MODE_RGMII_ID:
2830 mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2834 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2835 phy_interface_t interface)
2837 struct mt7530_priv *priv = ds->priv;
2838 struct phy_device *phydev;
2839 struct dsa_port *dp;
2841 if (phy_interface_mode_is_rgmii(interface)) {
2842 dp = dsa_to_port(ds, port);
2843 phydev = dp->user->phydev;
2844 mt7531_rgmii_setup(priv, interface, phydev);
2848 static struct phylink_pcs *
2849 mt753x_phylink_mac_select_pcs(struct phylink_config *config,
2850 phy_interface_t interface)
2852 struct dsa_port *dp = dsa_phylink_to_port(config);
2853 struct mt7530_priv *priv = dp->ds->priv;
2855 switch (interface) {
2856 case PHY_INTERFACE_MODE_TRGMII:
2857 return &priv->pcs[dp->index].pcs;
2858 case PHY_INTERFACE_MODE_SGMII:
2859 case PHY_INTERFACE_MODE_1000BASEX:
2860 case PHY_INTERFACE_MODE_2500BASEX:
2861 return priv->ports[dp->index].sgmii_pcs;
2868 mt753x_phylink_mac_config(struct phylink_config *config, unsigned int mode,
2869 const struct phylink_link_state *state)
2871 struct dsa_port *dp = dsa_phylink_to_port(config);
2872 struct dsa_switch *ds = dp->ds;
2873 struct mt7530_priv *priv;
2874 int port = dp->index;
2878 if ((port == 5 || port == 6) && priv->info->mac_port_config)
2879 priv->info->mac_port_config(ds, port, mode, state->interface);
2881 /* Are we connected to external phy */
2882 if (port == 5 && dsa_is_user_port(ds, 5))
2883 mt7530_set(priv, MT753X_PMCR_P(port), PMCR_EXT_PHY);
2886 static void mt753x_phylink_mac_link_down(struct phylink_config *config,
2888 phy_interface_t interface)
2890 struct dsa_port *dp = dsa_phylink_to_port(config);
2891 struct mt7530_priv *priv = dp->ds->priv;
2893 mt7530_clear(priv, MT753X_PMCR_P(dp->index), PMCR_LINK_SETTINGS_MASK);
2896 static void mt753x_phylink_mac_link_up(struct phylink_config *config,
2897 struct phy_device *phydev,
2899 phy_interface_t interface,
2900 int speed, int duplex,
2901 bool tx_pause, bool rx_pause)
2903 struct dsa_port *dp = dsa_phylink_to_port(config);
2904 struct mt7530_priv *priv = dp->ds->priv;
2907 mcr = PMCR_MAC_RX_EN | PMCR_MAC_TX_EN | PMCR_FORCE_LNK;
2913 mcr |= PMCR_FORCE_SPEED_1000;
2916 mcr |= PMCR_FORCE_SPEED_100;
2919 if (duplex == DUPLEX_FULL) {
2920 mcr |= PMCR_FORCE_FDX;
2922 mcr |= PMCR_FORCE_TX_FC_EN;
2924 mcr |= PMCR_FORCE_RX_FC_EN;
2927 if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, false) >= 0) {
2931 mcr |= PMCR_FORCE_EEE1G;
2934 mcr |= PMCR_FORCE_EEE100;
2939 mt7530_set(priv, MT753X_PMCR_P(dp->index), mcr);
2942 static void mt753x_phylink_get_caps(struct dsa_switch *ds, int port,
2943 struct phylink_config *config)
2945 struct mt7530_priv *priv = ds->priv;
2947 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE;
2949 priv->info->mac_port_get_caps(ds, port, config);
2952 static int mt753x_pcs_validate(struct phylink_pcs *pcs,
2953 unsigned long *supported,
2954 const struct phylink_link_state *state)
2956 /* Autonegotiation is not supported in TRGMII nor 802.3z modes */
2957 if (state->interface == PHY_INTERFACE_MODE_TRGMII ||
2958 phy_interface_mode_is_8023z(state->interface))
2959 phylink_clear(supported, Autoneg);
2964 static void mt7530_pcs_get_state(struct phylink_pcs *pcs,
2965 struct phylink_link_state *state)
2967 struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
2968 int port = pcs_to_mt753x_pcs(pcs)->port;
2971 pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2973 state->link = (pmsr & PMSR_LINK);
2974 state->an_complete = state->link;
2975 state->duplex = !!(pmsr & PMSR_DPX);
2977 switch (pmsr & PMSR_SPEED_MASK) {
2979 state->speed = SPEED_10;
2981 case PMSR_SPEED_100:
2982 state->speed = SPEED_100;
2984 case PMSR_SPEED_1000:
2985 state->speed = SPEED_1000;
2988 state->speed = SPEED_UNKNOWN;
2992 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2993 if (pmsr & PMSR_RX_FC)
2994 state->pause |= MLO_PAUSE_RX;
2995 if (pmsr & PMSR_TX_FC)
2996 state->pause |= MLO_PAUSE_TX;
2999 static int mt753x_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
3000 phy_interface_t interface,
3001 const unsigned long *advertising,
3002 bool permit_pause_to_mac)
3007 static void mt7530_pcs_an_restart(struct phylink_pcs *pcs)
3011 static const struct phylink_pcs_ops mt7530_pcs_ops = {
3012 .pcs_validate = mt753x_pcs_validate,
3013 .pcs_get_state = mt7530_pcs_get_state,
3014 .pcs_config = mt753x_pcs_config,
3015 .pcs_an_restart = mt7530_pcs_an_restart,
3019 mt753x_setup(struct dsa_switch *ds)
3021 struct mt7530_priv *priv = ds->priv;
3022 int ret = priv->info->sw_setup(ds);
3028 ret = mt7530_setup_irq(priv);
3032 ret = mt7530_setup_mdio(priv);
3033 if (ret && priv->irq)
3034 mt7530_free_irq_common(priv);
3038 /* Initialise the PCS devices */
3039 for (i = 0; i < priv->ds->num_ports; i++) {
3040 priv->pcs[i].pcs.ops = priv->info->pcs_ops;
3041 priv->pcs[i].pcs.neg_mode = true;
3042 priv->pcs[i].priv = priv;
3043 priv->pcs[i].port = i;
3046 if (priv->create_sgmii) {
3047 ret = priv->create_sgmii(priv);
3048 if (ret && priv->irq)
3049 mt7530_free_irq(priv);
3055 static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
3056 struct ethtool_keee *e)
3058 struct mt7530_priv *priv = ds->priv;
3059 u32 eeecr = mt7530_read(priv, MT753X_PMEEECR_P(port));
3061 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
3062 e->tx_lpi_timer = LPI_THRESH_GET(eeecr);
3067 static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
3068 struct ethtool_keee *e)
3070 struct mt7530_priv *priv = ds->priv;
3071 u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
3073 if (e->tx_lpi_timer > 0xFFF)
3076 set = LPI_THRESH_SET(e->tx_lpi_timer);
3077 if (!e->tx_lpi_enabled)
3078 /* Force LPI Mode without a delay */
3080 mt7530_rmw(priv, MT753X_PMEEECR_P(port), mask, set);
3086 mt753x_conduit_state_change(struct dsa_switch *ds,
3087 const struct net_device *conduit,
3090 struct dsa_port *cpu_dp = conduit->dsa_ptr;
3091 struct mt7530_priv *priv = ds->priv;
3095 /* Set the CPU port to trap frames to for MT7530. Trapped frames will be
3096 * forwarded to the numerically smallest CPU port whose conduit
3099 if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
3102 mask = BIT(cpu_dp->index);
3105 priv->active_cpu_ports |= mask;
3107 priv->active_cpu_ports &= ~mask;
3109 if (priv->active_cpu_ports) {
3110 val = MT7530_CPU_EN |
3111 MT7530_CPU_PORT(__ffs(priv->active_cpu_ports));
3114 mt7530_rmw(priv, MT753X_MFC, MT7530_CPU_EN | MT7530_CPU_PORT_MASK, val);
3117 static int mt7988_setup(struct dsa_switch *ds)
3119 struct mt7530_priv *priv = ds->priv;
3121 /* Reset the switch */
3122 reset_control_assert(priv->rstc);
3123 usleep_range(20, 50);
3124 reset_control_deassert(priv->rstc);
3125 usleep_range(20, 50);
3127 /* Reset the switch PHYs */
3128 mt7530_write(priv, MT7530_SYS_CTRL, SYS_CTRL_PHY_RST);
3130 return mt7531_setup_common(ds);
3133 const struct dsa_switch_ops mt7530_switch_ops = {
3134 .get_tag_protocol = mtk_get_tag_protocol,
3135 .setup = mt753x_setup,
3136 .preferred_default_local_cpu_port = mt753x_preferred_default_local_cpu_port,
3137 .get_strings = mt7530_get_strings,
3138 .get_ethtool_stats = mt7530_get_ethtool_stats,
3139 .get_sset_count = mt7530_get_sset_count,
3140 .set_ageing_time = mt7530_set_ageing_time,
3141 .port_enable = mt7530_port_enable,
3142 .port_disable = mt7530_port_disable,
3143 .port_change_mtu = mt7530_port_change_mtu,
3144 .port_max_mtu = mt7530_port_max_mtu,
3145 .port_stp_state_set = mt7530_stp_state_set,
3146 .port_pre_bridge_flags = mt7530_port_pre_bridge_flags,
3147 .port_bridge_flags = mt7530_port_bridge_flags,
3148 .port_bridge_join = mt7530_port_bridge_join,
3149 .port_bridge_leave = mt7530_port_bridge_leave,
3150 .port_fdb_add = mt7530_port_fdb_add,
3151 .port_fdb_del = mt7530_port_fdb_del,
3152 .port_fdb_dump = mt7530_port_fdb_dump,
3153 .port_mdb_add = mt7530_port_mdb_add,
3154 .port_mdb_del = mt7530_port_mdb_del,
3155 .port_vlan_filtering = mt7530_port_vlan_filtering,
3156 .port_vlan_add = mt7530_port_vlan_add,
3157 .port_vlan_del = mt7530_port_vlan_del,
3158 .port_mirror_add = mt753x_port_mirror_add,
3159 .port_mirror_del = mt753x_port_mirror_del,
3160 .phylink_get_caps = mt753x_phylink_get_caps,
3161 .get_mac_eee = mt753x_get_mac_eee,
3162 .set_mac_eee = mt753x_set_mac_eee,
3163 .conduit_state_change = mt753x_conduit_state_change,
3165 EXPORT_SYMBOL_GPL(mt7530_switch_ops);
3167 static const struct phylink_mac_ops mt753x_phylink_mac_ops = {
3168 .mac_select_pcs = mt753x_phylink_mac_select_pcs,
3169 .mac_config = mt753x_phylink_mac_config,
3170 .mac_link_down = mt753x_phylink_mac_link_down,
3171 .mac_link_up = mt753x_phylink_mac_link_up,
3174 const struct mt753x_info mt753x_table[] = {
3177 .pcs_ops = &mt7530_pcs_ops,
3178 .sw_setup = mt7530_setup,
3179 .phy_read_c22 = mt7530_phy_read_c22,
3180 .phy_write_c22 = mt7530_phy_write_c22,
3181 .phy_read_c45 = mt7530_phy_read_c45,
3182 .phy_write_c45 = mt7530_phy_write_c45,
3183 .mac_port_get_caps = mt7530_mac_port_get_caps,
3184 .mac_port_config = mt7530_mac_config,
3188 .pcs_ops = &mt7530_pcs_ops,
3189 .sw_setup = mt7530_setup,
3190 .phy_read_c22 = mt7530_phy_read_c22,
3191 .phy_write_c22 = mt7530_phy_write_c22,
3192 .phy_read_c45 = mt7530_phy_read_c45,
3193 .phy_write_c45 = mt7530_phy_write_c45,
3194 .mac_port_get_caps = mt7530_mac_port_get_caps,
3195 .mac_port_config = mt7530_mac_config,
3199 .pcs_ops = &mt7530_pcs_ops,
3200 .sw_setup = mt7531_setup,
3201 .phy_read_c22 = mt7531_ind_c22_phy_read,
3202 .phy_write_c22 = mt7531_ind_c22_phy_write,
3203 .phy_read_c45 = mt7531_ind_c45_phy_read,
3204 .phy_write_c45 = mt7531_ind_c45_phy_write,
3205 .mac_port_get_caps = mt7531_mac_port_get_caps,
3206 .mac_port_config = mt7531_mac_config,
3210 .pcs_ops = &mt7530_pcs_ops,
3211 .sw_setup = mt7988_setup,
3212 .phy_read_c22 = mt7531_ind_c22_phy_read,
3213 .phy_write_c22 = mt7531_ind_c22_phy_write,
3214 .phy_read_c45 = mt7531_ind_c45_phy_read,
3215 .phy_write_c45 = mt7531_ind_c45_phy_write,
3216 .mac_port_get_caps = mt7988_mac_port_get_caps,
3219 EXPORT_SYMBOL_GPL(mt753x_table);
3222 mt7530_probe_common(struct mt7530_priv *priv)
3224 struct device *dev = priv->dev;
3226 priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
3230 priv->ds->dev = dev;
3231 priv->ds->num_ports = MT7530_NUM_PORTS;
3233 /* Get the hardware identifier from the devicetree node.
3234 * We will need it for some of the clock and regulator setup.
3236 priv->info = of_device_get_match_data(dev);
3240 priv->id = priv->info->id;
3242 priv->ds->priv = priv;
3243 priv->ds->ops = &mt7530_switch_ops;
3244 priv->ds->phylink_mac_ops = &mt753x_phylink_mac_ops;
3245 mutex_init(&priv->reg_mutex);
3246 dev_set_drvdata(dev, priv);
3250 EXPORT_SYMBOL_GPL(mt7530_probe_common);
3253 mt7530_remove_common(struct mt7530_priv *priv)
3256 mt7530_free_irq(priv);
3258 dsa_unregister_switch(priv->ds);
3260 mutex_destroy(&priv->reg_mutex);
3262 EXPORT_SYMBOL_GPL(mt7530_remove_common);
3265 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
3266 MODULE_LICENSE("GPL");