1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mediatek MT7530 DSA Switch driver
4 * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
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"),
77 /* Since phy_device has not yet been created and
78 * phy_{read,write}_mmd_indirect is not available, we provide our own
79 * core_{read,write}_mmd_indirect with core_{clear,write,set} wrappers
80 * to complete this function.
83 core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
85 struct mii_bus *bus = priv->bus;
88 /* Write the desired MMD Devad */
89 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
93 /* Write the desired MMD register address */
94 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
98 /* Select the Function : DATA with no post increment */
99 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
103 /* Read the content of the MMD's selected register */
104 value = bus->read(bus, 0, MII_MMD_DATA);
108 dev_err(&bus->dev, "failed to read mmd register\n");
114 core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
117 struct mii_bus *bus = priv->bus;
120 /* Write the desired MMD Devad */
121 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
125 /* Write the desired MMD register address */
126 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
130 /* Select the Function : DATA with no post increment */
131 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
135 /* Write the data into MMD's selected register */
136 ret = bus->write(bus, 0, MII_MMD_DATA, data);
140 "failed to write mmd register\n");
145 core_write(struct mt7530_priv *priv, u32 reg, u32 val)
147 struct mii_bus *bus = priv->bus;
149 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
151 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
153 mutex_unlock(&bus->mdio_lock);
157 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
159 struct mii_bus *bus = priv->bus;
162 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
164 val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
167 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
169 mutex_unlock(&bus->mdio_lock);
173 core_set(struct mt7530_priv *priv, u32 reg, u32 val)
175 core_rmw(priv, reg, 0, val);
179 core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
181 core_rmw(priv, reg, val, 0);
185 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
187 struct mii_bus *bus = priv->bus;
191 page = (reg >> 6) & 0x3ff;
192 r = (reg >> 2) & 0xf;
196 /* MT7530 uses 31 as the pseudo port */
197 ret = bus->write(bus, 0x1f, 0x1f, page);
201 ret = bus->write(bus, 0x1f, r, lo);
205 ret = bus->write(bus, 0x1f, 0x10, hi);
209 "failed to write mt7530 register\n");
214 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
216 struct mii_bus *bus = priv->bus;
220 page = (reg >> 6) & 0x3ff;
221 r = (reg >> 2) & 0xf;
223 /* MT7530 uses 31 as the pseudo port */
224 ret = bus->write(bus, 0x1f, 0x1f, page);
227 "failed to read mt7530 register\n");
231 lo = bus->read(bus, 0x1f, r);
232 hi = bus->read(bus, 0x1f, 0x10);
234 return (hi << 16) | (lo & 0xffff);
238 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
240 struct mii_bus *bus = priv->bus;
242 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
244 mt7530_mii_write(priv, reg, val);
246 mutex_unlock(&bus->mdio_lock);
250 _mt7530_unlocked_read(struct mt7530_dummy_poll *p)
252 return mt7530_mii_read(p->priv, p->reg);
256 _mt7530_read(struct mt7530_dummy_poll *p)
258 struct mii_bus *bus = p->priv->bus;
261 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
263 val = mt7530_mii_read(p->priv, p->reg);
265 mutex_unlock(&bus->mdio_lock);
271 mt7530_read(struct mt7530_priv *priv, u32 reg)
273 struct mt7530_dummy_poll p;
275 INIT_MT7530_DUMMY_POLL(&p, priv, reg);
276 return _mt7530_read(&p);
280 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
283 struct mii_bus *bus = priv->bus;
286 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
288 val = mt7530_mii_read(priv, reg);
291 mt7530_mii_write(priv, reg, val);
293 mutex_unlock(&bus->mdio_lock);
297 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
299 mt7530_rmw(priv, reg, 0, val);
303 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
305 mt7530_rmw(priv, reg, val, 0);
309 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
313 struct mt7530_dummy_poll p;
315 /* Set the command operating upon the MAC address entries */
316 val = ATC_BUSY | ATC_MAT(0) | cmd;
317 mt7530_write(priv, MT7530_ATC, val);
319 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
320 ret = readx_poll_timeout(_mt7530_read, &p, val,
321 !(val & ATC_BUSY), 20, 20000);
323 dev_err(priv->dev, "reset timeout\n");
327 /* Additional sanity for read command if the specified
330 val = mt7530_read(priv, MT7530_ATC);
331 if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
341 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
346 /* Read from ARL table into an array */
347 for (i = 0; i < 3; i++) {
348 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
350 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
351 __func__, __LINE__, i, reg[i]);
354 fdb->vid = (reg[1] >> CVID) & CVID_MASK;
355 fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
356 fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
357 fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
358 fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
359 fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
360 fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
361 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
362 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
363 fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
367 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
368 u8 port_mask, const u8 *mac,
374 reg[1] |= vid & CVID_MASK;
376 reg[1] |= ATA2_FID(FID_BRIDGED);
377 reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
378 reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
379 /* STATIC_ENT indicate that entry is static wouldn't
380 * be aged out and STATIC_EMP specified as erasing an
383 reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
384 reg[1] |= mac[5] << MAC_BYTE_5;
385 reg[1] |= mac[4] << MAC_BYTE_4;
386 reg[0] |= mac[3] << MAC_BYTE_3;
387 reg[0] |= mac[2] << MAC_BYTE_2;
388 reg[0] |= mac[1] << MAC_BYTE_1;
389 reg[0] |= mac[0] << MAC_BYTE_0;
391 /* Write array into the ARL table */
392 for (i = 0; i < 3; i++)
393 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
396 /* Set up switch core clock for MT7530 */
397 static void mt7530_pll_setup(struct mt7530_priv *priv)
399 /* Disable core clock */
400 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
403 core_write(priv, CORE_GSWPLL_GRP1, 0);
405 /* Set core clock into 500Mhz */
406 core_write(priv, CORE_GSWPLL_GRP2,
407 RG_GSWPLL_POSDIV_500M(1) |
408 RG_GSWPLL_FBKDIV_500M(25));
411 core_write(priv, CORE_GSWPLL_GRP1,
413 RG_GSWPLL_POSDIV_200M(2) |
414 RG_GSWPLL_FBKDIV_200M(32));
418 /* Enable core clock */
419 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
422 /* Setup port 6 interface mode and TRGMII TX circuit */
424 mt7530_pad_clk_setup(struct dsa_switch *ds, phy_interface_t interface)
426 struct mt7530_priv *priv = ds->priv;
427 u32 ncpo1, ssc_delta, trgint, xtal;
429 xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK;
431 if (xtal == HWTRAP_XTAL_20MHZ) {
433 "%s: MT7530 with a 20MHz XTAL is not supported!\n",
439 case PHY_INTERFACE_MODE_RGMII:
442 case PHY_INTERFACE_MODE_TRGMII:
444 if (xtal == HWTRAP_XTAL_25MHZ)
448 if (priv->id == ID_MT7621) {
449 /* PLL frequency: 125MHz: 1.0GBit */
450 if (xtal == HWTRAP_XTAL_40MHZ)
452 if (xtal == HWTRAP_XTAL_25MHZ)
454 } else { /* PLL frequency: 250MHz: 2.0Gbit */
455 if (xtal == HWTRAP_XTAL_40MHZ)
457 if (xtal == HWTRAP_XTAL_25MHZ)
462 dev_err(priv->dev, "xMII interface %d not supported\n",
467 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
468 P6_INTF_MODE(trgint));
471 /* Disable the MT7530 TRGMII clocks */
472 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
474 /* Setup the MT7530 TRGMII Tx Clock */
475 core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
476 core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
477 core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
478 core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
479 core_write(priv, CORE_PLL_GROUP4,
480 RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
481 RG_SYSPLL_BIAS_LPF_EN);
482 core_write(priv, CORE_PLL_GROUP2,
483 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
484 RG_SYSPLL_POSDIV(1));
485 core_write(priv, CORE_PLL_GROUP7,
486 RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
487 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
489 /* Enable the MT7530 TRGMII clocks */
490 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
496 static bool mt7531_dual_sgmii_supported(struct mt7530_priv *priv)
500 val = mt7530_read(priv, MT7531_TOP_SIG_SR);
502 return (val & PAD_DUAL_SGMII_EN) != 0;
506 mt7531_pad_setup(struct dsa_switch *ds, phy_interface_t interface)
512 mt7531_pll_setup(struct mt7530_priv *priv)
519 if (mt7531_dual_sgmii_supported(priv))
522 val = mt7530_read(priv, MT7531_CREV);
523 top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR);
524 hwstrap = mt7530_read(priv, MT7531_HWTRAP);
525 if ((val & CHIP_REV_M) > 0)
526 xtal = (top_sig & PAD_MCM_SMI_EN) ? HWTRAP_XTAL_FSEL_40MHZ :
527 HWTRAP_XTAL_FSEL_25MHZ;
529 xtal = hwstrap & HWTRAP_XTAL_FSEL_MASK;
531 /* Step 1 : Disable MT7531 COREPLL */
532 val = mt7530_read(priv, MT7531_PLLGP_EN);
534 mt7530_write(priv, MT7531_PLLGP_EN, val);
536 /* Step 2: switch to XTAL output */
537 val = mt7530_read(priv, MT7531_PLLGP_EN);
539 mt7530_write(priv, MT7531_PLLGP_EN, val);
541 val = mt7530_read(priv, MT7531_PLLGP_CR0);
542 val &= ~RG_COREPLL_EN;
543 mt7530_write(priv, MT7531_PLLGP_CR0, val);
545 /* Step 3: disable PLLGP and enable program PLLGP */
546 val = mt7530_read(priv, MT7531_PLLGP_EN);
548 mt7530_write(priv, MT7531_PLLGP_EN, val);
550 /* Step 4: program COREPLL output frequency to 500MHz */
551 val = mt7530_read(priv, MT7531_PLLGP_CR0);
552 val &= ~RG_COREPLL_POSDIV_M;
553 val |= 2 << RG_COREPLL_POSDIV_S;
554 mt7530_write(priv, MT7531_PLLGP_CR0, val);
555 usleep_range(25, 35);
558 case HWTRAP_XTAL_FSEL_25MHZ:
559 val = mt7530_read(priv, MT7531_PLLGP_CR0);
560 val &= ~RG_COREPLL_SDM_PCW_M;
561 val |= 0x140000 << RG_COREPLL_SDM_PCW_S;
562 mt7530_write(priv, MT7531_PLLGP_CR0, val);
564 case HWTRAP_XTAL_FSEL_40MHZ:
565 val = mt7530_read(priv, MT7531_PLLGP_CR0);
566 val &= ~RG_COREPLL_SDM_PCW_M;
567 val |= 0x190000 << RG_COREPLL_SDM_PCW_S;
568 mt7530_write(priv, MT7531_PLLGP_CR0, val);
572 /* Set feedback divide ratio update signal to high */
573 val = mt7530_read(priv, MT7531_PLLGP_CR0);
574 val |= RG_COREPLL_SDM_PCW_CHG;
575 mt7530_write(priv, MT7531_PLLGP_CR0, val);
576 /* Wait for at least 16 XTAL clocks */
577 usleep_range(10, 20);
579 /* Step 5: set feedback divide ratio update signal to low */
580 val = mt7530_read(priv, MT7531_PLLGP_CR0);
581 val &= ~RG_COREPLL_SDM_PCW_CHG;
582 mt7530_write(priv, MT7531_PLLGP_CR0, val);
584 /* Enable 325M clock for SGMII */
585 mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
587 /* Enable 250SSC clock for RGMII */
588 mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
590 /* Step 6: Enable MT7531 PLL */
591 val = mt7530_read(priv, MT7531_PLLGP_CR0);
592 val |= RG_COREPLL_EN;
593 mt7530_write(priv, MT7531_PLLGP_CR0, val);
595 val = mt7530_read(priv, MT7531_PLLGP_EN);
597 mt7530_write(priv, MT7531_PLLGP_EN, val);
598 usleep_range(25, 35);
602 mt7530_mib_reset(struct dsa_switch *ds)
604 struct mt7530_priv *priv = ds->priv;
606 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
607 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
610 static int mt7530_phy_read(struct mt7530_priv *priv, int port, int regnum)
612 return mdiobus_read_nested(priv->bus, port, regnum);
615 static int mt7530_phy_write(struct mt7530_priv *priv, int port, int regnum,
618 return mdiobus_write_nested(priv->bus, port, regnum, val);
622 mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad,
625 struct mii_bus *bus = priv->bus;
626 struct mt7530_dummy_poll p;
630 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
632 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
634 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
635 !(val & MT7531_PHY_ACS_ST), 20, 100000);
637 dev_err(priv->dev, "poll timeout\n");
641 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
642 MT7531_MDIO_DEV_ADDR(devad) | regnum;
643 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
645 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
646 !(val & MT7531_PHY_ACS_ST), 20, 100000);
648 dev_err(priv->dev, "poll timeout\n");
652 reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) |
653 MT7531_MDIO_DEV_ADDR(devad);
654 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
656 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
657 !(val & MT7531_PHY_ACS_ST), 20, 100000);
659 dev_err(priv->dev, "poll timeout\n");
663 ret = val & MT7531_MDIO_RW_DATA_MASK;
665 mutex_unlock(&bus->mdio_lock);
671 mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad,
672 int regnum, u32 data)
674 struct mii_bus *bus = priv->bus;
675 struct mt7530_dummy_poll p;
679 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
681 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
683 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
684 !(val & MT7531_PHY_ACS_ST), 20, 100000);
686 dev_err(priv->dev, "poll timeout\n");
690 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
691 MT7531_MDIO_DEV_ADDR(devad) | regnum;
692 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | 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 reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) |
702 MT7531_MDIO_DEV_ADDR(devad) | data;
703 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
705 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
706 !(val & MT7531_PHY_ACS_ST), 20, 100000);
708 dev_err(priv->dev, "poll timeout\n");
713 mutex_unlock(&bus->mdio_lock);
719 mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum)
721 struct mii_bus *bus = priv->bus;
722 struct mt7530_dummy_poll p;
726 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
728 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
730 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
731 !(val & MT7531_PHY_ACS_ST), 20, 100000);
733 dev_err(priv->dev, "poll timeout\n");
737 val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) |
738 MT7531_MDIO_REG_ADDR(regnum);
740 mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST);
742 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
743 !(val & MT7531_PHY_ACS_ST), 20, 100000);
745 dev_err(priv->dev, "poll timeout\n");
749 ret = val & MT7531_MDIO_RW_DATA_MASK;
751 mutex_unlock(&bus->mdio_lock);
757 mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum,
760 struct mii_bus *bus = priv->bus;
761 struct mt7530_dummy_poll p;
765 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
767 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
769 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
770 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
772 dev_err(priv->dev, "poll timeout\n");
776 reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) |
777 MT7531_MDIO_REG_ADDR(regnum) | data;
779 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
781 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
782 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
784 dev_err(priv->dev, "poll timeout\n");
789 mutex_unlock(&bus->mdio_lock);
795 mt7531_ind_phy_read(struct mt7530_priv *priv, int port, int regnum)
800 if (regnum & MII_ADDR_C45) {
801 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
802 ret = mt7531_ind_c45_phy_read(priv, port, devad,
803 regnum & MII_REGADDR_C45_MASK);
805 ret = mt7531_ind_c22_phy_read(priv, port, regnum);
812 mt7531_ind_phy_write(struct mt7530_priv *priv, int port, int regnum,
818 if (regnum & MII_ADDR_C45) {
819 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
820 ret = mt7531_ind_c45_phy_write(priv, port, devad,
821 regnum & MII_REGADDR_C45_MASK,
824 ret = mt7531_ind_c22_phy_write(priv, port, regnum, data);
831 mt753x_phy_read(struct mii_bus *bus, int port, int regnum)
833 struct mt7530_priv *priv = bus->priv;
835 return priv->info->phy_read(priv, port, regnum);
839 mt753x_phy_write(struct mii_bus *bus, int port, int regnum, u16 val)
841 struct mt7530_priv *priv = bus->priv;
843 return priv->info->phy_write(priv, port, regnum, val);
847 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
852 if (stringset != ETH_SS_STATS)
855 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
856 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
861 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
864 struct mt7530_priv *priv = ds->priv;
865 const struct mt7530_mib_desc *mib;
869 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
870 mib = &mt7530_mib[i];
871 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
873 data[i] = mt7530_read(priv, reg);
874 if (mib->size == 2) {
875 hi = mt7530_read(priv, reg + 4);
882 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
884 if (sset != ETH_SS_STATS)
887 return ARRAY_SIZE(mt7530_mib);
891 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
893 struct mt7530_priv *priv = ds->priv;
894 unsigned int secs = msecs / 1000;
895 unsigned int tmp_age_count;
896 unsigned int error = -1;
897 unsigned int age_count;
898 unsigned int age_unit;
900 /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
901 if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
904 /* iterate through all possible age_count to find the closest pair */
905 for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
906 unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
908 if (tmp_age_unit <= AGE_UNIT_MAX) {
909 unsigned int tmp_error = secs -
910 (tmp_age_count + 1) * (tmp_age_unit + 1);
912 /* found a closer pair */
913 if (error > tmp_error) {
915 age_count = tmp_age_count;
916 age_unit = tmp_age_unit;
919 /* found the exact match, so break the loop */
925 mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
930 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
932 struct mt7530_priv *priv = ds->priv;
936 mutex_lock(&priv->reg_mutex);
938 val = mt7530_read(priv, MT7530_MHWTRAP);
940 val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
941 val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
943 switch (priv->p5_intf_sel) {
944 case P5_INTF_SEL_PHY_P0:
945 /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
946 val |= MHWTRAP_PHY0_SEL;
948 case P5_INTF_SEL_PHY_P4:
949 /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
950 val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
952 /* Setup the MAC by default for the cpu port */
953 mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
955 case P5_INTF_SEL_GMAC5:
956 /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
957 val &= ~MHWTRAP_P5_DIS;
960 interface = PHY_INTERFACE_MODE_NA;
963 dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
968 /* Setup RGMII settings */
969 if (phy_interface_mode_is_rgmii(interface)) {
970 val |= MHWTRAP_P5_RGMII_MODE;
972 /* P5 RGMII RX Clock Control: delay setting for 1000M */
973 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
975 /* Don't set delay in DSA mode */
976 if (!dsa_is_dsa_port(priv->ds, 5) &&
977 (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
978 interface == PHY_INTERFACE_MODE_RGMII_ID))
979 tx_delay = 4; /* n * 0.5 ns */
981 /* P5 RGMII TX Clock Control: delay x */
982 mt7530_write(priv, MT7530_P5RGMIITXCR,
983 CSR_RGMII_TXC_CFG(0x10 + tx_delay));
985 /* reduce P5 RGMII Tx driving, 8mA */
986 mt7530_write(priv, MT7530_IO_DRV_CR,
987 P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
990 mt7530_write(priv, MT7530_MHWTRAP, val);
992 dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
993 val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
995 priv->p5_interface = interface;
998 mutex_unlock(&priv->reg_mutex);
1002 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
1004 struct mt7530_priv *priv = ds->priv;
1007 /* Setup max capability of CPU port at first */
1008 if (priv->info->cpu_port_config) {
1009 ret = priv->info->cpu_port_config(ds, port);
1014 /* Enable Mediatek header mode on the cpu port */
1015 mt7530_write(priv, MT7530_PVC_P(port),
1018 /* Enable flooding on the CPU port */
1019 mt7530_set(priv, MT7530_MFC, BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) |
1020 UNU_FFP(BIT(port)));
1022 /* Set CPU port number */
1023 if (priv->id == ID_MT7530 || priv->id == ID_MT7621)
1024 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
1026 /* CPU port gets connected to all user ports of
1029 mt7530_write(priv, MT7530_PCR_P(port),
1030 PCR_MATRIX(dsa_user_ports(priv->ds)));
1032 /* Set to fallback mode for independent VLAN learning */
1033 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1034 MT7530_PORT_FALLBACK_MODE);
1040 mt7530_port_enable(struct dsa_switch *ds, int port,
1041 struct phy_device *phy)
1043 struct dsa_port *dp = dsa_to_port(ds, port);
1044 struct mt7530_priv *priv = ds->priv;
1046 mutex_lock(&priv->reg_mutex);
1048 /* Allow the user port gets connected to the cpu port and also
1049 * restore the port matrix if the port is the member of a certain
1052 if (dsa_port_is_user(dp)) {
1053 struct dsa_port *cpu_dp = dp->cpu_dp;
1055 priv->ports[port].pm |= PCR_MATRIX(BIT(cpu_dp->index));
1057 priv->ports[port].enable = true;
1058 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1059 priv->ports[port].pm);
1060 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1062 mutex_unlock(&priv->reg_mutex);
1068 mt7530_port_disable(struct dsa_switch *ds, int port)
1070 struct mt7530_priv *priv = ds->priv;
1072 mutex_lock(&priv->reg_mutex);
1074 /* Clear up all port matrix which could be restored in the next
1075 * enablement for the port.
1077 priv->ports[port].enable = false;
1078 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1080 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1082 mutex_unlock(&priv->reg_mutex);
1086 mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1088 struct mt7530_priv *priv = ds->priv;
1089 struct mii_bus *bus = priv->bus;
1093 /* When a new MTU is set, DSA always set the CPU port's MTU to the
1094 * largest MTU of the slave ports. Because the switch only has a global
1095 * RX length register, only allowing CPU port here is enough.
1097 if (!dsa_is_cpu_port(ds, port))
1100 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1102 val = mt7530_mii_read(priv, MT7530_GMACCR);
1103 val &= ~MAX_RX_PKT_LEN_MASK;
1105 /* RX length also includes Ethernet header, MTK tag, and FCS length */
1106 length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
1107 if (length <= 1522) {
1108 val |= MAX_RX_PKT_LEN_1522;
1109 } else if (length <= 1536) {
1110 val |= MAX_RX_PKT_LEN_1536;
1111 } else if (length <= 1552) {
1112 val |= MAX_RX_PKT_LEN_1552;
1114 val &= ~MAX_RX_JUMBO_MASK;
1115 val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
1116 val |= MAX_RX_PKT_LEN_JUMBO;
1119 mt7530_mii_write(priv, MT7530_GMACCR, val);
1121 mutex_unlock(&bus->mdio_lock);
1127 mt7530_port_max_mtu(struct dsa_switch *ds, int port)
1129 return MT7530_MAX_MTU;
1133 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1135 struct mt7530_priv *priv = ds->priv;
1139 case BR_STATE_DISABLED:
1140 stp_state = MT7530_STP_DISABLED;
1142 case BR_STATE_BLOCKING:
1143 stp_state = MT7530_STP_BLOCKING;
1145 case BR_STATE_LISTENING:
1146 stp_state = MT7530_STP_LISTENING;
1148 case BR_STATE_LEARNING:
1149 stp_state = MT7530_STP_LEARNING;
1151 case BR_STATE_FORWARDING:
1153 stp_state = MT7530_STP_FORWARDING;
1157 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK(FID_BRIDGED),
1158 FID_PST(FID_BRIDGED, stp_state));
1162 mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1163 struct switchdev_brport_flags flags,
1164 struct netlink_ext_ack *extack)
1166 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1174 mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
1175 struct switchdev_brport_flags flags,
1176 struct netlink_ext_ack *extack)
1178 struct mt7530_priv *priv = ds->priv;
1180 if (flags.mask & BR_LEARNING)
1181 mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
1182 flags.val & BR_LEARNING ? 0 : SA_DIS);
1184 if (flags.mask & BR_FLOOD)
1185 mt7530_rmw(priv, MT7530_MFC, UNU_FFP(BIT(port)),
1186 flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
1188 if (flags.mask & BR_MCAST_FLOOD)
1189 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1190 flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
1192 if (flags.mask & BR_BCAST_FLOOD)
1193 mt7530_rmw(priv, MT7530_MFC, BC_FFP(BIT(port)),
1194 flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
1200 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1201 struct dsa_bridge bridge, bool *tx_fwd_offload,
1202 struct netlink_ext_ack *extack)
1204 struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1205 struct dsa_port *cpu_dp = dp->cpu_dp;
1206 u32 port_bitmap = BIT(cpu_dp->index);
1207 struct mt7530_priv *priv = ds->priv;
1209 mutex_lock(&priv->reg_mutex);
1211 dsa_switch_for_each_user_port(other_dp, ds) {
1212 int other_port = other_dp->index;
1217 /* Add this port to the port matrix of the other ports in the
1218 * same bridge. If the port is disabled, port matrix is kept
1219 * and not being setup until the port becomes enabled.
1221 if (!dsa_port_offloads_bridge(other_dp, &bridge))
1224 if (priv->ports[other_port].enable)
1225 mt7530_set(priv, MT7530_PCR_P(other_port),
1226 PCR_MATRIX(BIT(port)));
1227 priv->ports[other_port].pm |= PCR_MATRIX(BIT(port));
1229 port_bitmap |= BIT(other_port);
1232 /* Add the all other ports to this port matrix. */
1233 if (priv->ports[port].enable)
1234 mt7530_rmw(priv, MT7530_PCR_P(port),
1235 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1236 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1238 /* Set to fallback mode for independent VLAN learning */
1239 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1240 MT7530_PORT_FALLBACK_MODE);
1242 mutex_unlock(&priv->reg_mutex);
1248 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1250 struct mt7530_priv *priv = ds->priv;
1251 bool all_user_ports_removed = true;
1254 /* This is called after .port_bridge_leave when leaving a VLAN-aware
1255 * bridge. Don't set standalone ports to fallback mode.
1257 if (dsa_port_bridge_dev_get(dsa_to_port(ds, port)))
1258 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1259 MT7530_PORT_FALLBACK_MODE);
1261 mt7530_rmw(priv, MT7530_PVC_P(port),
1262 VLAN_ATTR_MASK | PVC_EG_TAG_MASK | ACC_FRM_MASK,
1263 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1264 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT) |
1265 MT7530_VLAN_ACC_ALL);
1268 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1271 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1272 if (dsa_is_user_port(ds, i) &&
1273 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1274 all_user_ports_removed = false;
1279 /* CPU port also does the same thing until all user ports belonging to
1280 * the CPU port get out of VLAN filtering mode.
1282 if (all_user_ports_removed) {
1283 struct dsa_port *dp = dsa_to_port(ds, port);
1284 struct dsa_port *cpu_dp = dp->cpu_dp;
1286 mt7530_write(priv, MT7530_PCR_P(cpu_dp->index),
1287 PCR_MATRIX(dsa_user_ports(priv->ds)));
1288 mt7530_write(priv, MT7530_PVC_P(cpu_dp->index), PORT_SPEC_TAG
1289 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1294 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1296 struct mt7530_priv *priv = ds->priv;
1298 /* Trapped into security mode allows packet forwarding through VLAN
1301 if (dsa_is_user_port(ds, port)) {
1302 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1303 MT7530_PORT_SECURITY_MODE);
1304 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1305 G0_PORT_VID(priv->ports[port].pvid));
1307 /* Only accept tagged frames if PVID is not set */
1308 if (!priv->ports[port].pvid)
1309 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1310 MT7530_VLAN_ACC_TAGGED);
1312 /* Set the port as a user port which is to be able to recognize
1313 * VID from incoming packets before fetching entry within the
1316 mt7530_rmw(priv, MT7530_PVC_P(port),
1317 VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1318 VLAN_ATTR(MT7530_VLAN_USER) |
1319 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
1321 /* Also set CPU ports to the "user" VLAN port attribute, to
1322 * allow VLAN classification, but keep the EG_TAG attribute as
1323 * "consistent" (i.o.w. don't change its value) for packets
1324 * received by the switch from the CPU, so that tagged packets
1325 * are forwarded to user ports as tagged, and untagged as
1328 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
1329 VLAN_ATTR(MT7530_VLAN_USER));
1334 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1335 struct dsa_bridge bridge)
1337 struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1338 struct dsa_port *cpu_dp = dp->cpu_dp;
1339 struct mt7530_priv *priv = ds->priv;
1341 mutex_lock(&priv->reg_mutex);
1343 dsa_switch_for_each_user_port(other_dp, ds) {
1344 int other_port = other_dp->index;
1349 /* Remove this port from the port matrix of the other ports
1350 * in the same bridge. If the port is disabled, port matrix
1351 * is kept and not being setup until the port becomes enabled.
1353 if (!dsa_port_offloads_bridge(other_dp, &bridge))
1356 if (priv->ports[other_port].enable)
1357 mt7530_clear(priv, MT7530_PCR_P(other_port),
1358 PCR_MATRIX(BIT(port)));
1359 priv->ports[other_port].pm &= ~PCR_MATRIX(BIT(port));
1362 /* Set the cpu port to be the only one in the port matrix of
1365 if (priv->ports[port].enable)
1366 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1367 PCR_MATRIX(BIT(cpu_dp->index)));
1368 priv->ports[port].pm = PCR_MATRIX(BIT(cpu_dp->index));
1370 /* When a port is removed from the bridge, the port would be set up
1371 * back to the default as is at initial boot which is a VLAN-unaware
1374 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1375 MT7530_PORT_MATRIX_MODE);
1377 mutex_unlock(&priv->reg_mutex);
1381 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1382 const unsigned char *addr, u16 vid,
1385 struct mt7530_priv *priv = ds->priv;
1387 u8 port_mask = BIT(port);
1389 mutex_lock(&priv->reg_mutex);
1390 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1391 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1392 mutex_unlock(&priv->reg_mutex);
1398 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1399 const unsigned char *addr, u16 vid,
1402 struct mt7530_priv *priv = ds->priv;
1404 u8 port_mask = BIT(port);
1406 mutex_lock(&priv->reg_mutex);
1407 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
1408 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1409 mutex_unlock(&priv->reg_mutex);
1415 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1416 dsa_fdb_dump_cb_t *cb, void *data)
1418 struct mt7530_priv *priv = ds->priv;
1419 struct mt7530_fdb _fdb = { 0 };
1420 int cnt = MT7530_NUM_FDB_RECORDS;
1424 mutex_lock(&priv->reg_mutex);
1426 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1431 if (rsp & ATC_SRCH_HIT) {
1432 mt7530_fdb_read(priv, &_fdb);
1433 if (_fdb.port_mask & BIT(port)) {
1434 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1441 !(rsp & ATC_SRCH_END) &&
1442 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1444 mutex_unlock(&priv->reg_mutex);
1450 mt7530_port_mdb_add(struct dsa_switch *ds, int port,
1451 const struct switchdev_obj_port_mdb *mdb,
1454 struct mt7530_priv *priv = ds->priv;
1455 const u8 *addr = mdb->addr;
1460 mutex_lock(&priv->reg_mutex);
1462 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1463 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1464 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1467 port_mask |= BIT(port);
1468 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1469 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1471 mutex_unlock(&priv->reg_mutex);
1477 mt7530_port_mdb_del(struct dsa_switch *ds, int port,
1478 const struct switchdev_obj_port_mdb *mdb,
1481 struct mt7530_priv *priv = ds->priv;
1482 const u8 *addr = mdb->addr;
1487 mutex_lock(&priv->reg_mutex);
1489 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1490 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1491 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1494 port_mask &= ~BIT(port);
1495 mt7530_fdb_write(priv, vid, port_mask, addr, -1,
1496 port_mask ? STATIC_ENT : STATIC_EMP);
1497 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1499 mutex_unlock(&priv->reg_mutex);
1505 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1507 struct mt7530_dummy_poll p;
1511 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1512 mt7530_write(priv, MT7530_VTCR, val);
1514 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1515 ret = readx_poll_timeout(_mt7530_read, &p, val,
1516 !(val & VTCR_BUSY), 20, 20000);
1518 dev_err(priv->dev, "poll timeout\n");
1522 val = mt7530_read(priv, MT7530_VTCR);
1523 if (val & VTCR_INVALID) {
1524 dev_err(priv->dev, "read VTCR invalid\n");
1532 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1533 struct netlink_ext_ack *extack)
1535 struct dsa_port *dp = dsa_to_port(ds, port);
1536 struct dsa_port *cpu_dp = dp->cpu_dp;
1538 if (vlan_filtering) {
1539 /* The port is being kept as VLAN-unaware port when bridge is
1540 * set up with vlan_filtering not being set, Otherwise, the
1541 * port and the corresponding CPU port is required the setup
1542 * for becoming a VLAN-aware port.
1544 mt7530_port_set_vlan_aware(ds, port);
1545 mt7530_port_set_vlan_aware(ds, cpu_dp->index);
1547 mt7530_port_set_vlan_unaware(ds, port);
1554 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1555 struct mt7530_hw_vlan_entry *entry)
1557 struct dsa_port *dp = dsa_to_port(priv->ds, entry->port);
1561 new_members = entry->old_members | BIT(entry->port);
1563 /* Validate the entry with independent learning, create egress tag per
1564 * VLAN and joining the port as one of the port members.
1566 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | FID(FID_BRIDGED) |
1568 mt7530_write(priv, MT7530_VAWD1, val);
1570 /* Decide whether adding tag or not for those outgoing packets from the
1571 * port inside the VLAN.
1572 * CPU port is always taken as a tagged port for serving more than one
1573 * VLANs across and also being applied with egress type stack mode for
1574 * that VLAN tags would be appended after hardware special tag used as
1577 if (dsa_port_is_cpu(dp))
1578 val = MT7530_VLAN_EGRESS_STACK;
1579 else if (entry->untagged)
1580 val = MT7530_VLAN_EGRESS_UNTAG;
1582 val = MT7530_VLAN_EGRESS_TAG;
1583 mt7530_rmw(priv, MT7530_VAWD2,
1584 ETAG_CTRL_P_MASK(entry->port),
1585 ETAG_CTRL_P(entry->port, val));
1589 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1590 struct mt7530_hw_vlan_entry *entry)
1595 new_members = entry->old_members & ~BIT(entry->port);
1597 val = mt7530_read(priv, MT7530_VAWD1);
1598 if (!(val & VLAN_VALID)) {
1600 "Cannot be deleted due to invalid entry\n");
1605 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1607 mt7530_write(priv, MT7530_VAWD1, val);
1609 mt7530_write(priv, MT7530_VAWD1, 0);
1610 mt7530_write(priv, MT7530_VAWD2, 0);
1615 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1616 struct mt7530_hw_vlan_entry *entry,
1617 mt7530_vlan_op vlan_op)
1622 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1624 val = mt7530_read(priv, MT7530_VAWD1);
1626 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1628 /* Manipulate entry */
1629 vlan_op(priv, entry);
1631 /* Flush result to hardware */
1632 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1636 mt7530_setup_vlan0(struct mt7530_priv *priv)
1640 /* Validate the entry with independent learning, keep the original
1641 * ingress tag attribute.
1643 val = IVL_MAC | EG_CON | PORT_MEM(MT7530_ALL_MEMBERS) | FID(FID_BRIDGED) |
1645 mt7530_write(priv, MT7530_VAWD1, val);
1647 return mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, 0);
1651 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1652 const struct switchdev_obj_port_vlan *vlan,
1653 struct netlink_ext_ack *extack)
1655 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1656 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1657 struct mt7530_hw_vlan_entry new_entry;
1658 struct mt7530_priv *priv = ds->priv;
1660 mutex_lock(&priv->reg_mutex);
1662 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1663 mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
1666 priv->ports[port].pvid = vlan->vid;
1668 /* Accept all frames if PVID is set */
1669 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1670 MT7530_VLAN_ACC_ALL);
1672 /* Only configure PVID if VLAN filtering is enabled */
1673 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1674 mt7530_rmw(priv, MT7530_PPBV1_P(port),
1676 G0_PORT_VID(vlan->vid));
1677 } else if (vlan->vid && priv->ports[port].pvid == vlan->vid) {
1678 /* This VLAN is overwritten without PVID, so unset it */
1679 priv->ports[port].pvid = G0_PORT_VID_DEF;
1681 /* Only accept tagged frames if the port is VLAN-aware */
1682 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1683 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1684 MT7530_VLAN_ACC_TAGGED);
1686 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1690 mutex_unlock(&priv->reg_mutex);
1696 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1697 const struct switchdev_obj_port_vlan *vlan)
1699 struct mt7530_hw_vlan_entry target_entry;
1700 struct mt7530_priv *priv = ds->priv;
1702 mutex_lock(&priv->reg_mutex);
1704 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1705 mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
1706 mt7530_hw_vlan_del);
1708 /* PVID is being restored to the default whenever the PVID port
1709 * is being removed from the VLAN.
1711 if (priv->ports[port].pvid == vlan->vid) {
1712 priv->ports[port].pvid = G0_PORT_VID_DEF;
1714 /* Only accept tagged frames if the port is VLAN-aware */
1715 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1716 mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
1717 MT7530_VLAN_ACC_TAGGED);
1719 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1724 mutex_unlock(&priv->reg_mutex);
1729 static int mt753x_mirror_port_get(unsigned int id, u32 val)
1731 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) :
1735 static int mt753x_mirror_port_set(unsigned int id, u32 val)
1737 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) :
1741 static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
1742 struct dsa_mall_mirror_tc_entry *mirror,
1743 bool ingress, struct netlink_ext_ack *extack)
1745 struct mt7530_priv *priv = ds->priv;
1749 /* Check for existent entry */
1750 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1753 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1755 /* MT7530 only supports one monitor port */
1756 monitor_port = mt753x_mirror_port_get(priv->id, val);
1757 if (val & MT753X_MIRROR_EN(priv->id) &&
1758 monitor_port != mirror->to_local_port)
1761 val |= MT753X_MIRROR_EN(priv->id);
1762 val &= ~MT753X_MIRROR_MASK(priv->id);
1763 val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port);
1764 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1766 val = mt7530_read(priv, MT7530_PCR_P(port));
1769 priv->mirror_rx |= BIT(port);
1772 priv->mirror_tx |= BIT(port);
1774 mt7530_write(priv, MT7530_PCR_P(port), val);
1779 static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
1780 struct dsa_mall_mirror_tc_entry *mirror)
1782 struct mt7530_priv *priv = ds->priv;
1785 val = mt7530_read(priv, MT7530_PCR_P(port));
1786 if (mirror->ingress) {
1787 val &= ~PORT_RX_MIR;
1788 priv->mirror_rx &= ~BIT(port);
1790 val &= ~PORT_TX_MIR;
1791 priv->mirror_tx &= ~BIT(port);
1793 mt7530_write(priv, MT7530_PCR_P(port), val);
1795 if (!priv->mirror_rx && !priv->mirror_tx) {
1796 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1797 val &= ~MT753X_MIRROR_EN(priv->id);
1798 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1802 static enum dsa_tag_protocol
1803 mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1804 enum dsa_tag_protocol mp)
1806 return DSA_TAG_PROTO_MTK;
1809 #ifdef CONFIG_GPIOLIB
1811 mt7530_gpio_to_bit(unsigned int offset)
1813 /* Map GPIO offset to register bit
1814 * [ 2: 0] port 0 LED 0..2 as GPIO 0..2
1815 * [ 6: 4] port 1 LED 0..2 as GPIO 3..5
1816 * [10: 8] port 2 LED 0..2 as GPIO 6..8
1817 * [14:12] port 3 LED 0..2 as GPIO 9..11
1818 * [18:16] port 4 LED 0..2 as GPIO 12..14
1820 return BIT(offset + offset / 3);
1824 mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
1826 struct mt7530_priv *priv = gpiochip_get_data(gc);
1827 u32 bit = mt7530_gpio_to_bit(offset);
1829 return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
1833 mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
1835 struct mt7530_priv *priv = gpiochip_get_data(gc);
1836 u32 bit = mt7530_gpio_to_bit(offset);
1839 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1841 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1845 mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
1847 struct mt7530_priv *priv = gpiochip_get_data(gc);
1848 u32 bit = mt7530_gpio_to_bit(offset);
1850 return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
1851 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1855 mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
1857 struct mt7530_priv *priv = gpiochip_get_data(gc);
1858 u32 bit = mt7530_gpio_to_bit(offset);
1860 mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
1861 mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
1867 mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
1869 struct mt7530_priv *priv = gpiochip_get_data(gc);
1870 u32 bit = mt7530_gpio_to_bit(offset);
1872 mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
1875 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1877 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1879 mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
1885 mt7530_setup_gpio(struct mt7530_priv *priv)
1887 struct device *dev = priv->dev;
1888 struct gpio_chip *gc;
1890 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
1894 mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
1895 mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
1896 mt7530_write(priv, MT7530_LED_IO_MODE, 0);
1898 gc->label = "mt7530";
1900 gc->owner = THIS_MODULE;
1901 gc->get_direction = mt7530_gpio_get_direction;
1902 gc->direction_input = mt7530_gpio_direction_input;
1903 gc->direction_output = mt7530_gpio_direction_output;
1904 gc->get = mt7530_gpio_get;
1905 gc->set = mt7530_gpio_set;
1908 gc->can_sleep = true;
1910 return devm_gpiochip_add_data(dev, gc, priv);
1912 #endif /* CONFIG_GPIOLIB */
1915 mt7530_irq_thread_fn(int irq, void *dev_id)
1917 struct mt7530_priv *priv = dev_id;
1918 bool handled = false;
1922 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1923 val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
1924 mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
1925 mutex_unlock(&priv->bus->mdio_lock);
1927 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1931 irq = irq_find_mapping(priv->irq_domain, p);
1932 handle_nested_irq(irq);
1937 return IRQ_RETVAL(handled);
1941 mt7530_irq_mask(struct irq_data *d)
1943 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1945 priv->irq_enable &= ~BIT(d->hwirq);
1949 mt7530_irq_unmask(struct irq_data *d)
1951 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1953 priv->irq_enable |= BIT(d->hwirq);
1957 mt7530_irq_bus_lock(struct irq_data *d)
1959 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1961 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1965 mt7530_irq_bus_sync_unlock(struct irq_data *d)
1967 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1969 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
1970 mutex_unlock(&priv->bus->mdio_lock);
1973 static struct irq_chip mt7530_irq_chip = {
1974 .name = KBUILD_MODNAME,
1975 .irq_mask = mt7530_irq_mask,
1976 .irq_unmask = mt7530_irq_unmask,
1977 .irq_bus_lock = mt7530_irq_bus_lock,
1978 .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
1982 mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
1983 irq_hw_number_t hwirq)
1985 irq_set_chip_data(irq, domain->host_data);
1986 irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
1987 irq_set_nested_thread(irq, true);
1988 irq_set_noprobe(irq);
1993 static const struct irq_domain_ops mt7530_irq_domain_ops = {
1994 .map = mt7530_irq_map,
1995 .xlate = irq_domain_xlate_onecell,
1999 mt7530_setup_mdio_irq(struct mt7530_priv *priv)
2001 struct dsa_switch *ds = priv->ds;
2004 for (p = 0; p < MT7530_NUM_PHYS; p++) {
2005 if (BIT(p) & ds->phys_mii_mask) {
2008 irq = irq_create_mapping(priv->irq_domain, p);
2009 ds->slave_mii_bus->irq[p] = irq;
2015 mt7530_setup_irq(struct mt7530_priv *priv)
2017 struct device *dev = priv->dev;
2018 struct device_node *np = dev->of_node;
2021 if (!of_property_read_bool(np, "interrupt-controller")) {
2022 dev_info(dev, "no interrupt support\n");
2026 priv->irq = of_irq_get(np, 0);
2027 if (priv->irq <= 0) {
2028 dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
2029 return priv->irq ? : -EINVAL;
2032 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
2033 &mt7530_irq_domain_ops, priv);
2034 if (!priv->irq_domain) {
2035 dev_err(dev, "failed to create IRQ domain\n");
2039 /* This register must be set for MT7530 to properly fire interrupts */
2040 if (priv->id != ID_MT7531)
2041 mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
2043 ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
2044 IRQF_ONESHOT, KBUILD_MODNAME, priv);
2046 irq_domain_remove(priv->irq_domain);
2047 dev_err(dev, "failed to request IRQ: %d\n", ret);
2055 mt7530_free_mdio_irq(struct mt7530_priv *priv)
2059 for (p = 0; p < MT7530_NUM_PHYS; p++) {
2060 if (BIT(p) & priv->ds->phys_mii_mask) {
2063 irq = irq_find_mapping(priv->irq_domain, p);
2064 irq_dispose_mapping(irq);
2070 mt7530_free_irq_common(struct mt7530_priv *priv)
2072 free_irq(priv->irq, priv);
2073 irq_domain_remove(priv->irq_domain);
2077 mt7530_free_irq(struct mt7530_priv *priv)
2079 mt7530_free_mdio_irq(priv);
2080 mt7530_free_irq_common(priv);
2084 mt7530_setup_mdio(struct mt7530_priv *priv)
2086 struct dsa_switch *ds = priv->ds;
2087 struct device *dev = priv->dev;
2088 struct mii_bus *bus;
2092 bus = devm_mdiobus_alloc(dev);
2096 ds->slave_mii_bus = bus;
2098 bus->name = KBUILD_MODNAME "-mii";
2099 snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
2100 bus->read = mt753x_phy_read;
2101 bus->write = mt753x_phy_write;
2103 bus->phy_mask = ~ds->phys_mii_mask;
2106 mt7530_setup_mdio_irq(priv);
2108 ret = devm_mdiobus_register(dev, bus);
2110 dev_err(dev, "failed to register MDIO bus: %d\n", ret);
2112 mt7530_free_mdio_irq(priv);
2119 mt7530_setup(struct dsa_switch *ds)
2121 struct mt7530_priv *priv = ds->priv;
2122 struct device_node *dn = NULL;
2123 struct device_node *phy_node;
2124 struct device_node *mac_np;
2125 struct mt7530_dummy_poll p;
2126 phy_interface_t interface;
2127 struct dsa_port *cpu_dp;
2131 /* The parent node of master netdev which holds the common system
2132 * controller also is the container for two GMACs nodes representing
2133 * as two netdev instances.
2135 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
2136 dn = cpu_dp->master->dev.of_node->parent;
2137 /* It doesn't matter which CPU port is found first,
2138 * their masters should share the same parent OF node
2144 dev_err(ds->dev, "parent OF node of DSA master not found");
2148 ds->assisted_learning_on_cpu_port = true;
2149 ds->mtu_enforcement_ingress = true;
2151 if (priv->id == ID_MT7530) {
2152 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
2153 ret = regulator_enable(priv->core_pwr);
2156 "Failed to enable core power: %d\n", ret);
2160 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
2161 ret = regulator_enable(priv->io_pwr);
2163 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
2169 /* Reset whole chip through gpio pin or memory-mapped registers for
2170 * different type of hardware
2173 reset_control_assert(priv->rstc);
2174 usleep_range(1000, 1100);
2175 reset_control_deassert(priv->rstc);
2177 gpiod_set_value_cansleep(priv->reset, 0);
2178 usleep_range(1000, 1100);
2179 gpiod_set_value_cansleep(priv->reset, 1);
2182 /* Waiting for MT7530 got to stable */
2183 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2184 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2187 dev_err(priv->dev, "reset timeout\n");
2191 id = mt7530_read(priv, MT7530_CREV);
2192 id >>= CHIP_NAME_SHIFT;
2193 if (id != MT7530_ID) {
2194 dev_err(priv->dev, "chip %x can't be supported\n", id);
2198 /* Reset the switch through internal reset */
2199 mt7530_write(priv, MT7530_SYS_CTRL,
2200 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2203 mt7530_pll_setup(priv);
2205 /* Lower Tx driving for TRGMII path */
2206 for (i = 0; i < NUM_TRGMII_CTRL; i++)
2207 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
2208 TD_DM_DRVP(8) | TD_DM_DRVN(8));
2210 for (i = 0; i < NUM_TRGMII_CTRL; i++)
2211 mt7530_rmw(priv, MT7530_TRGMII_RD(i),
2212 RD_TAP_MASK, RD_TAP(16));
2215 val = mt7530_read(priv, MT7530_MHWTRAP);
2216 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
2217 val |= MHWTRAP_MANUAL;
2218 mt7530_write(priv, MT7530_MHWTRAP, val);
2220 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2222 /* Enable and reset MIB counters */
2223 mt7530_mib_reset(ds);
2225 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2226 /* Disable forwarding by default on all ports */
2227 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2230 /* Disable learning by default on all ports */
2231 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2233 if (dsa_is_cpu_port(ds, i)) {
2234 ret = mt753x_cpu_port_enable(ds, i);
2238 mt7530_port_disable(ds, i);
2240 /* Set default PVID to 0 on all user ports */
2241 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2244 /* Enable consistent egress tag */
2245 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2246 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2249 /* Setup VLAN ID 0 for VLAN-unaware bridges */
2250 ret = mt7530_setup_vlan0(priv);
2255 priv->p5_intf_sel = P5_DISABLED;
2256 interface = PHY_INTERFACE_MODE_NA;
2258 if (!dsa_is_unused_port(ds, 5)) {
2259 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
2260 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
2261 if (ret && ret != -ENODEV)
2264 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
2265 for_each_child_of_node(dn, mac_np) {
2266 if (!of_device_is_compatible(mac_np,
2267 "mediatek,eth-mac"))
2270 ret = of_property_read_u32(mac_np, "reg", &id);
2271 if (ret < 0 || id != 1)
2274 phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
2278 if (phy_node->parent == priv->dev->of_node->parent) {
2279 ret = of_get_phy_mode(mac_np, &interface);
2280 if (ret && ret != -ENODEV) {
2281 of_node_put(mac_np);
2282 of_node_put(phy_node);
2285 id = of_mdio_parse_addr(ds->dev, phy_node);
2287 priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
2289 priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
2291 of_node_put(mac_np);
2292 of_node_put(phy_node);
2297 #ifdef CONFIG_GPIOLIB
2298 if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
2299 ret = mt7530_setup_gpio(priv);
2303 #endif /* CONFIG_GPIOLIB */
2305 mt7530_setup_port5(ds, interface);
2307 /* Flush the FDB table */
2308 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2316 mt7531_setup_common(struct dsa_switch *ds)
2318 struct mt7530_priv *priv = ds->priv;
2319 struct dsa_port *cpu_dp;
2322 /* BPDU to CPU port */
2323 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
2324 mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK,
2325 BIT(cpu_dp->index));
2328 mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK,
2329 MT753X_BPDU_CPU_ONLY);
2331 /* Enable and reset MIB counters */
2332 mt7530_mib_reset(ds);
2334 /* Disable flooding on all ports */
2335 mt7530_clear(priv, MT7530_MFC, BC_FFP_MASK | UNM_FFP_MASK |
2338 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2339 /* Disable forwarding by default on all ports */
2340 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2343 /* Disable learning by default on all ports */
2344 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2346 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
2348 if (dsa_is_cpu_port(ds, i)) {
2349 ret = mt753x_cpu_port_enable(ds, i);
2353 mt7530_port_disable(ds, i);
2355 /* Set default PVID to 0 on all user ports */
2356 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2360 /* Enable consistent egress tag */
2361 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2362 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2365 /* Flush the FDB table */
2366 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2374 mt7531_setup(struct dsa_switch *ds)
2376 struct mt7530_priv *priv = ds->priv;
2377 struct mt7530_dummy_poll p;
2381 /* Reset whole chip through gpio pin or memory-mapped registers for
2382 * different type of hardware
2385 reset_control_assert(priv->rstc);
2386 usleep_range(1000, 1100);
2387 reset_control_deassert(priv->rstc);
2389 gpiod_set_value_cansleep(priv->reset, 0);
2390 usleep_range(1000, 1100);
2391 gpiod_set_value_cansleep(priv->reset, 1);
2394 /* Waiting for MT7530 got to stable */
2395 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2396 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2399 dev_err(priv->dev, "reset timeout\n");
2403 id = mt7530_read(priv, MT7531_CREV);
2404 id >>= CHIP_NAME_SHIFT;
2406 if (id != MT7531_ID) {
2407 dev_err(priv->dev, "chip %x can't be supported\n", id);
2411 /* all MACs must be forced link-down before sw reset */
2412 for (i = 0; i < MT7530_NUM_PORTS; i++)
2413 mt7530_write(priv, MT7530_PMCR_P(i), MT7531_FORCE_LNK);
2415 /* Reset the switch through internal reset */
2416 mt7530_write(priv, MT7530_SYS_CTRL,
2417 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2420 mt7531_pll_setup(priv);
2422 if (mt7531_dual_sgmii_supported(priv)) {
2423 priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII;
2425 /* Let ds->slave_mii_bus be able to access external phy. */
2426 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
2427 MT7531_EXT_P_MDC_11);
2428 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
2429 MT7531_EXT_P_MDIO_12);
2431 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
2433 dev_dbg(ds->dev, "P5 support %s interface\n",
2434 p5_intf_modes(priv->p5_intf_sel));
2436 mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
2437 MT7531_GPIO0_INTERRUPT);
2439 /* Let phylink decide the interface later. */
2440 priv->p5_interface = PHY_INTERFACE_MODE_NA;
2441 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2443 /* Enable PHY core PLL, since phy_device has not yet been created
2444 * provided for phy_[read,write]_mmd_indirect is called, we provide
2445 * our own mt7531_ind_mmd_phy_[read,write] to complete this
2448 val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR,
2449 MDIO_MMD_VEND2, CORE_PLL_GROUP4);
2450 val |= MT7531_PHY_PLL_BYPASS_MODE;
2451 val &= ~MT7531_PHY_PLL_OFF;
2452 mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2,
2453 CORE_PLL_GROUP4, val);
2455 mt7531_setup_common(ds);
2457 /* Setup VLAN ID 0 for VLAN-unaware bridges */
2458 ret = mt7530_setup_vlan0(priv);
2462 ds->assisted_learning_on_cpu_port = true;
2463 ds->mtu_enforcement_ingress = true;
2468 static void mt7530_mac_port_get_caps(struct dsa_switch *ds, int port,
2469 struct phylink_config *config)
2472 case 0 ... 4: /* Internal phy */
2473 __set_bit(PHY_INTERFACE_MODE_GMII,
2474 config->supported_interfaces);
2477 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2478 phy_interface_set_rgmii(config->supported_interfaces);
2479 __set_bit(PHY_INTERFACE_MODE_MII,
2480 config->supported_interfaces);
2481 __set_bit(PHY_INTERFACE_MODE_GMII,
2482 config->supported_interfaces);
2485 case 6: /* 1st cpu port */
2486 __set_bit(PHY_INTERFACE_MODE_RGMII,
2487 config->supported_interfaces);
2488 __set_bit(PHY_INTERFACE_MODE_TRGMII,
2489 config->supported_interfaces);
2494 static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port)
2496 return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII);
2499 static void mt7531_mac_port_get_caps(struct dsa_switch *ds, int port,
2500 struct phylink_config *config)
2502 struct mt7530_priv *priv = ds->priv;
2505 case 0 ... 4: /* Internal phy */
2506 __set_bit(PHY_INTERFACE_MODE_GMII,
2507 config->supported_interfaces);
2510 case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */
2511 if (mt7531_is_rgmii_port(priv, port)) {
2512 phy_interface_set_rgmii(config->supported_interfaces);
2517 case 6: /* 1st cpu port supports sgmii/8023z only */
2518 __set_bit(PHY_INTERFACE_MODE_SGMII,
2519 config->supported_interfaces);
2520 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
2521 config->supported_interfaces);
2522 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
2523 config->supported_interfaces);
2525 config->mac_capabilities |= MAC_2500FD;
2531 mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
2533 struct mt7530_priv *priv = ds->priv;
2535 return priv->info->pad_setup(ds, state->interface);
2539 mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2540 phy_interface_t interface)
2542 struct mt7530_priv *priv = ds->priv;
2544 /* Only need to setup port5. */
2548 mt7530_setup_port5(priv->ds, interface);
2553 static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port,
2554 phy_interface_t interface,
2555 struct phy_device *phydev)
2559 if (!mt7531_is_rgmii_port(priv, port)) {
2560 dev_err(priv->dev, "RGMII mode is not available for port %d\n",
2565 val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2567 val &= ~GP_MODE_MASK;
2568 val |= GP_MODE(MT7531_GP_MODE_RGMII);
2569 val &= ~CLK_SKEW_IN_MASK;
2570 val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2571 val &= ~CLK_SKEW_OUT_MASK;
2572 val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2573 val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2575 /* Do not adjust rgmii delay when vendor phy driver presents. */
2576 if (!phydev || phy_driver_is_genphy(phydev)) {
2577 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2578 switch (interface) {
2579 case PHY_INTERFACE_MODE_RGMII:
2580 val |= TXCLK_NO_REVERSE;
2581 val |= RXCLK_NO_DELAY;
2583 case PHY_INTERFACE_MODE_RGMII_RXID:
2584 val |= TXCLK_NO_REVERSE;
2586 case PHY_INTERFACE_MODE_RGMII_TXID:
2587 val |= RXCLK_NO_DELAY;
2589 case PHY_INTERFACE_MODE_RGMII_ID:
2595 mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2600 static void mt7531_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
2601 phy_interface_t interface, int speed, int duplex)
2603 struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
2604 int port = pcs_to_mt753x_pcs(pcs)->port;
2607 /* For adjusting speed and duplex of SGMII force mode. */
2608 if (interface != PHY_INTERFACE_MODE_SGMII ||
2609 phylink_autoneg_inband(mode))
2612 /* SGMII force mode setting */
2613 val = mt7530_read(priv, MT7531_SGMII_MODE(port));
2614 val &= ~MT7531_SGMII_IF_MODE_MASK;
2618 val |= MT7531_SGMII_FORCE_SPEED_10;
2621 val |= MT7531_SGMII_FORCE_SPEED_100;
2624 val |= MT7531_SGMII_FORCE_SPEED_1000;
2628 /* MT7531 SGMII 1G force mode can only work in full duplex mode,
2629 * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2631 * The speed check is unnecessary as the MAC capabilities apply
2632 * this restriction. --rmk
2634 if ((speed == SPEED_10 || speed == SPEED_100) &&
2635 duplex != DUPLEX_FULL)
2636 val |= MT7531_SGMII_FORCE_HALF_DUPLEX;
2638 mt7530_write(priv, MT7531_SGMII_MODE(port), val);
2641 static bool mt753x_is_mac_port(u32 port)
2643 return (port == 5 || port == 6);
2646 static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port,
2647 phy_interface_t interface)
2651 if (!mt753x_is_mac_port(port))
2654 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2655 MT7531_SGMII_PHYA_PWD);
2657 val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port));
2658 val &= ~MT7531_RG_TPHY_SPEED_MASK;
2659 /* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B
2662 val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ?
2663 MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G;
2664 mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val);
2666 mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2668 /* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex
2669 * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2671 mt7530_rmw(priv, MT7531_SGMII_MODE(port),
2672 MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS,
2673 MT7531_SGMII_FORCE_SPEED_1000);
2675 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2680 static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port,
2681 phy_interface_t interface)
2683 if (!mt753x_is_mac_port(port))
2686 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2687 MT7531_SGMII_PHYA_PWD);
2689 mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
2690 MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G);
2692 mt7530_set(priv, MT7531_SGMII_MODE(port),
2693 MT7531_SGMII_REMOTE_FAULT_DIS |
2694 MT7531_SGMII_SPEED_DUPLEX_AN);
2696 mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port),
2697 MT7531_SGMII_TX_CONFIG_MASK, 1);
2699 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2701 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART);
2703 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2708 static void mt7531_pcs_an_restart(struct phylink_pcs *pcs)
2710 struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
2711 int port = pcs_to_mt753x_pcs(pcs)->port;
2714 /* Only restart AN when AN is enabled */
2715 val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2716 if (val & MT7531_SGMII_AN_ENABLE) {
2717 val |= MT7531_SGMII_AN_RESTART;
2718 mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val);
2723 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2724 phy_interface_t interface)
2726 struct mt7530_priv *priv = ds->priv;
2727 struct phy_device *phydev;
2728 struct dsa_port *dp;
2730 if (!mt753x_is_mac_port(port)) {
2731 dev_err(priv->dev, "port %d is not a MAC port\n", port);
2735 switch (interface) {
2736 case PHY_INTERFACE_MODE_RGMII:
2737 case PHY_INTERFACE_MODE_RGMII_ID:
2738 case PHY_INTERFACE_MODE_RGMII_RXID:
2739 case PHY_INTERFACE_MODE_RGMII_TXID:
2740 dp = dsa_to_port(ds, port);
2741 phydev = dp->slave->phydev;
2742 return mt7531_rgmii_setup(priv, port, interface, phydev);
2743 case PHY_INTERFACE_MODE_SGMII:
2744 return mt7531_sgmii_setup_mode_an(priv, port, interface);
2745 case PHY_INTERFACE_MODE_NA:
2746 case PHY_INTERFACE_MODE_1000BASEX:
2747 case PHY_INTERFACE_MODE_2500BASEX:
2748 return mt7531_sgmii_setup_mode_force(priv, port, interface);
2757 mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2758 const struct phylink_link_state *state)
2760 struct mt7530_priv *priv = ds->priv;
2762 return priv->info->mac_port_config(ds, port, mode, state->interface);
2765 static struct phylink_pcs *
2766 mt753x_phylink_mac_select_pcs(struct dsa_switch *ds, int port,
2767 phy_interface_t interface)
2769 struct mt7530_priv *priv = ds->priv;
2771 switch (interface) {
2772 case PHY_INTERFACE_MODE_TRGMII:
2773 case PHY_INTERFACE_MODE_SGMII:
2774 case PHY_INTERFACE_MODE_1000BASEX:
2775 case PHY_INTERFACE_MODE_2500BASEX:
2776 return &priv->pcs[port].pcs;
2784 mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2785 const struct phylink_link_state *state)
2787 struct mt7530_priv *priv = ds->priv;
2788 u32 mcr_cur, mcr_new;
2791 case 0 ... 4: /* Internal phy */
2792 if (state->interface != PHY_INTERFACE_MODE_GMII)
2795 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2796 if (priv->p5_interface == state->interface)
2799 if (mt753x_mac_config(ds, port, mode, state) < 0)
2802 if (priv->p5_intf_sel != P5_DISABLED)
2803 priv->p5_interface = state->interface;
2805 case 6: /* 1st cpu port */
2806 if (priv->p6_interface == state->interface)
2809 mt753x_pad_setup(ds, state);
2811 if (mt753x_mac_config(ds, port, mode, state) < 0)
2814 priv->p6_interface = state->interface;
2818 dev_err(ds->dev, "%s: unsupported %s port: %i\n",
2819 __func__, phy_modes(state->interface), port);
2823 mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
2825 mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
2826 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
2827 PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id);
2829 /* Are we connected to external phy */
2830 if (port == 5 && dsa_is_user_port(ds, 5))
2831 mcr_new |= PMCR_EXT_PHY;
2833 if (mcr_new != mcr_cur)
2834 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
2837 static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port,
2839 phy_interface_t interface)
2841 struct mt7530_priv *priv = ds->priv;
2843 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
2846 static void mt753x_phylink_pcs_link_up(struct phylink_pcs *pcs,
2848 phy_interface_t interface,
2849 int speed, int duplex)
2851 if (pcs->ops->pcs_link_up)
2852 pcs->ops->pcs_link_up(pcs, mode, interface, speed, duplex);
2855 static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port,
2857 phy_interface_t interface,
2858 struct phy_device *phydev,
2859 int speed, int duplex,
2860 bool tx_pause, bool rx_pause)
2862 struct mt7530_priv *priv = ds->priv;
2865 mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
2867 /* MT753x MAC works in 1G full duplex mode for all up-clocked
2870 if (interface == PHY_INTERFACE_MODE_TRGMII ||
2871 (phy_interface_mode_is_8023z(interface))) {
2873 duplex = DUPLEX_FULL;
2878 mcr |= PMCR_FORCE_SPEED_1000;
2881 mcr |= PMCR_FORCE_SPEED_100;
2884 if (duplex == DUPLEX_FULL) {
2885 mcr |= PMCR_FORCE_FDX;
2887 mcr |= PMCR_TX_FC_EN;
2889 mcr |= PMCR_RX_FC_EN;
2892 if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, false) >= 0) {
2895 mcr |= PMCR_FORCE_EEE1G;
2898 mcr |= PMCR_FORCE_EEE100;
2903 mt7530_set(priv, MT7530_PMCR_P(port), mcr);
2907 mt7531_cpu_port_config(struct dsa_switch *ds, int port)
2909 struct mt7530_priv *priv = ds->priv;
2910 phy_interface_t interface;
2916 if (mt7531_is_rgmii_port(priv, port))
2917 interface = PHY_INTERFACE_MODE_RGMII;
2919 interface = PHY_INTERFACE_MODE_2500BASEX;
2921 priv->p5_interface = interface;
2924 interface = PHY_INTERFACE_MODE_2500BASEX;
2926 priv->p6_interface = interface;
2932 if (interface == PHY_INTERFACE_MODE_2500BASEX)
2937 ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
2940 mt7530_write(priv, MT7530_PMCR_P(port),
2941 PMCR_CPU_PORT_SETTING(priv->id));
2942 mt753x_phylink_pcs_link_up(&priv->pcs[port].pcs, MLO_AN_FIXED,
2943 interface, speed, DUPLEX_FULL);
2944 mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2945 speed, DUPLEX_FULL, true, true);
2950 static void mt753x_phylink_get_caps(struct dsa_switch *ds, int port,
2951 struct phylink_config *config)
2953 struct mt7530_priv *priv = ds->priv;
2955 /* This switch only supports full-duplex at 1Gbps */
2956 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
2957 MAC_10 | MAC_100 | MAC_1000FD;
2959 if ((priv->id == ID_MT7531) && mt753x_is_mac_port(port))
2960 config->mac_capabilities |= MAC_2500FD;
2962 /* This driver does not make use of the speed, duplex, pause or the
2963 * advertisement in its mac_config, so it is safe to mark this driver
2966 config->legacy_pre_march2020 = false;
2968 priv->info->mac_port_get_caps(ds, port, config);
2971 static int mt753x_pcs_validate(struct phylink_pcs *pcs,
2972 unsigned long *supported,
2973 const struct phylink_link_state *state)
2975 /* Autonegotiation is not supported in TRGMII nor 802.3z modes */
2976 if (state->interface == PHY_INTERFACE_MODE_TRGMII ||
2977 phy_interface_mode_is_8023z(state->interface))
2978 phylink_clear(supported, Autoneg);
2983 static void mt7530_pcs_get_state(struct phylink_pcs *pcs,
2984 struct phylink_link_state *state)
2986 struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
2987 int port = pcs_to_mt753x_pcs(pcs)->port;
2990 pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2992 state->link = (pmsr & PMSR_LINK);
2993 state->an_complete = state->link;
2994 state->duplex = !!(pmsr & PMSR_DPX);
2996 switch (pmsr & PMSR_SPEED_MASK) {
2998 state->speed = SPEED_10;
3000 case PMSR_SPEED_100:
3001 state->speed = SPEED_100;
3003 case PMSR_SPEED_1000:
3004 state->speed = SPEED_1000;
3007 state->speed = SPEED_UNKNOWN;
3011 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
3012 if (pmsr & PMSR_RX_FC)
3013 state->pause |= MLO_PAUSE_RX;
3014 if (pmsr & PMSR_TX_FC)
3015 state->pause |= MLO_PAUSE_TX;
3019 mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port,
3020 struct phylink_link_state *state)
3025 status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
3026 state->link = !!(status & MT7531_SGMII_LINK_STATUS);
3027 state->an_complete = !!(status & MT7531_SGMII_AN_COMPLETE);
3028 if (state->interface == PHY_INTERFACE_MODE_SGMII &&
3029 (status & MT7531_SGMII_AN_ENABLE)) {
3030 val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port));
3031 config_reg = val >> 16;
3033 switch (config_reg & LPA_SGMII_SPD_MASK) {
3034 case LPA_SGMII_1000:
3035 state->speed = SPEED_1000;
3038 state->speed = SPEED_100;
3041 state->speed = SPEED_10;
3044 dev_err(priv->dev, "invalid sgmii PHY speed\n");
3045 state->link = false;
3049 if (config_reg & LPA_SGMII_FULL_DUPLEX)
3050 state->duplex = DUPLEX_FULL;
3052 state->duplex = DUPLEX_HALF;
3059 mt7531_sgmii_pcs_get_state_inband(struct mt7530_priv *priv, int port,
3060 struct phylink_link_state *state)
3064 val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
3065 state->link = !!(val & MT7531_SGMII_LINK_STATUS);
3069 state->an_complete = state->link;
3071 if (state->interface == PHY_INTERFACE_MODE_2500BASEX)
3072 state->speed = SPEED_2500;
3074 state->speed = SPEED_1000;
3076 state->duplex = DUPLEX_FULL;
3077 state->pause = MLO_PAUSE_NONE;
3080 static void mt7531_pcs_get_state(struct phylink_pcs *pcs,
3081 struct phylink_link_state *state)
3083 struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
3084 int port = pcs_to_mt753x_pcs(pcs)->port;
3086 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3087 mt7531_sgmii_pcs_get_state_an(priv, port, state);
3089 } else if ((state->interface == PHY_INTERFACE_MODE_1000BASEX) ||
3090 (state->interface == PHY_INTERFACE_MODE_2500BASEX)) {
3091 mt7531_sgmii_pcs_get_state_inband(priv, port, state);
3095 state->link = false;
3098 static int mt753x_pcs_config(struct phylink_pcs *pcs, unsigned int mode,
3099 phy_interface_t interface,
3100 const unsigned long *advertising,
3101 bool permit_pause_to_mac)
3106 static void mt7530_pcs_an_restart(struct phylink_pcs *pcs)
3110 static const struct phylink_pcs_ops mt7530_pcs_ops = {
3111 .pcs_validate = mt753x_pcs_validate,
3112 .pcs_get_state = mt7530_pcs_get_state,
3113 .pcs_config = mt753x_pcs_config,
3114 .pcs_an_restart = mt7530_pcs_an_restart,
3117 static const struct phylink_pcs_ops mt7531_pcs_ops = {
3118 .pcs_validate = mt753x_pcs_validate,
3119 .pcs_get_state = mt7531_pcs_get_state,
3120 .pcs_config = mt753x_pcs_config,
3121 .pcs_an_restart = mt7531_pcs_an_restart,
3122 .pcs_link_up = mt7531_pcs_link_up,
3126 mt753x_setup(struct dsa_switch *ds)
3128 struct mt7530_priv *priv = ds->priv;
3131 /* Initialise the PCS devices */
3132 for (i = 0; i < priv->ds->num_ports; i++) {
3133 priv->pcs[i].pcs.ops = priv->info->pcs_ops;
3134 priv->pcs[i].priv = priv;
3135 priv->pcs[i].port = i;
3136 if (mt753x_is_mac_port(i))
3137 priv->pcs[i].pcs.poll = 1;
3140 ret = priv->info->sw_setup(ds);
3144 ret = mt7530_setup_irq(priv);
3148 ret = mt7530_setup_mdio(priv);
3149 if (ret && priv->irq)
3150 mt7530_free_irq_common(priv);
3155 static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
3156 struct ethtool_eee *e)
3158 struct mt7530_priv *priv = ds->priv;
3159 u32 eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port));
3161 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
3162 e->tx_lpi_timer = GET_LPI_THRESH(eeecr);
3167 static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
3168 struct ethtool_eee *e)
3170 struct mt7530_priv *priv = ds->priv;
3171 u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
3173 if (e->tx_lpi_timer > 0xFFF)
3176 set = SET_LPI_THRESH(e->tx_lpi_timer);
3177 if (!e->tx_lpi_enabled)
3178 /* Force LPI Mode without a delay */
3180 mt7530_rmw(priv, MT7530_PMEEECR_P(port), mask, set);
3185 static const struct dsa_switch_ops mt7530_switch_ops = {
3186 .get_tag_protocol = mtk_get_tag_protocol,
3187 .setup = mt753x_setup,
3188 .get_strings = mt7530_get_strings,
3189 .get_ethtool_stats = mt7530_get_ethtool_stats,
3190 .get_sset_count = mt7530_get_sset_count,
3191 .set_ageing_time = mt7530_set_ageing_time,
3192 .port_enable = mt7530_port_enable,
3193 .port_disable = mt7530_port_disable,
3194 .port_change_mtu = mt7530_port_change_mtu,
3195 .port_max_mtu = mt7530_port_max_mtu,
3196 .port_stp_state_set = mt7530_stp_state_set,
3197 .port_pre_bridge_flags = mt7530_port_pre_bridge_flags,
3198 .port_bridge_flags = mt7530_port_bridge_flags,
3199 .port_bridge_join = mt7530_port_bridge_join,
3200 .port_bridge_leave = mt7530_port_bridge_leave,
3201 .port_fdb_add = mt7530_port_fdb_add,
3202 .port_fdb_del = mt7530_port_fdb_del,
3203 .port_fdb_dump = mt7530_port_fdb_dump,
3204 .port_mdb_add = mt7530_port_mdb_add,
3205 .port_mdb_del = mt7530_port_mdb_del,
3206 .port_vlan_filtering = mt7530_port_vlan_filtering,
3207 .port_vlan_add = mt7530_port_vlan_add,
3208 .port_vlan_del = mt7530_port_vlan_del,
3209 .port_mirror_add = mt753x_port_mirror_add,
3210 .port_mirror_del = mt753x_port_mirror_del,
3211 .phylink_get_caps = mt753x_phylink_get_caps,
3212 .phylink_mac_select_pcs = mt753x_phylink_mac_select_pcs,
3213 .phylink_mac_config = mt753x_phylink_mac_config,
3214 .phylink_mac_link_down = mt753x_phylink_mac_link_down,
3215 .phylink_mac_link_up = mt753x_phylink_mac_link_up,
3216 .get_mac_eee = mt753x_get_mac_eee,
3217 .set_mac_eee = mt753x_set_mac_eee,
3220 static const struct mt753x_info mt753x_table[] = {
3223 .pcs_ops = &mt7530_pcs_ops,
3224 .sw_setup = mt7530_setup,
3225 .phy_read = mt7530_phy_read,
3226 .phy_write = mt7530_phy_write,
3227 .pad_setup = mt7530_pad_clk_setup,
3228 .mac_port_get_caps = mt7530_mac_port_get_caps,
3229 .mac_port_config = mt7530_mac_config,
3233 .pcs_ops = &mt7530_pcs_ops,
3234 .sw_setup = mt7530_setup,
3235 .phy_read = mt7530_phy_read,
3236 .phy_write = mt7530_phy_write,
3237 .pad_setup = mt7530_pad_clk_setup,
3238 .mac_port_get_caps = mt7530_mac_port_get_caps,
3239 .mac_port_config = mt7530_mac_config,
3243 .pcs_ops = &mt7531_pcs_ops,
3244 .sw_setup = mt7531_setup,
3245 .phy_read = mt7531_ind_phy_read,
3246 .phy_write = mt7531_ind_phy_write,
3247 .pad_setup = mt7531_pad_setup,
3248 .cpu_port_config = mt7531_cpu_port_config,
3249 .mac_port_get_caps = mt7531_mac_port_get_caps,
3250 .mac_port_config = mt7531_mac_config,
3254 static const struct of_device_id mt7530_of_match[] = {
3255 { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
3256 { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
3257 { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
3260 MODULE_DEVICE_TABLE(of, mt7530_of_match);
3263 mt7530_probe(struct mdio_device *mdiodev)
3265 struct mt7530_priv *priv;
3266 struct device_node *dn;
3268 dn = mdiodev->dev.of_node;
3270 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
3274 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
3278 priv->ds->dev = &mdiodev->dev;
3279 priv->ds->num_ports = MT7530_NUM_PORTS;
3281 /* Use medatek,mcm property to distinguish hardware type that would
3282 * casues a little bit differences on power-on sequence.
3284 priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
3286 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
3288 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
3289 if (IS_ERR(priv->rstc)) {
3290 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3291 return PTR_ERR(priv->rstc);
3295 /* Get the hardware identifier from the devicetree node.
3296 * We will need it for some of the clock and regulator setup.
3298 priv->info = of_device_get_match_data(&mdiodev->dev);
3302 /* Sanity check if these required device operations are filled
3305 if (!priv->info->sw_setup || !priv->info->pad_setup ||
3306 !priv->info->phy_read || !priv->info->phy_write ||
3307 !priv->info->mac_port_get_caps ||
3308 !priv->info->mac_port_config)
3311 priv->id = priv->info->id;
3313 if (priv->id == ID_MT7530) {
3314 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
3315 if (IS_ERR(priv->core_pwr))
3316 return PTR_ERR(priv->core_pwr);
3318 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
3319 if (IS_ERR(priv->io_pwr))
3320 return PTR_ERR(priv->io_pwr);
3323 /* Not MCM that indicates switch works as the remote standalone
3324 * integrated circuit so the GPIO pin would be used to complete
3325 * the reset, otherwise memory-mapped register accessing used
3326 * through syscon provides in the case of MCM.
3329 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
3331 if (IS_ERR(priv->reset)) {
3332 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3333 return PTR_ERR(priv->reset);
3337 priv->bus = mdiodev->bus;
3338 priv->dev = &mdiodev->dev;
3339 priv->ds->priv = priv;
3340 priv->ds->ops = &mt7530_switch_ops;
3341 mutex_init(&priv->reg_mutex);
3342 dev_set_drvdata(&mdiodev->dev, priv);
3344 return dsa_register_switch(priv->ds);
3348 mt7530_remove(struct mdio_device *mdiodev)
3350 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
3356 ret = regulator_disable(priv->core_pwr);
3359 "Failed to disable core power: %d\n", ret);
3361 ret = regulator_disable(priv->io_pwr);
3363 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
3367 mt7530_free_irq(priv);
3369 dsa_unregister_switch(priv->ds);
3370 mutex_destroy(&priv->reg_mutex);
3373 static void mt7530_shutdown(struct mdio_device *mdiodev)
3375 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
3380 dsa_switch_shutdown(priv->ds);
3382 dev_set_drvdata(&mdiodev->dev, NULL);
3385 static struct mdio_driver mt7530_mdio_driver = {
3386 .probe = mt7530_probe,
3387 .remove = mt7530_remove,
3388 .shutdown = mt7530_shutdown,
3391 .of_match_table = mt7530_of_match,
3395 mdio_module_driver(mt7530_mdio_driver);
3397 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
3398 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
3399 MODULE_LICENSE("GPL");