1 From 6a0a62dec3c582db4260f411294770448efc3d6c Mon Sep 17 00:00:00 2001
2 From: Sean Wang <sean.wang@mediatek.com>
3 Date: Wed, 29 Mar 2017 17:38:23 +0800
4 Subject: [PATCH 30/57] net-next: dsa: add dsa support for Mediatek MT7530
7 MT7530 is a 7-ports Gigabit Ethernet Switch that could be found on
8 Mediatek router platforms such as MT7623A or MT7623N platform which
9 includes 7-port Gigabit Ethernet MAC and 5-port Gigabit Ethernet PHY.
10 Among these ports, The port from 0 to 4 are the user ports connecting
11 with the remote devices while the port 5 and 6 are the CPU ports
12 connecting into Mediatek Ethernet GMAC.
14 For port 6, it can communicate with the CPU via Mediatek Ethernet GMAC
15 through either the TRGMII or RGMII which could be controlled by phy-mode
16 in the dt-bindings to specify which mode is preferred to use. And for
17 port 5, only RGMII can be specified. However, currently, only port 6 is
18 being supported in this DSA driver.
20 The driver is made with the reference to qca8k and other existing DSA
21 driver. The most of the essential callbacks of the DSA are already
22 support in the driver, including tag insert for user port distinguishing,
23 port control, bridge offloading, STP setup and ethtool operation to allow
24 DSA to model each user port into a standalone netdevice as the other DSA
27 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
28 Signed-off-by: Landen Chao <Landen.Chao@mediatek.com>
30 drivers/net/dsa/Kconfig | 8 +
31 drivers/net/dsa/Makefile | 2 +-
32 drivers/net/dsa/mt7530.c | 1126 ++++++++++++++++++++++++++++++++++++++++++++++
33 drivers/net/dsa/mt7530.h | 390 ++++++++++++++++
34 4 files changed, 1525 insertions(+), 1 deletion(-)
35 create mode 100644 drivers/net/dsa/mt7530.c
36 create mode 100644 drivers/net/dsa/mt7530.h
38 --- a/drivers/net/dsa/Kconfig
39 +++ b/drivers/net/dsa/Kconfig
40 @@ -34,4 +34,12 @@ config NET_DSA_QCA8K
41 This enables support for the Qualcomm Atheros QCA8K Ethernet
44 +config NET_DSA_MT7530
45 + tristate "Mediatek MT7530 Ethernet switch support"
47 + select NET_DSA_TAG_MTK
49 + This enables support for the Mediatek MT7530 Ethernet switch
53 --- a/drivers/net/dsa/Makefile
54 +++ b/drivers/net/dsa/Makefile
56 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
57 obj-$(CONFIG_NET_DSA_BCM_SF2) += bcm_sf2.o
58 obj-$(CONFIG_NET_DSA_QCA8K) += qca8k.o
60 +obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o
64 +++ b/drivers/net/dsa/mt7530.c
67 + * Mediatek MT7530 DSA Switch driver
68 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
70 + * This program is free software; you can redistribute it and/or modify
71 + * it under the terms of the GNU General Public License version 2 as
72 + * published by the Free Software Foundation.
74 + * This program is distributed in the hope that it will be useful,
75 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
76 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77 + * GNU General Public License for more details.
79 +#include <linux/etherdevice.h>
80 +#include <linux/if_bridge.h>
81 +#include <linux/iopoll.h>
82 +#include <linux/mdio.h>
83 +#include <linux/mfd/syscon.h>
84 +#include <linux/module.h>
85 +#include <linux/netdevice.h>
86 +#include <linux/of_gpio.h>
87 +#include <linux/of_mdio.h>
88 +#include <linux/of_net.h>
89 +#include <linux/of_platform.h>
90 +#include <linux/phy.h>
91 +#include <linux/regmap.h>
92 +#include <linux/regulator/consumer.h>
93 +#include <linux/reset.h>
95 +#include <net/switchdev.h>
99 +/* String, offset, and register size in bytes if different from 4 bytes */
100 +static const struct mt7530_mib_desc mt7530_mib[] = {
101 + MIB_DESC(1, 0x00, "TxDrop"),
102 + MIB_DESC(1, 0x04, "TxCrcErr"),
103 + MIB_DESC(1, 0x08, "TxUnicast"),
104 + MIB_DESC(1, 0x0c, "TxMulticast"),
105 + MIB_DESC(1, 0x10, "TxBroadcast"),
106 + MIB_DESC(1, 0x14, "TxCollision"),
107 + MIB_DESC(1, 0x18, "TxSingleCollision"),
108 + MIB_DESC(1, 0x1c, "TxMultipleCollision"),
109 + MIB_DESC(1, 0x20, "TxDeferred"),
110 + MIB_DESC(1, 0x24, "TxLateCollision"),
111 + MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
112 + MIB_DESC(1, 0x2c, "TxPause"),
113 + MIB_DESC(1, 0x30, "TxPktSz64"),
114 + MIB_DESC(1, 0x34, "TxPktSz65To127"),
115 + MIB_DESC(1, 0x38, "TxPktSz128To255"),
116 + MIB_DESC(1, 0x3c, "TxPktSz256To511"),
117 + MIB_DESC(1, 0x40, "TxPktSz512To1023"),
118 + MIB_DESC(1, 0x44, "Tx1024ToMax"),
119 + MIB_DESC(2, 0x48, "TxBytes"),
120 + MIB_DESC(1, 0x60, "RxDrop"),
121 + MIB_DESC(1, 0x64, "RxFiltering"),
122 + MIB_DESC(1, 0x6c, "RxMulticast"),
123 + MIB_DESC(1, 0x70, "RxBroadcast"),
124 + MIB_DESC(1, 0x74, "RxAlignErr"),
125 + MIB_DESC(1, 0x78, "RxCrcErr"),
126 + MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
127 + MIB_DESC(1, 0x80, "RxFragErr"),
128 + MIB_DESC(1, 0x84, "RxOverSzErr"),
129 + MIB_DESC(1, 0x88, "RxJabberErr"),
130 + MIB_DESC(1, 0x8c, "RxPause"),
131 + MIB_DESC(1, 0x90, "RxPktSz64"),
132 + MIB_DESC(1, 0x94, "RxPktSz65To127"),
133 + MIB_DESC(1, 0x98, "RxPktSz128To255"),
134 + MIB_DESC(1, 0x9c, "RxPktSz256To511"),
135 + MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
136 + MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
137 + MIB_DESC(2, 0xa8, "RxBytes"),
138 + MIB_DESC(1, 0xb0, "RxCtrlDrop"),
139 + MIB_DESC(1, 0xb4, "RxIngressDrop"),
140 + MIB_DESC(1, 0xb8, "RxArlDrop"),
143 +static struct mt7530_priv *lpriv;
144 +static void mt7530_port_disable(struct dsa_switch *ds, int port,
145 + struct phy_device *phy);
146 +static int mt7530_cpu_port_enable(struct mt7530_priv *priv,
150 +mt7623_trgmii_write(struct mt7530_priv *priv, u32 reg, u32 val)
154 + ret = regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
157 + "failed to priv write register\n");
162 +mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
167 + ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
170 + "failed to priv read register\n");
178 +mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
183 + val = mt7623_trgmii_read(priv, reg);
186 + mt7623_trgmii_write(priv, reg, val);
190 +mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
192 + mt7623_trgmii_rmw(priv, reg, 0, val);
196 +mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
198 + mt7623_trgmii_rmw(priv, reg, val, 0);
202 +core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
204 + struct mii_bus *bus = priv->bus;
207 + /* Write the desired MMD Devad */
208 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
212 + /* Write the desired MMD register address */
213 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
217 + /* Select the Function : DATA with no post increment */
218 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
222 + /* Read the content of the MMD's selected register */
223 + value = bus->read(bus, 0, MII_MMD_DATA);
227 + dev_err(&bus->dev, "failed to read mmd register\n");
233 +core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
234 + int devad, u32 data)
236 + struct mii_bus *bus = priv->bus;
239 + /* Write the desired MMD Devad */
240 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
244 + /* Write the desired MMD register address */
245 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
249 + /* Select the Function : DATA with no post increment */
250 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
254 + /* Write the data into MMD's selected register */
255 + ret = bus->write(bus, 0, MII_MMD_DATA, data);
259 + "failed to write mmd register\n");
264 +core_write(struct mt7530_priv *priv, u32 reg, u32 val)
266 + struct mii_bus *bus = priv->bus;
268 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
270 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
272 + mutex_unlock(&bus->mdio_lock);
276 +core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
278 + struct mii_bus *bus = priv->bus;
281 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
283 + val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
286 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
288 + mutex_unlock(&bus->mdio_lock);
292 +core_set(struct mt7530_priv *priv, u32 reg, u32 val)
294 + core_rmw(priv, reg, 0, val);
298 +core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
300 + core_rmw(priv, reg, val, 0);
304 +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
306 + struct mii_bus *bus = priv->bus;
307 + u16 page, r, lo, hi;
310 + page = (reg >> 6) & 0x3ff;
311 + r = (reg >> 2) & 0xf;
315 + /* MT7530 uses 31 as the pseudo port */
316 + ret = bus->write(bus, 0x1f, 0x1f, page);
320 + ret = bus->write(bus, 0x1f, r, lo);
324 + ret = bus->write(bus, 0x1f, 0x10, hi);
328 + "failed to write mt7530 register\n");
333 +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
335 + struct mii_bus *bus = priv->bus;
336 + u16 page, r, lo, hi;
339 + page = (reg >> 6) & 0x3ff;
340 + r = (reg >> 2) & 0xf;
342 + /* MT7530 uses 31 as the pseudo port */
343 + ret = bus->write(bus, 0x1f, 0x1f, page);
346 + "failed to read mt7530 register\n");
350 + lo = bus->read(bus, 0x1f, r);
351 + hi = bus->read(bus, 0x1f, 0x10);
353 + return (hi << 16) | (lo & 0xffff);
357 +mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
359 + struct mii_bus *bus = priv->bus;
361 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
363 + mt7530_mii_write(priv, reg, val);
365 + mutex_unlock(&bus->mdio_lock);
369 +_mt7530_read(u32 reg)
371 + struct mt7530_priv *priv = lpriv;
372 + struct mii_bus *bus = priv->bus;
375 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
377 + val = mt7530_mii_read(priv, reg);
379 + mutex_unlock(&bus->mdio_lock);
385 +mt7530_read(struct mt7530_priv *priv, u32 reg)
387 + return _mt7530_read(reg);
391 +mt7530_rmw(struct mt7530_priv *priv, u32 reg,
394 + struct mii_bus *bus = priv->bus;
397 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
399 + val = mt7530_mii_read(priv, reg);
402 + mt7530_mii_write(priv, reg, val);
404 + mutex_unlock(&bus->mdio_lock);
408 +mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
410 + mt7530_rmw(priv, reg, 0, val);
414 +mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
416 + mt7530_rmw(priv, reg, val, 0);
420 +mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
425 + /* Set the command operating upon the MAC address entries */
426 + val = ATC_BUSY | ATC_MAT(0) | cmd;
427 + mt7530_write(priv, MT7530_ATC, val);
429 + ret = readx_poll_timeout(_mt7530_read, MT7530_ATC, val,
430 + !(val & ATC_BUSY), 20, 20000);
432 + dev_err(priv->dev, "reset timeout\n");
436 + /* Additional sanity for read command if the specified
439 + val = mt7530_read(priv, MT7530_ATC);
440 + if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
450 +mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
455 + /* Read from ARL table into an array */
456 + for (i = 0; i < 3; i++) {
457 + reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
459 + dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
460 + __func__, __LINE__, i, reg[i]);
463 + fdb->vid = (reg[1] >> CVID) & CVID_MASK;
464 + fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
465 + fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
466 + fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
467 + fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
468 + fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
469 + fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
470 + fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
471 + fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
472 + fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
476 +mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
477 + u8 port_mask, const u8 *mac,
480 + u32 reg[3] = { 0 };
483 + reg[1] |= vid & CVID_MASK;
484 + reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
485 + reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
486 + /* STATIC_ENT indicate that entry is static wouldn't
487 + * be aged out and STATIC_EMP specified as erasing an
490 + reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
491 + reg[1] |= mac[5] << MAC_BYTE_5;
492 + reg[1] |= mac[4] << MAC_BYTE_4;
493 + reg[0] |= mac[3] << MAC_BYTE_3;
494 + reg[0] |= mac[2] << MAC_BYTE_2;
495 + reg[0] |= mac[1] << MAC_BYTE_1;
496 + reg[0] |= mac[0] << MAC_BYTE_0;
498 + /* Write array into the ARL table */
499 + for (i = 0; i < 3; i++)
500 + mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
504 +mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
506 + struct mt7530_priv *priv = ds->priv;
507 + u32 ncpo1, ssc_delta, trgint, i;
510 + case PHY_INTERFACE_MODE_RGMII:
515 + case PHY_INTERFACE_MODE_TRGMII:
521 + dev_err(priv->dev, "xMII mode %d not supported\n", mode);
525 + mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
526 + P6_INTF_MODE(trgint));
528 + /* Lower Tx Driving for TRGMII path */
529 + for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
530 + mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
531 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
533 + /* Setup core clock for MT7530 */
535 + /* Disable MT7530 core clock */
536 + core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
538 + /* Disable PLL, since phy_device has not yet been created
539 + * provided for phy_[read,write]_mmd_indirect is called, we
540 + * provide our own core_write_mmd_indirect to complete this
543 + core_write_mmd_indirect(priv,
548 + /* Set core clock into 500Mhz */
549 + core_write(priv, CORE_GSWPLL_GRP2,
550 + RG_GSWPLL_POSDIV_500M(1) |
551 + RG_GSWPLL_FBKDIV_500M(25));
554 + core_write(priv, CORE_GSWPLL_GRP1,
556 + RG_GSWPLL_POSDIV_200M(2) |
557 + RG_GSWPLL_FBKDIV_200M(32));
559 + /* Enable MT7530 core clock */
560 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
563 + /* Setup the MT7530 TRGMII Tx Clock */
564 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
565 + core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
566 + core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
567 + core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
568 + core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
569 + core_write(priv, CORE_PLL_GROUP4,
570 + RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
571 + RG_SYSPLL_BIAS_LPF_EN);
572 + core_write(priv, CORE_PLL_GROUP2,
573 + RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
574 + RG_SYSPLL_POSDIV(1));
575 + core_write(priv, CORE_PLL_GROUP7,
576 + RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
577 + RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
578 + core_set(priv, CORE_TRGMII_GSW_CLK_CG,
579 + REG_GSWCK_EN | REG_TRGMIICK_EN);
582 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
583 + mt7530_rmw(priv, MT7530_TRGMII_RD(i),
584 + RD_TAP_MASK, RD_TAP(16));
586 + mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
592 +mt7623_pad_clk_setup(struct dsa_switch *ds)
594 + struct mt7530_priv *priv = ds->priv;
597 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
598 + mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
599 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
601 + mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
602 + mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
608 +mt7530_mib_reset(struct dsa_switch *ds)
610 + struct mt7530_priv *priv = ds->priv;
612 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
613 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
617 +mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
619 + u32 mask = PMCR_TX_EN | PMCR_RX_EN;
622 + mt7530_set(priv, MT7530_PMCR_P(port), mask);
624 + mt7530_clear(priv, MT7530_PMCR_P(port), mask);
628 +mt7530_setup(struct dsa_switch *ds)
630 + struct mt7530_priv *priv = ds->priv;
633 + struct device_node *dn;
635 + /* The parent node of master_netdev which holds the common system
636 + * controller also is the container for two GMACs nodes representing
637 + * as two netdev instances.
639 + dn = ds->master_netdev->dev.of_node->parent;
640 + priv->ethernet = syscon_node_to_regmap(dn);
641 + if (IS_ERR(priv->ethernet))
642 + return PTR_ERR(priv->ethernet);
644 + regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
645 + ret = regulator_enable(priv->core_pwr);
648 + "Failed to enable core power: %d\n", ret);
652 + regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
653 + ret = regulator_enable(priv->io_pwr);
655 + dev_err(priv->dev, "Failed to enable io pwr: %d\n",
660 + /* Reset whole chip through gpio pin or memory-mapped registers for
661 + * different type of hardware
664 + reset_control_assert(priv->rstc);
665 + usleep_range(1000, 1100);
666 + reset_control_deassert(priv->rstc);
668 + gpiod_set_value_cansleep(priv->reset, 0);
669 + usleep_range(1000, 1100);
670 + gpiod_set_value_cansleep(priv->reset, 1);
673 + /* Waiting for MT7530 got to stable */
674 + ret = readx_poll_timeout(_mt7530_read, MT7530_HWTRAP, val, val != 0,
677 + dev_err(priv->dev, "reset timeout\n");
681 + id = mt7530_read(priv, MT7530_CREV);
682 + id >>= CHIP_NAME_SHIFT;
683 + if (id != MT7530_ID) {
684 + dev_err(priv->dev, "chip %x can't be supported\n", id);
688 + /* Reset the switch through internal reset */
689 + mt7530_write(priv, MT7530_SYS_CTRL,
690 + SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
693 + /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
694 + val = mt7530_read(priv, MT7530_MHWTRAP);
695 + val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
696 + val |= MHWTRAP_MANUAL;
697 + mt7530_write(priv, MT7530_MHWTRAP, val);
699 + /* Enable and reset MIB counters */
700 + mt7530_mib_reset(ds);
702 + mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
704 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
705 + /* Disable forwarding by default on all ports */
706 + mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
709 + if (dsa_is_cpu_port(ds, i))
710 + mt7530_cpu_port_enable(priv, i);
712 + mt7530_port_disable(ds, i, NULL);
715 + /* Flush the FDB table */
716 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
723 +static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
725 + struct mt7530_priv *priv = ds->priv;
727 + return mdiobus_read_nested(priv->bus, port, regnum);
730 +int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
732 + struct mt7530_priv *priv = ds->priv;
734 + return mdiobus_write_nested(priv->bus, port, regnum, val);
738 +mt7530_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
742 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
743 + strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
748 +mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
751 + struct mt7530_priv *priv = ds->priv;
752 + const struct mt7530_mib_desc *mib;
756 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
757 + mib = &mt7530_mib[i];
758 + reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
760 + data[i] = mt7530_read(priv, reg);
761 + if (mib->size == 2) {
762 + hi = mt7530_read(priv, reg + 4);
763 + data[i] |= hi << 32;
769 +mt7530_get_sset_count(struct dsa_switch *ds)
771 + return ARRAY_SIZE(mt7530_mib);
774 +static void mt7530_adjust_link(struct dsa_switch *ds, int port,
775 + struct phy_device *phydev)
777 + struct mt7530_priv *priv = ds->priv;
779 + if (phy_is_pseudo_fixed_link(phydev)) {
780 + dev_dbg(priv->dev, "phy-mode for master device = %x\n",
781 + phydev->interface);
783 + /* Setup TX circuit incluing relevant PAD and driving */
784 + mt7530_pad_clk_setup(ds, phydev->interface);
786 + /* Setup RX circuit, relevant PAD and driving on the host
787 + * which must be placed after the setup on the device side is
790 + mt7623_pad_clk_setup(ds);
795 +mt7530_cpu_port_enable(struct mt7530_priv *priv,
798 + /* Enable Mediatek header mode on the cpu port */
799 + mt7530_write(priv, MT7530_PVC_P(port),
802 + /* Setup the MAC by default for the cpu port */
803 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
805 + /* Disable auto learning on the cpu port */
806 + mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
808 + /* Unknown unicast frame fordwarding to the cpu port */
809 + mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
811 + /* CPU port gets connected to all user ports of
814 + mt7530_write(priv, MT7530_PCR_P(port),
815 + PCR_MATRIX(priv->ds->enabled_port_mask));
821 +mt7530_port_enable(struct dsa_switch *ds, int port,
822 + struct phy_device *phy)
824 + struct mt7530_priv *priv = ds->priv;
826 + mutex_lock(&priv->reg_mutex);
828 + /* Setup the MAC for the user port */
829 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
831 + /* Allow the user port gets connected to the cpu port and also
832 + * restore the port matrix if the port is the member of a certain
835 + priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
836 + priv->ports[port].enable = true;
837 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
838 + priv->ports[port].pm);
839 + mt7530_port_set_status(priv, port, 1);
841 + mutex_unlock(&priv->reg_mutex);
847 +mt7530_port_disable(struct dsa_switch *ds, int port,
848 + struct phy_device *phy)
850 + struct mt7530_priv *priv = ds->priv;
852 + mutex_lock(&priv->reg_mutex);
854 + /* Clear up all port matrix which could be restored in the next
855 + * enablement for the port.
857 + priv->ports[port].enable = false;
858 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
860 + mt7530_port_set_status(priv, port, 0);
862 + mutex_unlock(&priv->reg_mutex);
866 +mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
868 + struct mt7530_priv *priv = ds->priv;
872 + case BR_STATE_DISABLED:
873 + stp_state = MT7530_STP_DISABLED;
875 + case BR_STATE_BLOCKING:
876 + stp_state = MT7530_STP_BLOCKING;
878 + case BR_STATE_LISTENING:
879 + stp_state = MT7530_STP_LISTENING;
881 + case BR_STATE_LEARNING:
882 + stp_state = MT7530_STP_LEARNING;
884 + case BR_STATE_FORWARDING:
886 + stp_state = MT7530_STP_FORWARDING;
890 + mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
894 +mt7530_port_bridge_join(struct dsa_switch *ds, int port,
895 + struct net_device *bridge)
897 + struct mt7530_priv *priv = ds->priv;
898 + u32 port_bitmap = BIT(MT7530_CPU_PORT);
901 + mutex_lock(&priv->reg_mutex);
903 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
904 + /* Add this port to the port matrix of the other ports in the
905 + * same bridge. If the port is disabled, port matrix is kept
906 + * and not being setup until the port becomes enabled.
908 + if (ds->enabled_port_mask & BIT(i) && i != port) {
909 + if (ds->ports[i].bridge_dev != bridge)
911 + if (priv->ports[i].enable)
912 + mt7530_set(priv, MT7530_PCR_P(i),
913 + PCR_MATRIX(BIT(port)));
914 + priv->ports[i].pm |= PCR_MATRIX(BIT(port));
916 + port_bitmap |= BIT(i);
920 + /* Add the all other ports to this port matrix. */
921 + if (priv->ports[port].enable)
922 + mt7530_rmw(priv, MT7530_PCR_P(port),
923 + PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
924 + priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
926 + mutex_unlock(&priv->reg_mutex);
932 +mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
933 + struct net_device *bridge)
935 + struct mt7530_priv *priv = ds->priv;
938 + mutex_lock(&priv->reg_mutex);
940 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
941 + /* Remove this port from the port matrix of the other ports
942 + * in the same bridge. If the port is disabled, port matrix
943 + * is kept and not being setup until the port becomes enabled.
945 + if (ds->enabled_port_mask & BIT(i) && i != port) {
946 + if (ds->ports[i].bridge_dev != bridge)
948 + if (priv->ports[i].enable)
949 + mt7530_clear(priv, MT7530_PCR_P(i),
950 + PCR_MATRIX(BIT(port)));
951 + priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
955 + /* Set the cpu port to be the only one in the port matrix of
958 + if (priv->ports[port].enable)
959 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
960 + PCR_MATRIX(BIT(MT7530_CPU_PORT)));
961 + priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
963 + mutex_unlock(&priv->reg_mutex);
967 +mt7530_port_fdb_prepare(struct dsa_switch *ds, int port,
968 + const struct switchdev_obj_port_fdb *fdb,
969 + struct switchdev_trans *trans)
971 + struct mt7530_priv *priv = ds->priv;
974 + /* Because auto-learned entrie shares the same FDB table.
975 + * an entry is reserved with no port_mask to make sure fdb_add
976 + * is called while the entry is still available.
978 + mutex_lock(&priv->reg_mutex);
979 + mt7530_fdb_write(priv, fdb->vid, 0, fdb->addr, -1, STATIC_ENT);
980 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
981 + mutex_unlock(&priv->reg_mutex);
987 +mt7530_port_fdb_add(struct dsa_switch *ds, int port,
988 + const struct switchdev_obj_port_fdb *fdb,
989 + struct switchdev_trans *trans)
991 + struct mt7530_priv *priv = ds->priv;
992 + u8 port_mask = BIT(port);
994 + mutex_lock(&priv->reg_mutex);
995 + mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_ENT);
996 + mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
997 + mutex_unlock(&priv->reg_mutex);
1001 +mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1002 + const struct switchdev_obj_port_fdb *fdb)
1004 + struct mt7530_priv *priv = ds->priv;
1006 + u8 port_mask = BIT(port);
1008 + mutex_lock(&priv->reg_mutex);
1009 + mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_EMP);
1010 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1011 + mutex_unlock(&priv->reg_mutex);
1017 +mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1018 + struct switchdev_obj_port_fdb *fdb,
1019 + int (*cb)(struct switchdev_obj *obj))
1021 + struct mt7530_priv *priv = ds->priv;
1022 + struct mt7530_fdb _fdb = { 0 };
1023 + int cnt = MT7530_NUM_FDB_RECORDS;
1027 + mutex_lock(&priv->reg_mutex);
1029 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1034 + if (rsp & ATC_SRCH_HIT) {
1035 + mt7530_fdb_read(priv, &_fdb);
1036 + if (_fdb.port_mask & BIT(port)) {
1037 + ether_addr_copy(fdb->addr, _fdb.mac);
1038 + fdb->vid = _fdb.vid;
1039 + fdb->ndm_state = _fdb.noarp ?
1040 + NUD_NOARP : NUD_REACHABLE;
1041 + ret = cb(&fdb->obj);
1047 + !(rsp & ATC_SRCH_END) &&
1048 + !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1050 + mutex_unlock(&priv->reg_mutex);
1055 +static enum dsa_tag_protocol
1056 +mtk_get_tag_protocol(struct dsa_switch *ds)
1058 + struct mt7530_priv *priv = ds->priv;
1060 + if (!dsa_is_cpu_port(ds, MT7530_CPU_PORT)) {
1061 + dev_warn(priv->dev,
1062 + "port not matched with tagging CPU port\n");
1063 + return DSA_TAG_PROTO_NONE;
1065 + return DSA_TAG_PROTO_MTK;
1069 +static struct dsa_switch_ops mt7530_switch_ops = {
1070 + .get_tag_protocol = mtk_get_tag_protocol,
1071 + .setup = mt7530_setup,
1072 + .get_strings = mt7530_get_strings,
1073 + .phy_read = mt7530_phy_read,
1074 + .phy_write = mt7530_phy_write,
1075 + .get_ethtool_stats = mt7530_get_ethtool_stats,
1076 + .get_sset_count = mt7530_get_sset_count,
1077 + .adjust_link = mt7530_adjust_link,
1078 + .port_enable = mt7530_port_enable,
1079 + .port_disable = mt7530_port_disable,
1080 + .port_stp_state_set = mt7530_stp_state_set,
1081 + .port_bridge_join = mt7530_port_bridge_join,
1082 + .port_bridge_leave = mt7530_port_bridge_leave,
1083 + .port_fdb_prepare = mt7530_port_fdb_prepare,
1084 + .port_fdb_add = mt7530_port_fdb_add,
1085 + .port_fdb_del = mt7530_port_fdb_del,
1086 + .port_fdb_dump = mt7530_port_fdb_dump,
1090 +mt7530_probe(struct mdio_device *mdiodev)
1092 + struct mt7530_priv *priv;
1093 + struct device_node *dn;
1095 + dn = mdiodev->dev.of_node;
1097 + priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1101 + priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
1105 + /* Use medatek,mcm property to distinguish hardware type that would
1106 + * casues a little bit differences on power-on sequence.
1108 + priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1110 + dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1112 + priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1113 + if (IS_ERR(priv->rstc)) {
1114 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1115 + return PTR_ERR(priv->rstc);
1119 + priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1120 + if (IS_ERR(priv->core_pwr))
1121 + return PTR_ERR(priv->core_pwr);
1123 + priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1124 + if (IS_ERR(priv->io_pwr))
1125 + return PTR_ERR(priv->io_pwr);
1127 + /* Not MCM that indicates switch works as the remote standalone
1128 + * integrated circuit so the GPIO pin would be used to complete
1129 + * the reset, otherwise memory-mapped register accessing used
1130 + * through syscon provides in the case of MCM.
1133 + priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1135 + if (IS_ERR(priv->reset)) {
1136 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1137 + return PTR_ERR(priv->reset);
1141 + priv->bus = mdiodev->bus;
1142 + priv->dev = &mdiodev->dev;
1143 + priv->ds->priv = priv;
1144 + priv->ds->ops = &mt7530_switch_ops;
1145 + mutex_init(&priv->reg_mutex);
1147 + dev_set_drvdata(&mdiodev->dev, priv);
1149 + return dsa_register_switch(priv->ds, &mdiodev->dev);
1153 +mt7530_remove(struct mdio_device *mdiodev)
1155 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1158 + ret = regulator_disable(priv->core_pwr);
1160 + dev_err(priv->dev,
1161 + "Failed to disable core power: %d\n", ret);
1163 + ret = regulator_disable(priv->io_pwr);
1165 + dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1168 + dsa_unregister_switch(priv->ds);
1169 + mutex_destroy(&priv->reg_mutex);
1172 +static const struct of_device_id mt7530_of_match[] = {
1173 + { .compatible = "mediatek,mt7530" },
1174 + { /* sentinel */ },
1177 +static struct mdio_driver mt7530_mdio_driver = {
1178 + .probe = mt7530_probe,
1179 + .remove = mt7530_remove,
1180 + .mdiodrv.driver = {
1182 + .of_match_table = mt7530_of_match,
1186 +mdio_module_driver(mt7530_mdio_driver);
1188 +MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1189 +MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1190 +MODULE_LICENSE("GPL");
1191 +MODULE_ALIAS("platform:mediatek-mt7530");
1193 +++ b/drivers/net/dsa/mt7530.h
1196 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
1198 + * This program is free software; you can redistribute it and/or modify
1199 + * it under the terms of the GNU General Public License version 2 as
1200 + * published by the Free Software Foundation.
1202 + * This program is distributed in the hope that it will be useful,
1203 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1204 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1205 + * GNU General Public License for more details.
1211 +#define MT7530_NUM_PORTS 7
1212 +#define MT7530_CPU_PORT 6
1213 +#define MT7530_NUM_FDB_RECORDS 2048
1215 +#define NUM_TRGMII_CTRL 5
1217 +#define TRGMII_BASE(x) (0x10000 + (x))
1219 +/* Registers to ethsys access */
1220 +#define ETHSYS_CLKCFG0 0x2c
1221 +#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
1223 +#define SYSC_REG_RSTCTRL 0x34
1224 +#define RESET_MCM BIT(2)
1226 +/* Registers to mac forward control for unknown frames */
1227 +#define MT7530_MFC 0x10
1228 +#define BC_FFP(x) (((x) & 0xff) << 24)
1229 +#define UNM_FFP(x) (((x) & 0xff) << 16)
1230 +#define UNU_FFP(x) (((x) & 0xff) << 8)
1231 +#define UNU_FFP_MASK UNU_FFP(~0)
1233 +/* Registers for address table access */
1234 +#define MT7530_ATA1 0x74
1235 +#define STATIC_EMP 0
1236 +#define STATIC_ENT 3
1237 +#define MT7530_ATA2 0x78
1239 +/* Register for address table write data */
1240 +#define MT7530_ATWD 0x7c
1242 +/* Register for address table control */
1243 +#define MT7530_ATC 0x80
1244 +#define ATC_HASH (((x) & 0xfff) << 16)
1245 +#define ATC_BUSY BIT(15)
1246 +#define ATC_SRCH_END BIT(14)
1247 +#define ATC_SRCH_HIT BIT(13)
1248 +#define ATC_INVALID BIT(12)
1249 +#define ATC_MAT(x) (((x) & 0xf) << 8)
1250 +#define ATC_MAT_MACTAB ATC_MAT(0)
1252 +enum mt7530_fdb_cmd {
1253 + MT7530_FDB_READ = 0,
1254 + MT7530_FDB_WRITE = 1,
1255 + MT7530_FDB_FLUSH = 2,
1256 + MT7530_FDB_START = 4,
1257 + MT7530_FDB_NEXT = 5,
1260 +/* Registers for table search read address */
1261 +#define MT7530_TSRA1 0x84
1262 +#define MAC_BYTE_0 24
1263 +#define MAC_BYTE_1 16
1264 +#define MAC_BYTE_2 8
1265 +#define MAC_BYTE_3 0
1266 +#define MAC_BYTE_MASK 0xff
1268 +#define MT7530_TSRA2 0x88
1269 +#define MAC_BYTE_4 24
1270 +#define MAC_BYTE_5 16
1272 +#define CVID_MASK 0xfff
1274 +#define MT7530_ATRD 0x8C
1275 +#define AGE_TIMER 24
1276 +#define AGE_TIMER_MASK 0xff
1278 +#define PORT_MAP_MASK 0xff
1279 +#define ENT_STATUS 2
1280 +#define ENT_STATUS_MASK 0x3
1282 +/* Register for vlan table control */
1283 +#define MT7530_VTCR 0x90
1284 +#define VTCR_BUSY BIT(31)
1285 +#define VTCR_FUNC (((x) & 0xf) << 12)
1286 +#define VTCR_FUNC_RD_VID 0x1
1287 +#define VTCR_FUNC_WR_VID 0x2
1288 +#define VTCR_FUNC_INV_VID 0x3
1289 +#define VTCR_FUNC_VAL_VID 0x4
1290 +#define VTCR_VID ((x) & 0xfff)
1292 +/* Register for setup vlan and acl write data */
1293 +#define MT7530_VAWD1 0x94
1294 +#define PORT_STAG BIT(31)
1295 +#define IVL_MAC BIT(30)
1296 +#define PORT_MEM(x) (((x) & 0xff) << 16)
1297 +#define VALID BIT(1)
1299 +#define MT7530_VAWD2 0x98
1301 +/* Register for port STP state control */
1302 +#define MT7530_SSP_P(x) (0x2000 + ((x) * 0x100))
1303 +#define FID_PST(x) ((x) & 0x3)
1304 +#define FID_PST_MASK FID_PST(0x3)
1306 +enum mt7530_stp_state {
1307 + MT7530_STP_DISABLED = 0,
1308 + MT7530_STP_BLOCKING = 1,
1309 + MT7530_STP_LISTENING = 1,
1310 + MT7530_STP_LEARNING = 2,
1311 + MT7530_STP_FORWARDING = 3
1314 +/* Register for port control */
1315 +#define MT7530_PCR_P(x) (0x2004 + ((x) * 0x100))
1316 +#define PORT_VLAN(x) ((x) & 0x3)
1317 +#define PCR_MATRIX(x) (((x) & 0xff) << 16)
1318 +#define PORT_PRI(x) (((x) & 0x7) << 24)
1319 +#define EG_TAG(x) (((x) & 0x3) << 28)
1320 +#define PCR_MATRIX_MASK PCR_MATRIX(0xff)
1321 +#define PCR_MATRIX_CLR PCR_MATRIX(0)
1323 +/* Register for port security control */
1324 +#define MT7530_PSC_P(x) (0x200c + ((x) * 0x100))
1325 +#define SA_DIS BIT(4)
1327 +/* Register for port vlan control */
1328 +#define MT7530_PVC_P(x) (0x2010 + ((x) * 0x100))
1329 +#define PORT_SPEC_TAG BIT(5)
1330 +#define VLAN_ATTR(x) (((x) & 0x3) << 6)
1331 +#define STAG_VPID (((x) & 0xffff) << 16)
1333 +/* Register for port port-and-protocol based vlan 1 control */
1334 +#define MT7530_PPBV1_P(x) (0x2014 + ((x) * 0x100))
1336 +/* Register for port MAC control register */
1337 +#define MT7530_PMCR_P(x) (0x3000 + ((x) * 0x100))
1338 +#define PMCR_IFG_XMIT(x) (((x) & 0x3) << 18)
1339 +#define PMCR_MAC_MODE BIT(16)
1340 +#define PMCR_FORCE_MODE BIT(15)
1341 +#define PMCR_TX_EN BIT(14)
1342 +#define PMCR_RX_EN BIT(13)
1343 +#define PMCR_BACKOFF_EN BIT(9)
1344 +#define PMCR_BACKPR_EN BIT(8)
1345 +#define PMCR_TX_FC_EN BIT(5)
1346 +#define PMCR_RX_FC_EN BIT(4)
1347 +#define PMCR_FORCE_SPEED_1000 BIT(3)
1348 +#define PMCR_FORCE_FDX BIT(1)
1349 +#define PMCR_FORCE_LNK BIT(0)
1350 +#define PMCR_COMMON_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1351 + PMCR_BACKOFF_EN | PMCR_BACKPR_EN | \
1352 + PMCR_TX_EN | PMCR_RX_EN | \
1353 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1354 +#define PMCR_CPUP_LINK (PMCR_COMMON_LINK | PMCR_FORCE_MODE | \
1355 + PMCR_FORCE_SPEED_1000 | \
1356 + PMCR_FORCE_FDX | \
1358 +#define PMCR_USERP_LINK PMCR_COMMON_LINK
1359 +#define PMCR_FIXED_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1360 + PMCR_FORCE_MODE | PMCR_TX_EN | \
1361 + PMCR_RX_EN | PMCR_BACKPR_EN | \
1362 + PMCR_BACKOFF_EN | \
1363 + PMCR_FORCE_SPEED_1000 | \
1364 + PMCR_FORCE_FDX | \
1366 +#define PMCR_FIXED_LINK_FC (PMCR_FIXED_LINK | \
1367 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1369 +#define MT7530_PMSR_P(x) (0x3008 + (x) * 0x100)
1371 +/* Register for MIB */
1372 +#define MT7530_PORT_MIB_COUNTER(x) (0x4000 + (x) * 0x100)
1373 +#define MT7530_MIB_CCR 0x4fe0
1374 +#define CCR_MIB_ENABLE BIT(31)
1375 +#define CCR_RX_OCT_CNT_GOOD BIT(7)
1376 +#define CCR_RX_OCT_CNT_BAD BIT(6)
1377 +#define CCR_TX_OCT_CNT_GOOD BIT(5)
1378 +#define CCR_TX_OCT_CNT_BAD BIT(4)
1379 +#define CCR_MIB_FLUSH (CCR_RX_OCT_CNT_GOOD | \
1380 + CCR_RX_OCT_CNT_BAD | \
1381 + CCR_TX_OCT_CNT_GOOD | \
1382 + CCR_TX_OCT_CNT_BAD)
1383 +#define CCR_MIB_ACTIVATE (CCR_MIB_ENABLE | \
1384 + CCR_RX_OCT_CNT_GOOD | \
1385 + CCR_RX_OCT_CNT_BAD | \
1386 + CCR_TX_OCT_CNT_GOOD | \
1387 + CCR_TX_OCT_CNT_BAD)
1388 +/* Register for system reset */
1389 +#define MT7530_SYS_CTRL 0x7000
1390 +#define SYS_CTRL_PHY_RST BIT(2)
1391 +#define SYS_CTRL_SW_RST BIT(1)
1392 +#define SYS_CTRL_REG_RST BIT(0)
1394 +/* Register for hw trap status */
1395 +#define MT7530_HWTRAP 0x7800
1397 +/* Register for hw trap modification */
1398 +#define MT7530_MHWTRAP 0x7804
1399 +#define MHWTRAP_MANUAL BIT(16)
1400 +#define MHWTRAP_P5_MAC_SEL BIT(13)
1401 +#define MHWTRAP_P6_DIS BIT(8)
1402 +#define MHWTRAP_P5_RGMII_MODE BIT(7)
1403 +#define MHWTRAP_P5_DIS BIT(6)
1404 +#define MHWTRAP_PHY_ACCESS BIT(5)
1406 +/* Register for TOP signal control */
1407 +#define MT7530_TOP_SIG_CTRL 0x7808
1408 +#define TOP_SIG_CTRL_NORMAL (BIT(17) | BIT(16))
1410 +#define MT7530_IO_DRV_CR 0x7810
1411 +#define P5_IO_CLK_DRV(x) ((x) & 0x3)
1412 +#define P5_IO_DATA_DRV(x) (((x) & 0x3) << 4)
1414 +#define MT7530_P6ECR 0x7830
1415 +#define P6_INTF_MODE_MASK 0x3
1416 +#define P6_INTF_MODE(x) ((x) & 0x3)
1418 +/* Registers for TRGMII on the both side */
1419 +#define MT7530_TRGMII_RCK_CTRL 0x7a00
1420 +#define GSW_TRGMII_RCK_CTRL 0x300
1421 +#define RX_RST BIT(31)
1422 +#define RXC_DQSISEL BIT(30)
1423 +#define DQSI1_TAP_MASK (0x7f << 8)
1424 +#define DQSI0_TAP_MASK 0x7f
1425 +#define DQSI1_TAP(x) (((x) & 0x7f) << 8)
1426 +#define DQSI0_TAP(x) ((x) & 0x7f)
1428 +#define MT7530_TRGMII_RCK_RTT 0x7a04
1429 +#define GSW_TRGMII_RCK_RTT 0x304
1430 +#define DQS1_GATE BIT(31)
1431 +#define DQS0_GATE BIT(30)
1433 +#define MT7530_TRGMII_RD(x) (0x7a10 + (x) * 8)
1434 +#define GSW_TRGMII_RD(x) (0x310 + (x) * 8)
1435 +#define BSLIP_EN BIT(31)
1436 +#define EDGE_CHK BIT(30)
1437 +#define RD_TAP_MASK 0x7f
1438 +#define RD_TAP(x) ((x) & 0x7f)
1440 +#define GSW_TRGMII_TXCTRL 0x340
1441 +#define MT7530_TRGMII_TXCTRL 0x7a40
1442 +#define TRAIN_TXEN BIT(31)
1443 +#define TXC_INV BIT(30)
1444 +#define TX_RST BIT(28)
1446 +#define MT7530_TRGMII_TD_ODT(i) (0x7a54 + 8 * (i))
1447 +#define GSW_TRGMII_TD_ODT(i) (0x354 + 8 * (i))
1448 +#define TD_DM_DRVP(x) ((x) & 0xf)
1449 +#define TD_DM_DRVN(x) (((x) & 0xf) << 4)
1451 +#define GSW_INTF_MODE 0x390
1452 +#define INTF_MODE_TRGMII BIT(1)
1454 +#define MT7530_TRGMII_TCK_CTRL 0x7a78
1455 +#define TCK_TAP(x) (((x) & 0xf) << 8)
1457 +#define MT7530_P5RGMIIRXCR 0x7b00
1458 +#define CSR_RGMII_EDGE_ALIGN BIT(8)
1459 +#define CSR_RGMII_RXC_0DEG_CFG(x) ((x) & 0xf)
1461 +#define MT7530_P5RGMIITXCR 0x7b04
1462 +#define CSR_RGMII_TXC_CFG(x) ((x) & 0x1f)
1464 +#define MT7530_CREV 0x7ffc
1465 +#define CHIP_NAME_SHIFT 16
1466 +#define MT7530_ID 0x7530
1468 +/* Registers for core PLL access through mmd indirect */
1469 +#define CORE_PLL_GROUP2 0x401
1470 +#define RG_SYSPLL_EN_NORMAL BIT(15)
1471 +#define RG_SYSPLL_VODEN BIT(14)
1472 +#define RG_SYSPLL_LF BIT(13)
1473 +#define RG_SYSPLL_RST_DLY(x) (((x) & 0x3) << 12)
1474 +#define RG_SYSPLL_LVROD_EN BIT(10)
1475 +#define RG_SYSPLL_PREDIV(x) (((x) & 0x3) << 8)
1476 +#define RG_SYSPLL_POSDIV(x) (((x) & 0x3) << 5)
1477 +#define RG_SYSPLL_FBKSEL BIT(4)
1478 +#define RT_SYSPLL_EN_AFE_OLT BIT(0)
1480 +#define CORE_PLL_GROUP4 0x403
1481 +#define RG_SYSPLL_DDSFBK_EN BIT(12)
1482 +#define RG_SYSPLL_BIAS_EN BIT(11)
1483 +#define RG_SYSPLL_BIAS_LPF_EN BIT(10)
1485 +#define CORE_PLL_GROUP5 0x404
1486 +#define RG_LCDDS_PCW_NCPO1(x) ((x) & 0xffff)
1488 +#define CORE_PLL_GROUP6 0x405
1489 +#define RG_LCDDS_PCW_NCPO0(x) ((x) & 0xffff)
1491 +#define CORE_PLL_GROUP7 0x406
1492 +#define RG_LCDDS_PWDB BIT(15)
1493 +#define RG_LCDDS_ISO_EN BIT(13)
1494 +#define RG_LCCDS_C(x) (((x) & 0x7) << 4)
1495 +#define RG_LCDDS_PCW_NCPO_CHG BIT(3)
1497 +#define CORE_PLL_GROUP10 0x409
1498 +#define RG_LCDDS_SSC_DELTA(x) ((x) & 0xfff)
1500 +#define CORE_PLL_GROUP11 0x40a
1501 +#define RG_LCDDS_SSC_DELTA1(x) ((x) & 0xfff)
1503 +#define CORE_GSWPLL_GRP1 0x40d
1504 +#define RG_GSWPLL_PREDIV(x) (((x) & 0x3) << 14)
1505 +#define RG_GSWPLL_POSDIV_200M(x) (((x) & 0x3) << 12)
1506 +#define RG_GSWPLL_EN_PRE BIT(11)
1507 +#define RG_GSWPLL_FBKSEL BIT(10)
1508 +#define RG_GSWPLL_BP BIT(9)
1509 +#define RG_GSWPLL_BR BIT(8)
1510 +#define RG_GSWPLL_FBKDIV_200M(x) ((x) & 0xff)
1512 +#define CORE_GSWPLL_GRP2 0x40e
1513 +#define RG_GSWPLL_POSDIV_500M(x) (((x) & 0x3) << 8)
1514 +#define RG_GSWPLL_FBKDIV_500M(x) ((x) & 0xff)
1516 +#define CORE_TRGMII_GSW_CLK_CG 0x410
1517 +#define REG_GSWCK_EN BIT(0)
1518 +#define REG_TRGMIICK_EN BIT(1)
1520 +#define MIB_DESC(_s, _o, _n) \
1527 +struct mt7530_mib_desc {
1528 + unsigned int size;
1529 + unsigned int offset;
1533 +struct mt7530_fdb {
1541 +struct mt7530_port {
1546 +/* struct mt7530_priv - This is the main data structure for holding the state
1548 + * @dev: The device pointer
1549 + * @ds: The pointer to the dsa core structure
1550 + * @bus: The bus used for the device and built-in PHY
1551 + * @rstc: The pointer to reset control used by MCM
1552 + * @ethernet: The regmap used for access TRGMII-based registers
1553 + * @core_pwr: The power supplied into the core
1554 + * @io_pwr: The power supplied into the I/O
1555 + * @reset: The descriptor for GPIO line tied to its reset pin
1556 + * @mcm: Flag for distinguishing if standalone IC or module
1558 + * @ports: Holding the state among ports
1559 + * @reg_mutex: The lock for protecting among process accessing
1562 +struct mt7530_priv {
1563 + struct device *dev;
1564 + struct dsa_switch *ds;
1565 + struct mii_bus *bus;
1566 + struct reset_control *rstc;
1567 + struct regmap *ethernet;
1568 + struct regulator *core_pwr;
1569 + struct regulator *io_pwr;
1570 + struct gpio_desc *reset;
1573 + struct mt7530_port ports[MT7530_NUM_PORTS];
1574 + /* protect among processes for registers access*/
1575 + struct mutex reg_mutex;
1578 +struct mt7530_hw_stats {
1579 + const char *string;
1584 +#endif /* __MT7530_H */