1 From patchwork Wed Mar 29 09:38:23 2017
2 Content-Type: text/plain; charset="utf-8"
4 Content-Transfer-Encoding: 7bit
5 Subject: [net-next, v3,
6 5/5] net-next: dsa: add dsa support for Mediatek MT7530 switch
7 From: sean.wang@mediatek.com
8 X-Patchwork-Id: 9651095
9 Message-Id: <1490780303-18598-6-git-send-email-sean.wang@mediatek.com>
10 To: <andrew@lunn.ch>, <f.fainelli@gmail.com>,
11 <vivien.didelot@savoirfairelinux.com>, <matthias.bgg@gmail.com>,
12 <robh+dt@kernel.org>, <mark.rutland@arm.com>
13 Cc: devicetree@vger.kernel.org, Landen.Chao@mediatek.com, keyhaede@gmail.com,
14 netdev@vger.kernel.org, sean.wang@mediatek.com,
15 linux-kernel@vger.kernel.org,
16 linux-mediatek@lists.infradead.org, objelf@gmail.com, davem@davemloft.net
17 Date: Wed, 29 Mar 2017 17:38:23 +0800
19 From: Sean Wang <sean.wang@mediatek.com>
21 MT7530 is a 7-ports Gigabit Ethernet Switch that could be found on
22 Mediatek router platforms such as MT7623A or MT7623N platform which
23 includes 7-port Gigabit Ethernet MAC and 5-port Gigabit Ethernet PHY.
24 Among these ports, The port from 0 to 4 are the user ports connecting
25 with the remote devices while the port 5 and 6 are the CPU ports
26 connecting into Mediatek Ethernet GMAC.
28 For port 6, it can communicate with the CPU via Mediatek Ethernet GMAC
29 through either the TRGMII or RGMII which could be controlled by phy-mode
30 in the dt-bindings to specify which mode is preferred to use. And for
31 port 5, only RGMII can be specified. However, currently, only port 6 is
32 being supported in this DSA driver.
34 The driver is made with the reference to qca8k and other existing DSA
35 driver. The most of the essential callbacks of the DSA are already
36 support in the driver, including tag insert for user port distinguishing,
37 port control, bridge offloading, STP setup and ethtool operation to allow
38 DSA to model each user port into a standalone netdevice as the other DSA
41 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
42 Signed-off-by: Landen Chao <Landen.Chao@mediatek.com>
44 drivers/net/dsa/Kconfig | 8 +
45 drivers/net/dsa/Makefile | 2 +-
46 drivers/net/dsa/mt7530.c | 1126 ++++++++++++++++++++++++++++++++++++++++++++++
47 drivers/net/dsa/mt7530.h | 390 ++++++++++++++++
48 4 files changed, 1525 insertions(+), 1 deletion(-)
49 create mode 100644 drivers/net/dsa/mt7530.c
50 create mode 100644 drivers/net/dsa/mt7530.h
52 --- a/drivers/net/dsa/Kconfig
53 +++ b/drivers/net/dsa/Kconfig
54 @@ -34,4 +34,12 @@ config NET_DSA_QCA8K
55 This enables support for the Qualcomm Atheros QCA8K Ethernet
58 +config NET_DSA_MT7530
59 + tristate "Mediatek MT7530 Ethernet switch support"
61 + select NET_DSA_TAG_MTK
63 + This enables support for the Mediatek MT7530 Ethernet switch
67 --- a/drivers/net/dsa/Makefile
68 +++ b/drivers/net/dsa/Makefile
70 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
71 obj-$(CONFIG_NET_DSA_BCM_SF2) += bcm_sf2.o
72 obj-$(CONFIG_NET_DSA_QCA8K) += qca8k.o
74 +obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o
78 +++ b/drivers/net/dsa/mt7530.c
81 + * Mediatek MT7530 DSA Switch driver
82 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
84 + * This program is free software; you can redistribute it and/or modify
85 + * it under the terms of the GNU General Public License version 2 as
86 + * published by the Free Software Foundation.
88 + * This program is distributed in the hope that it will be useful,
89 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
90 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91 + * GNU General Public License for more details.
93 +#include <linux/etherdevice.h>
94 +#include <linux/if_bridge.h>
95 +#include <linux/iopoll.h>
96 +#include <linux/mdio.h>
97 +#include <linux/mfd/syscon.h>
98 +#include <linux/module.h>
99 +#include <linux/netdevice.h>
100 +#include <linux/of_gpio.h>
101 +#include <linux/of_mdio.h>
102 +#include <linux/of_net.h>
103 +#include <linux/of_platform.h>
104 +#include <linux/phy.h>
105 +#include <linux/regmap.h>
106 +#include <linux/regulator/consumer.h>
107 +#include <linux/reset.h>
108 +#include <net/dsa.h>
109 +#include <net/switchdev.h>
113 +/* String, offset, and register size in bytes if different from 4 bytes */
114 +static const struct mt7530_mib_desc mt7530_mib[] = {
115 + MIB_DESC(1, 0x00, "TxDrop"),
116 + MIB_DESC(1, 0x04, "TxCrcErr"),
117 + MIB_DESC(1, 0x08, "TxUnicast"),
118 + MIB_DESC(1, 0x0c, "TxMulticast"),
119 + MIB_DESC(1, 0x10, "TxBroadcast"),
120 + MIB_DESC(1, 0x14, "TxCollision"),
121 + MIB_DESC(1, 0x18, "TxSingleCollision"),
122 + MIB_DESC(1, 0x1c, "TxMultipleCollision"),
123 + MIB_DESC(1, 0x20, "TxDeferred"),
124 + MIB_DESC(1, 0x24, "TxLateCollision"),
125 + MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
126 + MIB_DESC(1, 0x2c, "TxPause"),
127 + MIB_DESC(1, 0x30, "TxPktSz64"),
128 + MIB_DESC(1, 0x34, "TxPktSz65To127"),
129 + MIB_DESC(1, 0x38, "TxPktSz128To255"),
130 + MIB_DESC(1, 0x3c, "TxPktSz256To511"),
131 + MIB_DESC(1, 0x40, "TxPktSz512To1023"),
132 + MIB_DESC(1, 0x44, "Tx1024ToMax"),
133 + MIB_DESC(2, 0x48, "TxBytes"),
134 + MIB_DESC(1, 0x60, "RxDrop"),
135 + MIB_DESC(1, 0x64, "RxFiltering"),
136 + MIB_DESC(1, 0x6c, "RxMulticast"),
137 + MIB_DESC(1, 0x70, "RxBroadcast"),
138 + MIB_DESC(1, 0x74, "RxAlignErr"),
139 + MIB_DESC(1, 0x78, "RxCrcErr"),
140 + MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
141 + MIB_DESC(1, 0x80, "RxFragErr"),
142 + MIB_DESC(1, 0x84, "RxOverSzErr"),
143 + MIB_DESC(1, 0x88, "RxJabberErr"),
144 + MIB_DESC(1, 0x8c, "RxPause"),
145 + MIB_DESC(1, 0x90, "RxPktSz64"),
146 + MIB_DESC(1, 0x94, "RxPktSz65To127"),
147 + MIB_DESC(1, 0x98, "RxPktSz128To255"),
148 + MIB_DESC(1, 0x9c, "RxPktSz256To511"),
149 + MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
150 + MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
151 + MIB_DESC(2, 0xa8, "RxBytes"),
152 + MIB_DESC(1, 0xb0, "RxCtrlDrop"),
153 + MIB_DESC(1, 0xb4, "RxIngressDrop"),
154 + MIB_DESC(1, 0xb8, "RxArlDrop"),
157 +static struct mt7530_priv *lpriv;
158 +static void mt7530_port_disable(struct dsa_switch *ds, int port,
159 + struct phy_device *phy);
160 +static int mt7530_cpu_port_enable(struct mt7530_priv *priv,
164 +mt7623_trgmii_write(struct mt7530_priv *priv, u32 reg, u32 val)
168 + ret = regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
171 + "failed to priv write register\n");
176 +mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
181 + ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
184 + "failed to priv read register\n");
192 +mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
197 + val = mt7623_trgmii_read(priv, reg);
200 + mt7623_trgmii_write(priv, reg, val);
204 +mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
206 + mt7623_trgmii_rmw(priv, reg, 0, val);
210 +mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
212 + mt7623_trgmii_rmw(priv, reg, val, 0);
216 +core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
218 + struct mii_bus *bus = priv->bus;
221 + /* Write the desired MMD Devad */
222 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
226 + /* Write the desired MMD register address */
227 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
231 + /* Select the Function : DATA with no post increment */
232 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
236 + /* Read the content of the MMD's selected register */
237 + value = bus->read(bus, 0, MII_MMD_DATA);
241 + dev_err(&bus->dev, "failed to read mmd register\n");
247 +core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
248 + int devad, u32 data)
250 + struct mii_bus *bus = priv->bus;
253 + /* Write the desired MMD Devad */
254 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
258 + /* Write the desired MMD register address */
259 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
263 + /* Select the Function : DATA with no post increment */
264 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
268 + /* Write the data into MMD's selected register */
269 + ret = bus->write(bus, 0, MII_MMD_DATA, data);
273 + "failed to write mmd register\n");
278 +core_write(struct mt7530_priv *priv, u32 reg, u32 val)
280 + struct mii_bus *bus = priv->bus;
282 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
284 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
286 + mutex_unlock(&bus->mdio_lock);
290 +core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
292 + struct mii_bus *bus = priv->bus;
295 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
297 + val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
300 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
302 + mutex_unlock(&bus->mdio_lock);
306 +core_set(struct mt7530_priv *priv, u32 reg, u32 val)
308 + core_rmw(priv, reg, 0, val);
312 +core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
314 + core_rmw(priv, reg, val, 0);
318 +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
320 + struct mii_bus *bus = priv->bus;
321 + u16 page, r, lo, hi;
324 + page = (reg >> 6) & 0x3ff;
325 + r = (reg >> 2) & 0xf;
329 + /* MT7530 uses 31 as the pseudo port */
330 + ret = bus->write(bus, 0x1f, 0x1f, page);
334 + ret = bus->write(bus, 0x1f, r, lo);
338 + ret = bus->write(bus, 0x1f, 0x10, hi);
342 + "failed to write mt7530 register\n");
347 +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
349 + struct mii_bus *bus = priv->bus;
350 + u16 page, r, lo, hi;
353 + page = (reg >> 6) & 0x3ff;
354 + r = (reg >> 2) & 0xf;
356 + /* MT7530 uses 31 as the pseudo port */
357 + ret = bus->write(bus, 0x1f, 0x1f, page);
360 + "failed to read mt7530 register\n");
364 + lo = bus->read(bus, 0x1f, r);
365 + hi = bus->read(bus, 0x1f, 0x10);
367 + return (hi << 16) | (lo & 0xffff);
371 +mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
373 + struct mii_bus *bus = priv->bus;
375 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
377 + mt7530_mii_write(priv, reg, val);
379 + mutex_unlock(&bus->mdio_lock);
383 +_mt7530_read(u32 reg)
385 + struct mt7530_priv *priv = lpriv;
386 + struct mii_bus *bus = priv->bus;
389 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
391 + val = mt7530_mii_read(priv, reg);
393 + mutex_unlock(&bus->mdio_lock);
399 +mt7530_read(struct mt7530_priv *priv, u32 reg)
401 + return _mt7530_read(reg);
405 +mt7530_rmw(struct mt7530_priv *priv, u32 reg,
408 + struct mii_bus *bus = priv->bus;
411 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
413 + val = mt7530_mii_read(priv, reg);
416 + mt7530_mii_write(priv, reg, val);
418 + mutex_unlock(&bus->mdio_lock);
422 +mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
424 + mt7530_rmw(priv, reg, 0, val);
428 +mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
430 + mt7530_rmw(priv, reg, val, 0);
434 +mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
439 + /* Set the command operating upon the MAC address entries */
440 + val = ATC_BUSY | ATC_MAT(0) | cmd;
441 + mt7530_write(priv, MT7530_ATC, val);
443 + ret = readx_poll_timeout(_mt7530_read, MT7530_ATC, val,
444 + !(val & ATC_BUSY), 20, 20000);
446 + dev_err(priv->dev, "reset timeout\n");
450 + /* Additional sanity for read command if the specified
453 + val = mt7530_read(priv, MT7530_ATC);
454 + if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
464 +mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
469 + /* Read from ARL table into an array */
470 + for (i = 0; i < 3; i++) {
471 + reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
473 + dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
474 + __func__, __LINE__, i, reg[i]);
477 + fdb->vid = (reg[1] >> CVID) & CVID_MASK;
478 + fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
479 + fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
480 + fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
481 + fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
482 + fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
483 + fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
484 + fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
485 + fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
486 + fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
490 +mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
491 + u8 port_mask, const u8 *mac,
494 + u32 reg[3] = { 0 };
497 + reg[1] |= vid & CVID_MASK;
498 + reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
499 + reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
500 + /* STATIC_ENT indicate that entry is static wouldn't
501 + * be aged out and STATIC_EMP specified as erasing an
504 + reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
505 + reg[1] |= mac[5] << MAC_BYTE_5;
506 + reg[1] |= mac[4] << MAC_BYTE_4;
507 + reg[0] |= mac[3] << MAC_BYTE_3;
508 + reg[0] |= mac[2] << MAC_BYTE_2;
509 + reg[0] |= mac[1] << MAC_BYTE_1;
510 + reg[0] |= mac[0] << MAC_BYTE_0;
512 + /* Write array into the ARL table */
513 + for (i = 0; i < 3; i++)
514 + mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
518 +mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
520 + struct mt7530_priv *priv = ds->priv;
521 + u32 ncpo1, ssc_delta, trgint, i;
524 + case PHY_INTERFACE_MODE_RGMII:
529 + case PHY_INTERFACE_MODE_TRGMII:
535 + dev_err(priv->dev, "xMII mode %d not supported\n", mode);
539 + mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
540 + P6_INTF_MODE(trgint));
542 + /* Lower Tx Driving for TRGMII path */
543 + for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
544 + mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
545 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
547 + /* Setup core clock for MT7530 */
549 + /* Disable MT7530 core clock */
550 + core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
552 + /* Disable PLL, since phy_device has not yet been created
553 + * provided for phy_[read,write]_mmd_indirect is called, we
554 + * provide our own core_write_mmd_indirect to complete this
557 + core_write_mmd_indirect(priv,
562 + /* Set core clock into 500Mhz */
563 + core_write(priv, CORE_GSWPLL_GRP2,
564 + RG_GSWPLL_POSDIV_500M(1) |
565 + RG_GSWPLL_FBKDIV_500M(25));
568 + core_write(priv, CORE_GSWPLL_GRP1,
570 + RG_GSWPLL_POSDIV_200M(2) |
571 + RG_GSWPLL_FBKDIV_200M(32));
573 + /* Enable MT7530 core clock */
574 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
577 + /* Setup the MT7530 TRGMII Tx Clock */
578 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
579 + core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
580 + core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
581 + core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
582 + core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
583 + core_write(priv, CORE_PLL_GROUP4,
584 + RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
585 + RG_SYSPLL_BIAS_LPF_EN);
586 + core_write(priv, CORE_PLL_GROUP2,
587 + RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
588 + RG_SYSPLL_POSDIV(1));
589 + core_write(priv, CORE_PLL_GROUP7,
590 + RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
591 + RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
592 + core_set(priv, CORE_TRGMII_GSW_CLK_CG,
593 + REG_GSWCK_EN | REG_TRGMIICK_EN);
596 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
597 + mt7530_rmw(priv, MT7530_TRGMII_RD(i),
598 + RD_TAP_MASK, RD_TAP(16));
600 + mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
606 +mt7623_pad_clk_setup(struct dsa_switch *ds)
608 + struct mt7530_priv *priv = ds->priv;
611 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
612 + mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
613 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
615 + mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
616 + mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
622 +mt7530_mib_reset(struct dsa_switch *ds)
624 + struct mt7530_priv *priv = ds->priv;
626 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
627 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
631 +mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
633 + u32 mask = PMCR_TX_EN | PMCR_RX_EN;
636 + mt7530_set(priv, MT7530_PMCR_P(port), mask);
638 + mt7530_clear(priv, MT7530_PMCR_P(port), mask);
642 +mt7530_setup(struct dsa_switch *ds)
644 + struct mt7530_priv *priv = ds->priv;
647 + struct device_node *dn;
649 + /* The parent node of master_netdev which holds the common system
650 + * controller also is the container for two GMACs nodes representing
651 + * as two netdev instances.
653 + dn = ds->master_netdev->dev.of_node->parent;
654 + priv->ethernet = syscon_node_to_regmap(dn);
655 + if (IS_ERR(priv->ethernet))
656 + return PTR_ERR(priv->ethernet);
658 + regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
659 + ret = regulator_enable(priv->core_pwr);
662 + "Failed to enable core power: %d\n", ret);
666 + regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
667 + ret = regulator_enable(priv->io_pwr);
669 + dev_err(priv->dev, "Failed to enable io pwr: %d\n",
674 + /* Reset whole chip through gpio pin or memory-mapped registers for
675 + * different type of hardware
678 + reset_control_assert(priv->rstc);
679 + usleep_range(1000, 1100);
680 + reset_control_deassert(priv->rstc);
682 + gpiod_set_value_cansleep(priv->reset, 0);
683 + usleep_range(1000, 1100);
684 + gpiod_set_value_cansleep(priv->reset, 1);
687 + /* Waiting for MT7530 got to stable */
688 + ret = readx_poll_timeout(_mt7530_read, MT7530_HWTRAP, val, val != 0,
691 + dev_err(priv->dev, "reset timeout\n");
695 + id = mt7530_read(priv, MT7530_CREV);
696 + id >>= CHIP_NAME_SHIFT;
697 + if (id != MT7530_ID) {
698 + dev_err(priv->dev, "chip %x can't be supported\n", id);
702 + /* Reset the switch through internal reset */
703 + mt7530_write(priv, MT7530_SYS_CTRL,
704 + SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
707 + /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
708 + val = mt7530_read(priv, MT7530_MHWTRAP);
709 + val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
710 + val |= MHWTRAP_MANUAL;
711 + mt7530_write(priv, MT7530_MHWTRAP, val);
713 + /* Enable and reset MIB counters */
714 + mt7530_mib_reset(ds);
716 + mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
718 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
719 + /* Disable forwarding by default on all ports */
720 + mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
723 + if (dsa_is_cpu_port(ds, i))
724 + mt7530_cpu_port_enable(priv, i);
726 + mt7530_port_disable(ds, i, NULL);
729 + /* Flush the FDB table */
730 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
737 +static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
739 + struct mt7530_priv *priv = ds->priv;
741 + return mdiobus_read_nested(priv->bus, port, regnum);
744 +int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
746 + struct mt7530_priv *priv = ds->priv;
748 + return mdiobus_write_nested(priv->bus, port, regnum, val);
752 +mt7530_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
756 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
757 + strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
762 +mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
765 + struct mt7530_priv *priv = ds->priv;
766 + const struct mt7530_mib_desc *mib;
770 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
771 + mib = &mt7530_mib[i];
772 + reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
774 + data[i] = mt7530_read(priv, reg);
775 + if (mib->size == 2) {
776 + hi = mt7530_read(priv, reg + 4);
777 + data[i] |= hi << 32;
783 +mt7530_get_sset_count(struct dsa_switch *ds)
785 + return ARRAY_SIZE(mt7530_mib);
788 +static void mt7530_adjust_link(struct dsa_switch *ds, int port,
789 + struct phy_device *phydev)
791 + struct mt7530_priv *priv = ds->priv;
793 + if (phy_is_pseudo_fixed_link(phydev)) {
794 + dev_dbg(priv->dev, "phy-mode for master device = %x\n",
795 + phydev->interface);
797 + /* Setup TX circuit incluing relevant PAD and driving */
798 + mt7530_pad_clk_setup(ds, phydev->interface);
800 + /* Setup RX circuit, relevant PAD and driving on the host
801 + * which must be placed after the setup on the device side is
804 + mt7623_pad_clk_setup(ds);
809 +mt7530_cpu_port_enable(struct mt7530_priv *priv,
812 + /* Enable Mediatek header mode on the cpu port */
813 + mt7530_write(priv, MT7530_PVC_P(port),
816 + /* Setup the MAC by default for the cpu port */
817 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
819 + /* Disable auto learning on the cpu port */
820 + mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
822 + /* Unknown unicast frame fordwarding to the cpu port */
823 + mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
825 + /* CPU port gets connected to all user ports of
828 + mt7530_write(priv, MT7530_PCR_P(port),
829 + PCR_MATRIX(priv->ds->enabled_port_mask));
835 +mt7530_port_enable(struct dsa_switch *ds, int port,
836 + struct phy_device *phy)
838 + struct mt7530_priv *priv = ds->priv;
840 + mutex_lock(&priv->reg_mutex);
842 + /* Setup the MAC for the user port */
843 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
845 + /* Allow the user port gets connected to the cpu port and also
846 + * restore the port matrix if the port is the member of a certain
849 + priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
850 + priv->ports[port].enable = true;
851 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
852 + priv->ports[port].pm);
853 + mt7530_port_set_status(priv, port, 1);
855 + mutex_unlock(&priv->reg_mutex);
861 +mt7530_port_disable(struct dsa_switch *ds, int port,
862 + struct phy_device *phy)
864 + struct mt7530_priv *priv = ds->priv;
866 + mutex_lock(&priv->reg_mutex);
868 + /* Clear up all port matrix which could be restored in the next
869 + * enablement for the port.
871 + priv->ports[port].enable = false;
872 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
874 + mt7530_port_set_status(priv, port, 0);
876 + mutex_unlock(&priv->reg_mutex);
880 +mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
882 + struct mt7530_priv *priv = ds->priv;
886 + case BR_STATE_DISABLED:
887 + stp_state = MT7530_STP_DISABLED;
889 + case BR_STATE_BLOCKING:
890 + stp_state = MT7530_STP_BLOCKING;
892 + case BR_STATE_LISTENING:
893 + stp_state = MT7530_STP_LISTENING;
895 + case BR_STATE_LEARNING:
896 + stp_state = MT7530_STP_LEARNING;
898 + case BR_STATE_FORWARDING:
900 + stp_state = MT7530_STP_FORWARDING;
904 + mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
908 +mt7530_port_bridge_join(struct dsa_switch *ds, int port,
909 + struct net_device *bridge)
911 + struct mt7530_priv *priv = ds->priv;
912 + u32 port_bitmap = BIT(MT7530_CPU_PORT);
915 + mutex_lock(&priv->reg_mutex);
917 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
918 + /* Add this port to the port matrix of the other ports in the
919 + * same bridge. If the port is disabled, port matrix is kept
920 + * and not being setup until the port becomes enabled.
922 + if (ds->enabled_port_mask & BIT(i) && i != port) {
923 + if (ds->ports[i].bridge_dev != bridge)
925 + if (priv->ports[i].enable)
926 + mt7530_set(priv, MT7530_PCR_P(i),
927 + PCR_MATRIX(BIT(port)));
928 + priv->ports[i].pm |= PCR_MATRIX(BIT(port));
930 + port_bitmap |= BIT(i);
934 + /* Add the all other ports to this port matrix. */
935 + if (priv->ports[port].enable)
936 + mt7530_rmw(priv, MT7530_PCR_P(port),
937 + PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
938 + priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
940 + mutex_unlock(&priv->reg_mutex);
946 +mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
947 + struct net_device *bridge)
949 + struct mt7530_priv *priv = ds->priv;
952 + mutex_lock(&priv->reg_mutex);
954 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
955 + /* Remove this port from the port matrix of the other ports
956 + * in the same bridge. If the port is disabled, port matrix
957 + * is kept and not being setup until the port becomes enabled.
959 + if (ds->enabled_port_mask & BIT(i) && i != port) {
960 + if (ds->ports[i].bridge_dev != bridge)
962 + if (priv->ports[i].enable)
963 + mt7530_clear(priv, MT7530_PCR_P(i),
964 + PCR_MATRIX(BIT(port)));
965 + priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
969 + /* Set the cpu port to be the only one in the port matrix of
972 + if (priv->ports[port].enable)
973 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
974 + PCR_MATRIX(BIT(MT7530_CPU_PORT)));
975 + priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
977 + mutex_unlock(&priv->reg_mutex);
981 +mt7530_port_fdb_prepare(struct dsa_switch *ds, int port,
982 + const struct switchdev_obj_port_fdb *fdb,
983 + struct switchdev_trans *trans)
985 + struct mt7530_priv *priv = ds->priv;
988 + /* Because auto-learned entrie shares the same FDB table.
989 + * an entry is reserved with no port_mask to make sure fdb_add
990 + * is called while the entry is still available.
992 + mutex_lock(&priv->reg_mutex);
993 + mt7530_fdb_write(priv, fdb->vid, 0, fdb->addr, -1, STATIC_ENT);
994 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
995 + mutex_unlock(&priv->reg_mutex);
1001 +mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1002 + const struct switchdev_obj_port_fdb *fdb,
1003 + struct switchdev_trans *trans)
1005 + 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_ENT);
1010 + mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1011 + mutex_unlock(&priv->reg_mutex);
1015 +mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1016 + const struct switchdev_obj_port_fdb *fdb)
1018 + struct mt7530_priv *priv = ds->priv;
1020 + u8 port_mask = BIT(port);
1022 + mutex_lock(&priv->reg_mutex);
1023 + mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_EMP);
1024 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1025 + mutex_unlock(&priv->reg_mutex);
1031 +mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1032 + struct switchdev_obj_port_fdb *fdb,
1033 + int (*cb)(struct switchdev_obj *obj))
1035 + struct mt7530_priv *priv = ds->priv;
1036 + struct mt7530_fdb _fdb = { 0 };
1037 + int cnt = MT7530_NUM_FDB_RECORDS;
1041 + mutex_lock(&priv->reg_mutex);
1043 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1048 + if (rsp & ATC_SRCH_HIT) {
1049 + mt7530_fdb_read(priv, &_fdb);
1050 + if (_fdb.port_mask & BIT(port)) {
1051 + ether_addr_copy(fdb->addr, _fdb.mac);
1052 + fdb->vid = _fdb.vid;
1053 + fdb->ndm_state = _fdb.noarp ?
1054 + NUD_NOARP : NUD_REACHABLE;
1055 + ret = cb(&fdb->obj);
1061 + !(rsp & ATC_SRCH_END) &&
1062 + !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1064 + mutex_unlock(&priv->reg_mutex);
1069 +static enum dsa_tag_protocol
1070 +mtk_get_tag_protocol(struct dsa_switch *ds)
1072 + struct mt7530_priv *priv = ds->priv;
1074 + if (!dsa_is_cpu_port(ds, MT7530_CPU_PORT)) {
1075 + dev_warn(priv->dev,
1076 + "port not matched with tagging CPU port\n");
1077 + return DSA_TAG_PROTO_NONE;
1079 + return DSA_TAG_PROTO_MTK;
1083 +static struct dsa_switch_ops mt7530_switch_ops = {
1084 + .get_tag_protocol = mtk_get_tag_protocol,
1085 + .setup = mt7530_setup,
1086 + .get_strings = mt7530_get_strings,
1087 + .phy_read = mt7530_phy_read,
1088 + .phy_write = mt7530_phy_write,
1089 + .get_ethtool_stats = mt7530_get_ethtool_stats,
1090 + .get_sset_count = mt7530_get_sset_count,
1091 + .adjust_link = mt7530_adjust_link,
1092 + .port_enable = mt7530_port_enable,
1093 + .port_disable = mt7530_port_disable,
1094 + .port_stp_state_set = mt7530_stp_state_set,
1095 + .port_bridge_join = mt7530_port_bridge_join,
1096 + .port_bridge_leave = mt7530_port_bridge_leave,
1097 + .port_fdb_prepare = mt7530_port_fdb_prepare,
1098 + .port_fdb_add = mt7530_port_fdb_add,
1099 + .port_fdb_del = mt7530_port_fdb_del,
1100 + .port_fdb_dump = mt7530_port_fdb_dump,
1104 +mt7530_probe(struct mdio_device *mdiodev)
1106 + struct mt7530_priv *priv;
1107 + struct device_node *dn;
1109 + dn = mdiodev->dev.of_node;
1111 + priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1115 + priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
1119 + /* Use medatek,mcm property to distinguish hardware type that would
1120 + * casues a little bit differences on power-on sequence.
1122 + priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1124 + dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1126 + priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1127 + if (IS_ERR(priv->rstc)) {
1128 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1129 + return PTR_ERR(priv->rstc);
1133 + priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1134 + if (IS_ERR(priv->core_pwr))
1135 + return PTR_ERR(priv->core_pwr);
1137 + priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1138 + if (IS_ERR(priv->io_pwr))
1139 + return PTR_ERR(priv->io_pwr);
1141 + /* Not MCM that indicates switch works as the remote standalone
1142 + * integrated circuit so the GPIO pin would be used to complete
1143 + * the reset, otherwise memory-mapped register accessing used
1144 + * through syscon provides in the case of MCM.
1147 + priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1149 + if (IS_ERR(priv->reset)) {
1150 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1151 + return PTR_ERR(priv->reset);
1155 + priv->bus = mdiodev->bus;
1156 + priv->dev = &mdiodev->dev;
1157 + priv->ds->priv = priv;
1158 + priv->ds->ops = &mt7530_switch_ops;
1159 + mutex_init(&priv->reg_mutex);
1161 + dev_set_drvdata(&mdiodev->dev, priv);
1163 + return dsa_register_switch(priv->ds, &mdiodev->dev);
1167 +mt7530_remove(struct mdio_device *mdiodev)
1169 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1172 + ret = regulator_disable(priv->core_pwr);
1174 + dev_err(priv->dev,
1175 + "Failed to disable core power: %d\n", ret);
1177 + ret = regulator_disable(priv->io_pwr);
1179 + dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1182 + dsa_unregister_switch(priv->ds);
1183 + mutex_destroy(&priv->reg_mutex);
1186 +static const struct of_device_id mt7530_of_match[] = {
1187 + { .compatible = "mediatek,mt7530" },
1188 + { /* sentinel */ },
1191 +static struct mdio_driver mt7530_mdio_driver = {
1192 + .probe = mt7530_probe,
1193 + .remove = mt7530_remove,
1194 + .mdiodrv.driver = {
1196 + .of_match_table = mt7530_of_match,
1200 +mdio_module_driver(mt7530_mdio_driver);
1202 +MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1203 +MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1204 +MODULE_LICENSE("GPL");
1205 +MODULE_ALIAS("platform:mediatek-mt7530");
1207 +++ b/drivers/net/dsa/mt7530.h
1210 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
1212 + * This program is free software; you can redistribute it and/or modify
1213 + * it under the terms of the GNU General Public License version 2 as
1214 + * published by the Free Software Foundation.
1216 + * This program is distributed in the hope that it will be useful,
1217 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1218 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1219 + * GNU General Public License for more details.
1225 +#define MT7530_NUM_PORTS 7
1226 +#define MT7530_CPU_PORT 6
1227 +#define MT7530_NUM_FDB_RECORDS 2048
1229 +#define NUM_TRGMII_CTRL 5
1231 +#define TRGMII_BASE(x) (0x10000 + (x))
1233 +/* Registers to ethsys access */
1234 +#define ETHSYS_CLKCFG0 0x2c
1235 +#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
1237 +#define SYSC_REG_RSTCTRL 0x34
1238 +#define RESET_MCM BIT(2)
1240 +/* Registers to mac forward control for unknown frames */
1241 +#define MT7530_MFC 0x10
1242 +#define BC_FFP(x) (((x) & 0xff) << 24)
1243 +#define UNM_FFP(x) (((x) & 0xff) << 16)
1244 +#define UNU_FFP(x) (((x) & 0xff) << 8)
1245 +#define UNU_FFP_MASK UNU_FFP(~0)
1247 +/* Registers for address table access */
1248 +#define MT7530_ATA1 0x74
1249 +#define STATIC_EMP 0
1250 +#define STATIC_ENT 3
1251 +#define MT7530_ATA2 0x78
1253 +/* Register for address table write data */
1254 +#define MT7530_ATWD 0x7c
1256 +/* Register for address table control */
1257 +#define MT7530_ATC 0x80
1258 +#define ATC_HASH (((x) & 0xfff) << 16)
1259 +#define ATC_BUSY BIT(15)
1260 +#define ATC_SRCH_END BIT(14)
1261 +#define ATC_SRCH_HIT BIT(13)
1262 +#define ATC_INVALID BIT(12)
1263 +#define ATC_MAT(x) (((x) & 0xf) << 8)
1264 +#define ATC_MAT_MACTAB ATC_MAT(0)
1266 +enum mt7530_fdb_cmd {
1267 + MT7530_FDB_READ = 0,
1268 + MT7530_FDB_WRITE = 1,
1269 + MT7530_FDB_FLUSH = 2,
1270 + MT7530_FDB_START = 4,
1271 + MT7530_FDB_NEXT = 5,
1274 +/* Registers for table search read address */
1275 +#define MT7530_TSRA1 0x84
1276 +#define MAC_BYTE_0 24
1277 +#define MAC_BYTE_1 16
1278 +#define MAC_BYTE_2 8
1279 +#define MAC_BYTE_3 0
1280 +#define MAC_BYTE_MASK 0xff
1282 +#define MT7530_TSRA2 0x88
1283 +#define MAC_BYTE_4 24
1284 +#define MAC_BYTE_5 16
1286 +#define CVID_MASK 0xfff
1288 +#define MT7530_ATRD 0x8C
1289 +#define AGE_TIMER 24
1290 +#define AGE_TIMER_MASK 0xff
1292 +#define PORT_MAP_MASK 0xff
1293 +#define ENT_STATUS 2
1294 +#define ENT_STATUS_MASK 0x3
1296 +/* Register for vlan table control */
1297 +#define MT7530_VTCR 0x90
1298 +#define VTCR_BUSY BIT(31)
1299 +#define VTCR_FUNC (((x) & 0xf) << 12)
1300 +#define VTCR_FUNC_RD_VID 0x1
1301 +#define VTCR_FUNC_WR_VID 0x2
1302 +#define VTCR_FUNC_INV_VID 0x3
1303 +#define VTCR_FUNC_VAL_VID 0x4
1304 +#define VTCR_VID ((x) & 0xfff)
1306 +/* Register for setup vlan and acl write data */
1307 +#define MT7530_VAWD1 0x94
1308 +#define PORT_STAG BIT(31)
1309 +#define IVL_MAC BIT(30)
1310 +#define PORT_MEM(x) (((x) & 0xff) << 16)
1311 +#define VALID BIT(1)
1313 +#define MT7530_VAWD2 0x98
1315 +/* Register for port STP state control */
1316 +#define MT7530_SSP_P(x) (0x2000 + ((x) * 0x100))
1317 +#define FID_PST(x) ((x) & 0x3)
1318 +#define FID_PST_MASK FID_PST(0x3)
1320 +enum mt7530_stp_state {
1321 + MT7530_STP_DISABLED = 0,
1322 + MT7530_STP_BLOCKING = 1,
1323 + MT7530_STP_LISTENING = 1,
1324 + MT7530_STP_LEARNING = 2,
1325 + MT7530_STP_FORWARDING = 3
1328 +/* Register for port control */
1329 +#define MT7530_PCR_P(x) (0x2004 + ((x) * 0x100))
1330 +#define PORT_VLAN(x) ((x) & 0x3)
1331 +#define PCR_MATRIX(x) (((x) & 0xff) << 16)
1332 +#define PORT_PRI(x) (((x) & 0x7) << 24)
1333 +#define EG_TAG(x) (((x) & 0x3) << 28)
1334 +#define PCR_MATRIX_MASK PCR_MATRIX(0xff)
1335 +#define PCR_MATRIX_CLR PCR_MATRIX(0)
1337 +/* Register for port security control */
1338 +#define MT7530_PSC_P(x) (0x200c + ((x) * 0x100))
1339 +#define SA_DIS BIT(4)
1341 +/* Register for port vlan control */
1342 +#define MT7530_PVC_P(x) (0x2010 + ((x) * 0x100))
1343 +#define PORT_SPEC_TAG BIT(5)
1344 +#define VLAN_ATTR(x) (((x) & 0x3) << 6)
1345 +#define STAG_VPID (((x) & 0xffff) << 16)
1347 +/* Register for port port-and-protocol based vlan 1 control */
1348 +#define MT7530_PPBV1_P(x) (0x2014 + ((x) * 0x100))
1350 +/* Register for port MAC control register */
1351 +#define MT7530_PMCR_P(x) (0x3000 + ((x) * 0x100))
1352 +#define PMCR_IFG_XMIT(x) (((x) & 0x3) << 18)
1353 +#define PMCR_MAC_MODE BIT(16)
1354 +#define PMCR_FORCE_MODE BIT(15)
1355 +#define PMCR_TX_EN BIT(14)
1356 +#define PMCR_RX_EN BIT(13)
1357 +#define PMCR_BACKOFF_EN BIT(9)
1358 +#define PMCR_BACKPR_EN BIT(8)
1359 +#define PMCR_TX_FC_EN BIT(5)
1360 +#define PMCR_RX_FC_EN BIT(4)
1361 +#define PMCR_FORCE_SPEED_1000 BIT(3)
1362 +#define PMCR_FORCE_FDX BIT(1)
1363 +#define PMCR_FORCE_LNK BIT(0)
1364 +#define PMCR_COMMON_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1365 + PMCR_BACKOFF_EN | PMCR_BACKPR_EN | \
1366 + PMCR_TX_EN | PMCR_RX_EN | \
1367 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1368 +#define PMCR_CPUP_LINK (PMCR_COMMON_LINK | PMCR_FORCE_MODE | \
1369 + PMCR_FORCE_SPEED_1000 | \
1370 + PMCR_FORCE_FDX | \
1372 +#define PMCR_USERP_LINK PMCR_COMMON_LINK
1373 +#define PMCR_FIXED_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1374 + PMCR_FORCE_MODE | PMCR_TX_EN | \
1375 + PMCR_RX_EN | PMCR_BACKPR_EN | \
1376 + PMCR_BACKOFF_EN | \
1377 + PMCR_FORCE_SPEED_1000 | \
1378 + PMCR_FORCE_FDX | \
1380 +#define PMCR_FIXED_LINK_FC (PMCR_FIXED_LINK | \
1381 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1383 +#define MT7530_PMSR_P(x) (0x3008 + (x) * 0x100)
1385 +/* Register for MIB */
1386 +#define MT7530_PORT_MIB_COUNTER(x) (0x4000 + (x) * 0x100)
1387 +#define MT7530_MIB_CCR 0x4fe0
1388 +#define CCR_MIB_ENABLE BIT(31)
1389 +#define CCR_RX_OCT_CNT_GOOD BIT(7)
1390 +#define CCR_RX_OCT_CNT_BAD BIT(6)
1391 +#define CCR_TX_OCT_CNT_GOOD BIT(5)
1392 +#define CCR_TX_OCT_CNT_BAD BIT(4)
1393 +#define CCR_MIB_FLUSH (CCR_RX_OCT_CNT_GOOD | \
1394 + CCR_RX_OCT_CNT_BAD | \
1395 + CCR_TX_OCT_CNT_GOOD | \
1396 + CCR_TX_OCT_CNT_BAD)
1397 +#define CCR_MIB_ACTIVATE (CCR_MIB_ENABLE | \
1398 + CCR_RX_OCT_CNT_GOOD | \
1399 + CCR_RX_OCT_CNT_BAD | \
1400 + CCR_TX_OCT_CNT_GOOD | \
1401 + CCR_TX_OCT_CNT_BAD)
1402 +/* Register for system reset */
1403 +#define MT7530_SYS_CTRL 0x7000
1404 +#define SYS_CTRL_PHY_RST BIT(2)
1405 +#define SYS_CTRL_SW_RST BIT(1)
1406 +#define SYS_CTRL_REG_RST BIT(0)
1408 +/* Register for hw trap status */
1409 +#define MT7530_HWTRAP 0x7800
1411 +/* Register for hw trap modification */
1412 +#define MT7530_MHWTRAP 0x7804
1413 +#define MHWTRAP_MANUAL BIT(16)
1414 +#define MHWTRAP_P5_MAC_SEL BIT(13)
1415 +#define MHWTRAP_P6_DIS BIT(8)
1416 +#define MHWTRAP_P5_RGMII_MODE BIT(7)
1417 +#define MHWTRAP_P5_DIS BIT(6)
1418 +#define MHWTRAP_PHY_ACCESS BIT(5)
1420 +/* Register for TOP signal control */
1421 +#define MT7530_TOP_SIG_CTRL 0x7808
1422 +#define TOP_SIG_CTRL_NORMAL (BIT(17) | BIT(16))
1424 +#define MT7530_IO_DRV_CR 0x7810
1425 +#define P5_IO_CLK_DRV(x) ((x) & 0x3)
1426 +#define P5_IO_DATA_DRV(x) (((x) & 0x3) << 4)
1428 +#define MT7530_P6ECR 0x7830
1429 +#define P6_INTF_MODE_MASK 0x3
1430 +#define P6_INTF_MODE(x) ((x) & 0x3)
1432 +/* Registers for TRGMII on the both side */
1433 +#define MT7530_TRGMII_RCK_CTRL 0x7a00
1434 +#define GSW_TRGMII_RCK_CTRL 0x300
1435 +#define RX_RST BIT(31)
1436 +#define RXC_DQSISEL BIT(30)
1437 +#define DQSI1_TAP_MASK (0x7f << 8)
1438 +#define DQSI0_TAP_MASK 0x7f
1439 +#define DQSI1_TAP(x) (((x) & 0x7f) << 8)
1440 +#define DQSI0_TAP(x) ((x) & 0x7f)
1442 +#define MT7530_TRGMII_RCK_RTT 0x7a04
1443 +#define GSW_TRGMII_RCK_RTT 0x304
1444 +#define DQS1_GATE BIT(31)
1445 +#define DQS0_GATE BIT(30)
1447 +#define MT7530_TRGMII_RD(x) (0x7a10 + (x) * 8)
1448 +#define GSW_TRGMII_RD(x) (0x310 + (x) * 8)
1449 +#define BSLIP_EN BIT(31)
1450 +#define EDGE_CHK BIT(30)
1451 +#define RD_TAP_MASK 0x7f
1452 +#define RD_TAP(x) ((x) & 0x7f)
1454 +#define GSW_TRGMII_TXCTRL 0x340
1455 +#define MT7530_TRGMII_TXCTRL 0x7a40
1456 +#define TRAIN_TXEN BIT(31)
1457 +#define TXC_INV BIT(30)
1458 +#define TX_RST BIT(28)
1460 +#define MT7530_TRGMII_TD_ODT(i) (0x7a54 + 8 * (i))
1461 +#define GSW_TRGMII_TD_ODT(i) (0x354 + 8 * (i))
1462 +#define TD_DM_DRVP(x) ((x) & 0xf)
1463 +#define TD_DM_DRVN(x) (((x) & 0xf) << 4)
1465 +#define GSW_INTF_MODE 0x390
1466 +#define INTF_MODE_TRGMII BIT(1)
1468 +#define MT7530_TRGMII_TCK_CTRL 0x7a78
1469 +#define TCK_TAP(x) (((x) & 0xf) << 8)
1471 +#define MT7530_P5RGMIIRXCR 0x7b00
1472 +#define CSR_RGMII_EDGE_ALIGN BIT(8)
1473 +#define CSR_RGMII_RXC_0DEG_CFG(x) ((x) & 0xf)
1475 +#define MT7530_P5RGMIITXCR 0x7b04
1476 +#define CSR_RGMII_TXC_CFG(x) ((x) & 0x1f)
1478 +#define MT7530_CREV 0x7ffc
1479 +#define CHIP_NAME_SHIFT 16
1480 +#define MT7530_ID 0x7530
1482 +/* Registers for core PLL access through mmd indirect */
1483 +#define CORE_PLL_GROUP2 0x401
1484 +#define RG_SYSPLL_EN_NORMAL BIT(15)
1485 +#define RG_SYSPLL_VODEN BIT(14)
1486 +#define RG_SYSPLL_LF BIT(13)
1487 +#define RG_SYSPLL_RST_DLY(x) (((x) & 0x3) << 12)
1488 +#define RG_SYSPLL_LVROD_EN BIT(10)
1489 +#define RG_SYSPLL_PREDIV(x) (((x) & 0x3) << 8)
1490 +#define RG_SYSPLL_POSDIV(x) (((x) & 0x3) << 5)
1491 +#define RG_SYSPLL_FBKSEL BIT(4)
1492 +#define RT_SYSPLL_EN_AFE_OLT BIT(0)
1494 +#define CORE_PLL_GROUP4 0x403
1495 +#define RG_SYSPLL_DDSFBK_EN BIT(12)
1496 +#define RG_SYSPLL_BIAS_EN BIT(11)
1497 +#define RG_SYSPLL_BIAS_LPF_EN BIT(10)
1499 +#define CORE_PLL_GROUP5 0x404
1500 +#define RG_LCDDS_PCW_NCPO1(x) ((x) & 0xffff)
1502 +#define CORE_PLL_GROUP6 0x405
1503 +#define RG_LCDDS_PCW_NCPO0(x) ((x) & 0xffff)
1505 +#define CORE_PLL_GROUP7 0x406
1506 +#define RG_LCDDS_PWDB BIT(15)
1507 +#define RG_LCDDS_ISO_EN BIT(13)
1508 +#define RG_LCCDS_C(x) (((x) & 0x7) << 4)
1509 +#define RG_LCDDS_PCW_NCPO_CHG BIT(3)
1511 +#define CORE_PLL_GROUP10 0x409
1512 +#define RG_LCDDS_SSC_DELTA(x) ((x) & 0xfff)
1514 +#define CORE_PLL_GROUP11 0x40a
1515 +#define RG_LCDDS_SSC_DELTA1(x) ((x) & 0xfff)
1517 +#define CORE_GSWPLL_GRP1 0x40d
1518 +#define RG_GSWPLL_PREDIV(x) (((x) & 0x3) << 14)
1519 +#define RG_GSWPLL_POSDIV_200M(x) (((x) & 0x3) << 12)
1520 +#define RG_GSWPLL_EN_PRE BIT(11)
1521 +#define RG_GSWPLL_FBKSEL BIT(10)
1522 +#define RG_GSWPLL_BP BIT(9)
1523 +#define RG_GSWPLL_BR BIT(8)
1524 +#define RG_GSWPLL_FBKDIV_200M(x) ((x) & 0xff)
1526 +#define CORE_GSWPLL_GRP2 0x40e
1527 +#define RG_GSWPLL_POSDIV_500M(x) (((x) & 0x3) << 8)
1528 +#define RG_GSWPLL_FBKDIV_500M(x) ((x) & 0xff)
1530 +#define CORE_TRGMII_GSW_CLK_CG 0x410
1531 +#define REG_GSWCK_EN BIT(0)
1532 +#define REG_TRGMIICK_EN BIT(1)
1534 +#define MIB_DESC(_s, _o, _n) \
1541 +struct mt7530_mib_desc {
1542 + unsigned int size;
1543 + unsigned int offset;
1547 +struct mt7530_fdb {
1555 +struct mt7530_port {
1560 +/* struct mt7530_priv - This is the main data structure for holding the state
1562 + * @dev: The device pointer
1563 + * @ds: The pointer to the dsa core structure
1564 + * @bus: The bus used for the device and built-in PHY
1565 + * @rstc: The pointer to reset control used by MCM
1566 + * @ethernet: The regmap used for access TRGMII-based registers
1567 + * @core_pwr: The power supplied into the core
1568 + * @io_pwr: The power supplied into the I/O
1569 + * @reset: The descriptor for GPIO line tied to its reset pin
1570 + * @mcm: Flag for distinguishing if standalone IC or module
1572 + * @ports: Holding the state among ports
1573 + * @reg_mutex: The lock for protecting among process accessing
1576 +struct mt7530_priv {
1577 + struct device *dev;
1578 + struct dsa_switch *ds;
1579 + struct mii_bus *bus;
1580 + struct reset_control *rstc;
1581 + struct regmap *ethernet;
1582 + struct regulator *core_pwr;
1583 + struct regulator *io_pwr;
1584 + struct gpio_desc *reset;
1587 + struct mt7530_port ports[MT7530_NUM_PORTS];
1588 + /* protect among processes for registers access*/
1589 + struct mutex reg_mutex;
1592 +struct mt7530_hw_stats {
1593 + const char *string;
1598 +#endif /* __MT7530_H */