xref: /openbmc/linux/drivers/net/dsa/mt7530.c (revision 9257bd80b917cc7908abd27ed5a5211964563f62)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Mediatek MT7530 DSA Switch driver
4  * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
5  */
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_mdio.h>
14 #include <linux/of_net.h>
15 #include <linux/of_platform.h>
16 #include <linux/phylink.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/reset.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/gpio/driver.h>
22 #include <net/dsa.h>
23 
24 #include "mt7530.h"
25 
26 /* String, offset, and register size in bytes if different from 4 bytes */
27 static const struct mt7530_mib_desc mt7530_mib[] = {
28 	MIB_DESC(1, 0x00, "TxDrop"),
29 	MIB_DESC(1, 0x04, "TxCrcErr"),
30 	MIB_DESC(1, 0x08, "TxUnicast"),
31 	MIB_DESC(1, 0x0c, "TxMulticast"),
32 	MIB_DESC(1, 0x10, "TxBroadcast"),
33 	MIB_DESC(1, 0x14, "TxCollision"),
34 	MIB_DESC(1, 0x18, "TxSingleCollision"),
35 	MIB_DESC(1, 0x1c, "TxMultipleCollision"),
36 	MIB_DESC(1, 0x20, "TxDeferred"),
37 	MIB_DESC(1, 0x24, "TxLateCollision"),
38 	MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
39 	MIB_DESC(1, 0x2c, "TxPause"),
40 	MIB_DESC(1, 0x30, "TxPktSz64"),
41 	MIB_DESC(1, 0x34, "TxPktSz65To127"),
42 	MIB_DESC(1, 0x38, "TxPktSz128To255"),
43 	MIB_DESC(1, 0x3c, "TxPktSz256To511"),
44 	MIB_DESC(1, 0x40, "TxPktSz512To1023"),
45 	MIB_DESC(1, 0x44, "Tx1024ToMax"),
46 	MIB_DESC(2, 0x48, "TxBytes"),
47 	MIB_DESC(1, 0x60, "RxDrop"),
48 	MIB_DESC(1, 0x64, "RxFiltering"),
49 	MIB_DESC(1, 0x6c, "RxMulticast"),
50 	MIB_DESC(1, 0x70, "RxBroadcast"),
51 	MIB_DESC(1, 0x74, "RxAlignErr"),
52 	MIB_DESC(1, 0x78, "RxCrcErr"),
53 	MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
54 	MIB_DESC(1, 0x80, "RxFragErr"),
55 	MIB_DESC(1, 0x84, "RxOverSzErr"),
56 	MIB_DESC(1, 0x88, "RxJabberErr"),
57 	MIB_DESC(1, 0x8c, "RxPause"),
58 	MIB_DESC(1, 0x90, "RxPktSz64"),
59 	MIB_DESC(1, 0x94, "RxPktSz65To127"),
60 	MIB_DESC(1, 0x98, "RxPktSz128To255"),
61 	MIB_DESC(1, 0x9c, "RxPktSz256To511"),
62 	MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
63 	MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
64 	MIB_DESC(2, 0xa8, "RxBytes"),
65 	MIB_DESC(1, 0xb0, "RxCtrlDrop"),
66 	MIB_DESC(1, 0xb4, "RxIngressDrop"),
67 	MIB_DESC(1, 0xb8, "RxArlDrop"),
68 };
69 
70 /* Since phy_device has not yet been created and
71  * phy_{read,write}_mmd_indirect is not available, we provide our own
72  * core_{read,write}_mmd_indirect with core_{clear,write,set} wrappers
73  * to complete this function.
74  */
75 static int
76 core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
77 {
78 	struct mii_bus *bus = priv->bus;
79 	int value, ret;
80 
81 	/* Write the desired MMD Devad */
82 	ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
83 	if (ret < 0)
84 		goto err;
85 
86 	/* Write the desired MMD register address */
87 	ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
88 	if (ret < 0)
89 		goto err;
90 
91 	/* Select the Function : DATA with no post increment */
92 	ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
93 	if (ret < 0)
94 		goto err;
95 
96 	/* Read the content of the MMD's selected register */
97 	value = bus->read(bus, 0, MII_MMD_DATA);
98 
99 	return value;
100 err:
101 	dev_err(&bus->dev,  "failed to read mmd register\n");
102 
103 	return ret;
104 }
105 
106 static int
107 core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
108 			int devad, u32 data)
109 {
110 	struct mii_bus *bus = priv->bus;
111 	int ret;
112 
113 	/* Write the desired MMD Devad */
114 	ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
115 	if (ret < 0)
116 		goto err;
117 
118 	/* Write the desired MMD register address */
119 	ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
120 	if (ret < 0)
121 		goto err;
122 
123 	/* Select the Function : DATA with no post increment */
124 	ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
125 	if (ret < 0)
126 		goto err;
127 
128 	/* Write the data into MMD's selected register */
129 	ret = bus->write(bus, 0, MII_MMD_DATA, data);
130 err:
131 	if (ret < 0)
132 		dev_err(&bus->dev,
133 			"failed to write mmd register\n");
134 	return ret;
135 }
136 
137 static void
138 core_write(struct mt7530_priv *priv, u32 reg, u32 val)
139 {
140 	struct mii_bus *bus = priv->bus;
141 
142 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
143 
144 	core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
145 
146 	mutex_unlock(&bus->mdio_lock);
147 }
148 
149 static void
150 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
151 {
152 	struct mii_bus *bus = priv->bus;
153 	u32 val;
154 
155 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
156 
157 	val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
158 	val &= ~mask;
159 	val |= set;
160 	core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
161 
162 	mutex_unlock(&bus->mdio_lock);
163 }
164 
165 static void
166 core_set(struct mt7530_priv *priv, u32 reg, u32 val)
167 {
168 	core_rmw(priv, reg, 0, val);
169 }
170 
171 static void
172 core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
173 {
174 	core_rmw(priv, reg, val, 0);
175 }
176 
177 static int
178 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
179 {
180 	struct mii_bus *bus = priv->bus;
181 	u16 page, r, lo, hi;
182 	int ret;
183 
184 	page = (reg >> 6) & 0x3ff;
185 	r  = (reg >> 2) & 0xf;
186 	lo = val & 0xffff;
187 	hi = val >> 16;
188 
189 	/* MT7530 uses 31 as the pseudo port */
190 	ret = bus->write(bus, 0x1f, 0x1f, page);
191 	if (ret < 0)
192 		goto err;
193 
194 	ret = bus->write(bus, 0x1f, r,  lo);
195 	if (ret < 0)
196 		goto err;
197 
198 	ret = bus->write(bus, 0x1f, 0x10, hi);
199 err:
200 	if (ret < 0)
201 		dev_err(&bus->dev,
202 			"failed to write mt7530 register\n");
203 	return ret;
204 }
205 
206 static u32
207 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
208 {
209 	struct mii_bus *bus = priv->bus;
210 	u16 page, r, lo, hi;
211 	int ret;
212 
213 	page = (reg >> 6) & 0x3ff;
214 	r = (reg >> 2) & 0xf;
215 
216 	/* MT7530 uses 31 as the pseudo port */
217 	ret = bus->write(bus, 0x1f, 0x1f, page);
218 	if (ret < 0) {
219 		dev_err(&bus->dev,
220 			"failed to read mt7530 register\n");
221 		return ret;
222 	}
223 
224 	lo = bus->read(bus, 0x1f, r);
225 	hi = bus->read(bus, 0x1f, 0x10);
226 
227 	return (hi << 16) | (lo & 0xffff);
228 }
229 
230 static void
231 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
232 {
233 	struct mii_bus *bus = priv->bus;
234 
235 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
236 
237 	mt7530_mii_write(priv, reg, val);
238 
239 	mutex_unlock(&bus->mdio_lock);
240 }
241 
242 static u32
243 _mt7530_unlocked_read(struct mt7530_dummy_poll *p)
244 {
245 	return mt7530_mii_read(p->priv, p->reg);
246 }
247 
248 static u32
249 _mt7530_read(struct mt7530_dummy_poll *p)
250 {
251 	struct mii_bus		*bus = p->priv->bus;
252 	u32 val;
253 
254 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
255 
256 	val = mt7530_mii_read(p->priv, p->reg);
257 
258 	mutex_unlock(&bus->mdio_lock);
259 
260 	return val;
261 }
262 
263 static u32
264 mt7530_read(struct mt7530_priv *priv, u32 reg)
265 {
266 	struct mt7530_dummy_poll p;
267 
268 	INIT_MT7530_DUMMY_POLL(&p, priv, reg);
269 	return _mt7530_read(&p);
270 }
271 
272 static void
273 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
274 	   u32 mask, u32 set)
275 {
276 	struct mii_bus *bus = priv->bus;
277 	u32 val;
278 
279 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
280 
281 	val = mt7530_mii_read(priv, reg);
282 	val &= ~mask;
283 	val |= set;
284 	mt7530_mii_write(priv, reg, val);
285 
286 	mutex_unlock(&bus->mdio_lock);
287 }
288 
289 static void
290 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
291 {
292 	mt7530_rmw(priv, reg, 0, val);
293 }
294 
295 static void
296 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
297 {
298 	mt7530_rmw(priv, reg, val, 0);
299 }
300 
301 static int
302 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
303 {
304 	u32 val;
305 	int ret;
306 	struct mt7530_dummy_poll p;
307 
308 	/* Set the command operating upon the MAC address entries */
309 	val = ATC_BUSY | ATC_MAT(0) | cmd;
310 	mt7530_write(priv, MT7530_ATC, val);
311 
312 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
313 	ret = readx_poll_timeout(_mt7530_read, &p, val,
314 				 !(val & ATC_BUSY), 20, 20000);
315 	if (ret < 0) {
316 		dev_err(priv->dev, "reset timeout\n");
317 		return ret;
318 	}
319 
320 	/* Additional sanity for read command if the specified
321 	 * entry is invalid
322 	 */
323 	val = mt7530_read(priv, MT7530_ATC);
324 	if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
325 		return -EINVAL;
326 
327 	if (rsp)
328 		*rsp = val;
329 
330 	return 0;
331 }
332 
333 static void
334 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
335 {
336 	u32 reg[3];
337 	int i;
338 
339 	/* Read from ARL table into an array */
340 	for (i = 0; i < 3; i++) {
341 		reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
342 
343 		dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
344 			__func__, __LINE__, i, reg[i]);
345 	}
346 
347 	fdb->vid = (reg[1] >> CVID) & CVID_MASK;
348 	fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
349 	fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
350 	fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
351 	fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
352 	fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
353 	fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
354 	fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
355 	fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
356 	fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
357 }
358 
359 static void
360 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
361 		 u8 port_mask, const u8 *mac,
362 		 u8 aging, u8 type)
363 {
364 	u32 reg[3] = { 0 };
365 	int i;
366 
367 	reg[1] |= vid & CVID_MASK;
368 	reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
369 	reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
370 	/* STATIC_ENT indicate that entry is static wouldn't
371 	 * be aged out and STATIC_EMP specified as erasing an
372 	 * entry
373 	 */
374 	reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
375 	reg[1] |= mac[5] << MAC_BYTE_5;
376 	reg[1] |= mac[4] << MAC_BYTE_4;
377 	reg[0] |= mac[3] << MAC_BYTE_3;
378 	reg[0] |= mac[2] << MAC_BYTE_2;
379 	reg[0] |= mac[1] << MAC_BYTE_1;
380 	reg[0] |= mac[0] << MAC_BYTE_0;
381 
382 	/* Write array into the ARL table */
383 	for (i = 0; i < 3; i++)
384 		mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
385 }
386 
387 /* Setup TX circuit including relevant PAD and driving */
388 static int
389 mt7530_pad_clk_setup(struct dsa_switch *ds, phy_interface_t interface)
390 {
391 	struct mt7530_priv *priv = ds->priv;
392 	u32 ncpo1, ssc_delta, trgint, i, xtal;
393 
394 	xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK;
395 
396 	if (xtal == HWTRAP_XTAL_20MHZ) {
397 		dev_err(priv->dev,
398 			"%s: MT7530 with a 20MHz XTAL is not supported!\n",
399 			__func__);
400 		return -EINVAL;
401 	}
402 
403 	switch (interface) {
404 	case PHY_INTERFACE_MODE_RGMII:
405 		trgint = 0;
406 		/* PLL frequency: 125MHz */
407 		ncpo1 = 0x0c80;
408 		break;
409 	case PHY_INTERFACE_MODE_TRGMII:
410 		trgint = 1;
411 		if (priv->id == ID_MT7621) {
412 			/* PLL frequency: 150MHz: 1.2GBit */
413 			if (xtal == HWTRAP_XTAL_40MHZ)
414 				ncpo1 = 0x0780;
415 			if (xtal == HWTRAP_XTAL_25MHZ)
416 				ncpo1 = 0x0a00;
417 		} else { /* PLL frequency: 250MHz: 2.0Gbit */
418 			if (xtal == HWTRAP_XTAL_40MHZ)
419 				ncpo1 = 0x0c80;
420 			if (xtal == HWTRAP_XTAL_25MHZ)
421 				ncpo1 = 0x1400;
422 		}
423 		break;
424 	default:
425 		dev_err(priv->dev, "xMII interface %d not supported\n",
426 			interface);
427 		return -EINVAL;
428 	}
429 
430 	if (xtal == HWTRAP_XTAL_25MHZ)
431 		ssc_delta = 0x57;
432 	else
433 		ssc_delta = 0x87;
434 
435 	mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
436 		   P6_INTF_MODE(trgint));
437 
438 	/* Lower Tx Driving for TRGMII path */
439 	for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
440 		mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
441 			     TD_DM_DRVP(8) | TD_DM_DRVN(8));
442 
443 	/* Disable MT7530 core and TRGMII Tx clocks */
444 	core_clear(priv, CORE_TRGMII_GSW_CLK_CG,
445 		   REG_GSWCK_EN | REG_TRGMIICK_EN);
446 
447 	/* Setup core clock for MT7530 */
448 	/* Disable PLL */
449 	core_write(priv, CORE_GSWPLL_GRP1, 0);
450 
451 	/* Set core clock into 500Mhz */
452 	core_write(priv, CORE_GSWPLL_GRP2,
453 		   RG_GSWPLL_POSDIV_500M(1) |
454 		   RG_GSWPLL_FBKDIV_500M(25));
455 
456 	/* Enable PLL */
457 	core_write(priv, CORE_GSWPLL_GRP1,
458 		   RG_GSWPLL_EN_PRE |
459 		   RG_GSWPLL_POSDIV_200M(2) |
460 		   RG_GSWPLL_FBKDIV_200M(32));
461 
462 	/* Setup the MT7530 TRGMII Tx Clock */
463 	core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
464 	core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
465 	core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
466 	core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
467 	core_write(priv, CORE_PLL_GROUP4,
468 		   RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
469 		   RG_SYSPLL_BIAS_LPF_EN);
470 	core_write(priv, CORE_PLL_GROUP2,
471 		   RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
472 		   RG_SYSPLL_POSDIV(1));
473 	core_write(priv, CORE_PLL_GROUP7,
474 		   RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
475 		   RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
476 
477 	/* Enable MT7530 core and TRGMII Tx clocks */
478 	core_set(priv, CORE_TRGMII_GSW_CLK_CG,
479 		 REG_GSWCK_EN | REG_TRGMIICK_EN);
480 
481 	if (!trgint)
482 		for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
483 			mt7530_rmw(priv, MT7530_TRGMII_RD(i),
484 				   RD_TAP_MASK, RD_TAP(16));
485 	return 0;
486 }
487 
488 static bool mt7531_dual_sgmii_supported(struct mt7530_priv *priv)
489 {
490 	u32 val;
491 
492 	val = mt7530_read(priv, MT7531_TOP_SIG_SR);
493 
494 	return (val & PAD_DUAL_SGMII_EN) != 0;
495 }
496 
497 static int
498 mt7531_pad_setup(struct dsa_switch *ds, phy_interface_t interface)
499 {
500 	struct mt7530_priv *priv = ds->priv;
501 	u32 top_sig;
502 	u32 hwstrap;
503 	u32 xtal;
504 	u32 val;
505 
506 	if (mt7531_dual_sgmii_supported(priv))
507 		return 0;
508 
509 	val = mt7530_read(priv, MT7531_CREV);
510 	top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR);
511 	hwstrap = mt7530_read(priv, MT7531_HWTRAP);
512 	if ((val & CHIP_REV_M) > 0)
513 		xtal = (top_sig & PAD_MCM_SMI_EN) ? HWTRAP_XTAL_FSEL_40MHZ :
514 						    HWTRAP_XTAL_FSEL_25MHZ;
515 	else
516 		xtal = hwstrap & HWTRAP_XTAL_FSEL_MASK;
517 
518 	/* Step 1 : Disable MT7531 COREPLL */
519 	val = mt7530_read(priv, MT7531_PLLGP_EN);
520 	val &= ~EN_COREPLL;
521 	mt7530_write(priv, MT7531_PLLGP_EN, val);
522 
523 	/* Step 2: switch to XTAL output */
524 	val = mt7530_read(priv, MT7531_PLLGP_EN);
525 	val |= SW_CLKSW;
526 	mt7530_write(priv, MT7531_PLLGP_EN, val);
527 
528 	val = mt7530_read(priv, MT7531_PLLGP_CR0);
529 	val &= ~RG_COREPLL_EN;
530 	mt7530_write(priv, MT7531_PLLGP_CR0, val);
531 
532 	/* Step 3: disable PLLGP and enable program PLLGP */
533 	val = mt7530_read(priv, MT7531_PLLGP_EN);
534 	val |= SW_PLLGP;
535 	mt7530_write(priv, MT7531_PLLGP_EN, val);
536 
537 	/* Step 4: program COREPLL output frequency to 500MHz */
538 	val = mt7530_read(priv, MT7531_PLLGP_CR0);
539 	val &= ~RG_COREPLL_POSDIV_M;
540 	val |= 2 << RG_COREPLL_POSDIV_S;
541 	mt7530_write(priv, MT7531_PLLGP_CR0, val);
542 	usleep_range(25, 35);
543 
544 	switch (xtal) {
545 	case HWTRAP_XTAL_FSEL_25MHZ:
546 		val = mt7530_read(priv, MT7531_PLLGP_CR0);
547 		val &= ~RG_COREPLL_SDM_PCW_M;
548 		val |= 0x140000 << RG_COREPLL_SDM_PCW_S;
549 		mt7530_write(priv, MT7531_PLLGP_CR0, val);
550 		break;
551 	case HWTRAP_XTAL_FSEL_40MHZ:
552 		val = mt7530_read(priv, MT7531_PLLGP_CR0);
553 		val &= ~RG_COREPLL_SDM_PCW_M;
554 		val |= 0x190000 << RG_COREPLL_SDM_PCW_S;
555 		mt7530_write(priv, MT7531_PLLGP_CR0, val);
556 		break;
557 	}
558 
559 	/* Set feedback divide ratio update signal to high */
560 	val = mt7530_read(priv, MT7531_PLLGP_CR0);
561 	val |= RG_COREPLL_SDM_PCW_CHG;
562 	mt7530_write(priv, MT7531_PLLGP_CR0, val);
563 	/* Wait for at least 16 XTAL clocks */
564 	usleep_range(10, 20);
565 
566 	/* Step 5: set feedback divide ratio update signal to low */
567 	val = mt7530_read(priv, MT7531_PLLGP_CR0);
568 	val &= ~RG_COREPLL_SDM_PCW_CHG;
569 	mt7530_write(priv, MT7531_PLLGP_CR0, val);
570 
571 	/* Enable 325M clock for SGMII */
572 	mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
573 
574 	/* Enable 250SSC clock for RGMII */
575 	mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
576 
577 	/* Step 6: Enable MT7531 PLL */
578 	val = mt7530_read(priv, MT7531_PLLGP_CR0);
579 	val |= RG_COREPLL_EN;
580 	mt7530_write(priv, MT7531_PLLGP_CR0, val);
581 
582 	val = mt7530_read(priv, MT7531_PLLGP_EN);
583 	val |= EN_COREPLL;
584 	mt7530_write(priv, MT7531_PLLGP_EN, val);
585 	usleep_range(25, 35);
586 
587 	return 0;
588 }
589 
590 static void
591 mt7530_mib_reset(struct dsa_switch *ds)
592 {
593 	struct mt7530_priv *priv = ds->priv;
594 
595 	mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
596 	mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
597 }
598 
599 static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
600 {
601 	struct mt7530_priv *priv = ds->priv;
602 
603 	return mdiobus_read_nested(priv->bus, port, regnum);
604 }
605 
606 static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
607 			    u16 val)
608 {
609 	struct mt7530_priv *priv = ds->priv;
610 
611 	return mdiobus_write_nested(priv->bus, port, regnum, val);
612 }
613 
614 static int
615 mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad,
616 			int regnum)
617 {
618 	struct mii_bus *bus = priv->bus;
619 	struct mt7530_dummy_poll p;
620 	u32 reg, val;
621 	int ret;
622 
623 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
624 
625 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
626 
627 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
628 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
629 	if (ret < 0) {
630 		dev_err(priv->dev, "poll timeout\n");
631 		goto out;
632 	}
633 
634 	reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
635 	      MT7531_MDIO_DEV_ADDR(devad) | regnum;
636 	mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
637 
638 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
639 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
640 	if (ret < 0) {
641 		dev_err(priv->dev, "poll timeout\n");
642 		goto out;
643 	}
644 
645 	reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) |
646 	      MT7531_MDIO_DEV_ADDR(devad);
647 	mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
648 
649 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
650 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
651 	if (ret < 0) {
652 		dev_err(priv->dev, "poll timeout\n");
653 		goto out;
654 	}
655 
656 	ret = val & MT7531_MDIO_RW_DATA_MASK;
657 out:
658 	mutex_unlock(&bus->mdio_lock);
659 
660 	return ret;
661 }
662 
663 static int
664 mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad,
665 			 int regnum, u32 data)
666 {
667 	struct mii_bus *bus = priv->bus;
668 	struct mt7530_dummy_poll p;
669 	u32 val, reg;
670 	int ret;
671 
672 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
673 
674 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
675 
676 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
677 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
678 	if (ret < 0) {
679 		dev_err(priv->dev, "poll timeout\n");
680 		goto out;
681 	}
682 
683 	reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
684 	      MT7531_MDIO_DEV_ADDR(devad) | regnum;
685 	mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
686 
687 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
688 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
689 	if (ret < 0) {
690 		dev_err(priv->dev, "poll timeout\n");
691 		goto out;
692 	}
693 
694 	reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) |
695 	      MT7531_MDIO_DEV_ADDR(devad) | data;
696 	mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
697 
698 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
699 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
700 	if (ret < 0) {
701 		dev_err(priv->dev, "poll timeout\n");
702 		goto out;
703 	}
704 
705 out:
706 	mutex_unlock(&bus->mdio_lock);
707 
708 	return ret;
709 }
710 
711 static int
712 mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum)
713 {
714 	struct mii_bus *bus = priv->bus;
715 	struct mt7530_dummy_poll p;
716 	int ret;
717 	u32 val;
718 
719 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
720 
721 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
722 
723 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
724 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
725 	if (ret < 0) {
726 		dev_err(priv->dev, "poll timeout\n");
727 		goto out;
728 	}
729 
730 	val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) |
731 	      MT7531_MDIO_REG_ADDR(regnum);
732 
733 	mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST);
734 
735 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
736 				 !(val & MT7531_PHY_ACS_ST), 20, 100000);
737 	if (ret < 0) {
738 		dev_err(priv->dev, "poll timeout\n");
739 		goto out;
740 	}
741 
742 	ret = val & MT7531_MDIO_RW_DATA_MASK;
743 out:
744 	mutex_unlock(&bus->mdio_lock);
745 
746 	return ret;
747 }
748 
749 static int
750 mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum,
751 			 u16 data)
752 {
753 	struct mii_bus *bus = priv->bus;
754 	struct mt7530_dummy_poll p;
755 	int ret;
756 	u32 reg;
757 
758 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
759 
760 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
761 
762 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
763 				 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
764 	if (ret < 0) {
765 		dev_err(priv->dev, "poll timeout\n");
766 		goto out;
767 	}
768 
769 	reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) |
770 	      MT7531_MDIO_REG_ADDR(regnum) | data;
771 
772 	mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
773 
774 	ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
775 				 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
776 	if (ret < 0) {
777 		dev_err(priv->dev, "poll timeout\n");
778 		goto out;
779 	}
780 
781 out:
782 	mutex_unlock(&bus->mdio_lock);
783 
784 	return ret;
785 }
786 
787 static int
788 mt7531_ind_phy_read(struct dsa_switch *ds, int port, int regnum)
789 {
790 	struct mt7530_priv *priv = ds->priv;
791 	int devad;
792 	int ret;
793 
794 	if (regnum & MII_ADDR_C45) {
795 		devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
796 		ret = mt7531_ind_c45_phy_read(priv, port, devad,
797 					      regnum & MII_REGADDR_C45_MASK);
798 	} else {
799 		ret = mt7531_ind_c22_phy_read(priv, port, regnum);
800 	}
801 
802 	return ret;
803 }
804 
805 static int
806 mt7531_ind_phy_write(struct dsa_switch *ds, int port, int regnum,
807 		     u16 data)
808 {
809 	struct mt7530_priv *priv = ds->priv;
810 	int devad;
811 	int ret;
812 
813 	if (regnum & MII_ADDR_C45) {
814 		devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
815 		ret = mt7531_ind_c45_phy_write(priv, port, devad,
816 					       regnum & MII_REGADDR_C45_MASK,
817 					       data);
818 	} else {
819 		ret = mt7531_ind_c22_phy_write(priv, port, regnum, data);
820 	}
821 
822 	return ret;
823 }
824 
825 static void
826 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
827 		   uint8_t *data)
828 {
829 	int i;
830 
831 	if (stringset != ETH_SS_STATS)
832 		return;
833 
834 	for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
835 		strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
836 			ETH_GSTRING_LEN);
837 }
838 
839 static void
840 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
841 			 uint64_t *data)
842 {
843 	struct mt7530_priv *priv = ds->priv;
844 	const struct mt7530_mib_desc *mib;
845 	u32 reg, i;
846 	u64 hi;
847 
848 	for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
849 		mib = &mt7530_mib[i];
850 		reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
851 
852 		data[i] = mt7530_read(priv, reg);
853 		if (mib->size == 2) {
854 			hi = mt7530_read(priv, reg + 4);
855 			data[i] |= hi << 32;
856 		}
857 	}
858 }
859 
860 static int
861 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
862 {
863 	if (sset != ETH_SS_STATS)
864 		return 0;
865 
866 	return ARRAY_SIZE(mt7530_mib);
867 }
868 
869 static int
870 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
871 {
872 	struct mt7530_priv *priv = ds->priv;
873 	unsigned int secs = msecs / 1000;
874 	unsigned int tmp_age_count;
875 	unsigned int error = -1;
876 	unsigned int age_count;
877 	unsigned int age_unit;
878 
879 	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
880 	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
881 		return -ERANGE;
882 
883 	/* iterate through all possible age_count to find the closest pair */
884 	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
885 		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
886 
887 		if (tmp_age_unit <= AGE_UNIT_MAX) {
888 			unsigned int tmp_error = secs -
889 				(tmp_age_count + 1) * (tmp_age_unit + 1);
890 
891 			/* found a closer pair */
892 			if (error > tmp_error) {
893 				error = tmp_error;
894 				age_count = tmp_age_count;
895 				age_unit = tmp_age_unit;
896 			}
897 
898 			/* found the exact match, so break the loop */
899 			if (!error)
900 				break;
901 		}
902 	}
903 
904 	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
905 
906 	return 0;
907 }
908 
909 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
910 {
911 	struct mt7530_priv *priv = ds->priv;
912 	u8 tx_delay = 0;
913 	int val;
914 
915 	mutex_lock(&priv->reg_mutex);
916 
917 	val = mt7530_read(priv, MT7530_MHWTRAP);
918 
919 	val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
920 	val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
921 
922 	switch (priv->p5_intf_sel) {
923 	case P5_INTF_SEL_PHY_P0:
924 		/* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
925 		val |= MHWTRAP_PHY0_SEL;
926 		fallthrough;
927 	case P5_INTF_SEL_PHY_P4:
928 		/* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
929 		val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
930 
931 		/* Setup the MAC by default for the cpu port */
932 		mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
933 		break;
934 	case P5_INTF_SEL_GMAC5:
935 		/* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
936 		val &= ~MHWTRAP_P5_DIS;
937 		break;
938 	case P5_DISABLED:
939 		interface = PHY_INTERFACE_MODE_NA;
940 		break;
941 	default:
942 		dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
943 			priv->p5_intf_sel);
944 		goto unlock_exit;
945 	}
946 
947 	/* Setup RGMII settings */
948 	if (phy_interface_mode_is_rgmii(interface)) {
949 		val |= MHWTRAP_P5_RGMII_MODE;
950 
951 		/* P5 RGMII RX Clock Control: delay setting for 1000M */
952 		mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
953 
954 		/* Don't set delay in DSA mode */
955 		if (!dsa_is_dsa_port(priv->ds, 5) &&
956 		    (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
957 		     interface == PHY_INTERFACE_MODE_RGMII_ID))
958 			tx_delay = 4; /* n * 0.5 ns */
959 
960 		/* P5 RGMII TX Clock Control: delay x */
961 		mt7530_write(priv, MT7530_P5RGMIITXCR,
962 			     CSR_RGMII_TXC_CFG(0x10 + tx_delay));
963 
964 		/* reduce P5 RGMII Tx driving, 8mA */
965 		mt7530_write(priv, MT7530_IO_DRV_CR,
966 			     P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
967 	}
968 
969 	mt7530_write(priv, MT7530_MHWTRAP, val);
970 
971 	dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
972 		val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
973 
974 	priv->p5_interface = interface;
975 
976 unlock_exit:
977 	mutex_unlock(&priv->reg_mutex);
978 }
979 
980 static int
981 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
982 {
983 	struct mt7530_priv *priv = ds->priv;
984 	int ret;
985 
986 	/* Setup max capability of CPU port at first */
987 	if (priv->info->cpu_port_config) {
988 		ret = priv->info->cpu_port_config(ds, port);
989 		if (ret)
990 			return ret;
991 	}
992 
993 	/* Enable Mediatek header mode on the cpu port */
994 	mt7530_write(priv, MT7530_PVC_P(port),
995 		     PORT_SPEC_TAG);
996 
997 	/* Disable flooding by default */
998 	mt7530_rmw(priv, MT7530_MFC, BC_FFP_MASK | UNM_FFP_MASK | UNU_FFP_MASK,
999 		   BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) | UNU_FFP(BIT(port)));
1000 
1001 	/* Set CPU port number */
1002 	if (priv->id == ID_MT7621)
1003 		mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
1004 
1005 	/* CPU port gets connected to all user ports of
1006 	 * the switch.
1007 	 */
1008 	mt7530_write(priv, MT7530_PCR_P(port),
1009 		     PCR_MATRIX(dsa_user_ports(priv->ds)));
1010 
1011 	return 0;
1012 }
1013 
1014 static int
1015 mt7530_port_enable(struct dsa_switch *ds, int port,
1016 		   struct phy_device *phy)
1017 {
1018 	struct mt7530_priv *priv = ds->priv;
1019 
1020 	if (!dsa_is_user_port(ds, port))
1021 		return 0;
1022 
1023 	mutex_lock(&priv->reg_mutex);
1024 
1025 	/* Allow the user port gets connected to the cpu port and also
1026 	 * restore the port matrix if the port is the member of a certain
1027 	 * bridge.
1028 	 */
1029 	priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
1030 	priv->ports[port].enable = true;
1031 	mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1032 		   priv->ports[port].pm);
1033 	mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1034 
1035 	mutex_unlock(&priv->reg_mutex);
1036 
1037 	return 0;
1038 }
1039 
1040 static void
1041 mt7530_port_disable(struct dsa_switch *ds, int port)
1042 {
1043 	struct mt7530_priv *priv = ds->priv;
1044 
1045 	if (!dsa_is_user_port(ds, port))
1046 		return;
1047 
1048 	mutex_lock(&priv->reg_mutex);
1049 
1050 	/* Clear up all port matrix which could be restored in the next
1051 	 * enablement for the port.
1052 	 */
1053 	priv->ports[port].enable = false;
1054 	mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1055 		   PCR_MATRIX_CLR);
1056 	mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1057 
1058 	mutex_unlock(&priv->reg_mutex);
1059 }
1060 
1061 static int
1062 mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1063 {
1064 	struct mt7530_priv *priv = ds->priv;
1065 	struct mii_bus *bus = priv->bus;
1066 	int length;
1067 	u32 val;
1068 
1069 	/* When a new MTU is set, DSA always set the CPU port's MTU to the
1070 	 * largest MTU of the slave ports. Because the switch only has a global
1071 	 * RX length register, only allowing CPU port here is enough.
1072 	 */
1073 	if (!dsa_is_cpu_port(ds, port))
1074 		return 0;
1075 
1076 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1077 
1078 	val = mt7530_mii_read(priv, MT7530_GMACCR);
1079 	val &= ~MAX_RX_PKT_LEN_MASK;
1080 
1081 	/* RX length also includes Ethernet header, MTK tag, and FCS length */
1082 	length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
1083 	if (length <= 1522) {
1084 		val |= MAX_RX_PKT_LEN_1522;
1085 	} else if (length <= 1536) {
1086 		val |= MAX_RX_PKT_LEN_1536;
1087 	} else if (length <= 1552) {
1088 		val |= MAX_RX_PKT_LEN_1552;
1089 	} else {
1090 		val &= ~MAX_RX_JUMBO_MASK;
1091 		val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
1092 		val |= MAX_RX_PKT_LEN_JUMBO;
1093 	}
1094 
1095 	mt7530_mii_write(priv, MT7530_GMACCR, val);
1096 
1097 	mutex_unlock(&bus->mdio_lock);
1098 
1099 	return 0;
1100 }
1101 
1102 static int
1103 mt7530_port_max_mtu(struct dsa_switch *ds, int port)
1104 {
1105 	return MT7530_MAX_MTU;
1106 }
1107 
1108 static void
1109 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1110 {
1111 	struct mt7530_priv *priv = ds->priv;
1112 	u32 stp_state;
1113 
1114 	switch (state) {
1115 	case BR_STATE_DISABLED:
1116 		stp_state = MT7530_STP_DISABLED;
1117 		break;
1118 	case BR_STATE_BLOCKING:
1119 		stp_state = MT7530_STP_BLOCKING;
1120 		break;
1121 	case BR_STATE_LISTENING:
1122 		stp_state = MT7530_STP_LISTENING;
1123 		break;
1124 	case BR_STATE_LEARNING:
1125 		stp_state = MT7530_STP_LEARNING;
1126 		break;
1127 	case BR_STATE_FORWARDING:
1128 	default:
1129 		stp_state = MT7530_STP_FORWARDING;
1130 		break;
1131 	}
1132 
1133 	mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
1134 }
1135 
1136 static int
1137 mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1138 			     struct switchdev_brport_flags flags,
1139 			     struct netlink_ext_ack *extack)
1140 {
1141 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1142 			   BR_BCAST_FLOOD))
1143 		return -EINVAL;
1144 
1145 	return 0;
1146 }
1147 
1148 static int
1149 mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
1150 			 struct switchdev_brport_flags flags,
1151 			 struct netlink_ext_ack *extack)
1152 {
1153 	struct mt7530_priv *priv = ds->priv;
1154 
1155 	if (flags.mask & BR_LEARNING)
1156 		mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
1157 			   flags.val & BR_LEARNING ? 0 : SA_DIS);
1158 
1159 	if (flags.mask & BR_FLOOD)
1160 		mt7530_rmw(priv, MT7530_MFC, UNU_FFP(BIT(port)),
1161 			   flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
1162 
1163 	if (flags.mask & BR_MCAST_FLOOD)
1164 		mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1165 			   flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
1166 
1167 	if (flags.mask & BR_BCAST_FLOOD)
1168 		mt7530_rmw(priv, MT7530_MFC, BC_FFP(BIT(port)),
1169 			   flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
1170 
1171 	return 0;
1172 }
1173 
1174 static int
1175 mt7530_port_set_mrouter(struct dsa_switch *ds, int port, bool mrouter,
1176 			struct netlink_ext_ack *extack)
1177 {
1178 	struct mt7530_priv *priv = ds->priv;
1179 
1180 	mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1181 		   mrouter ? UNM_FFP(BIT(port)) : 0);
1182 
1183 	return 0;
1184 }
1185 
1186 static int
1187 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1188 			struct net_device *bridge)
1189 {
1190 	struct mt7530_priv *priv = ds->priv;
1191 	u32 port_bitmap = BIT(MT7530_CPU_PORT);
1192 	int i;
1193 
1194 	mutex_lock(&priv->reg_mutex);
1195 
1196 	for (i = 0; i < MT7530_NUM_PORTS; i++) {
1197 		/* Add this port to the port matrix of the other ports in the
1198 		 * same bridge. If the port is disabled, port matrix is kept
1199 		 * and not being setup until the port becomes enabled.
1200 		 */
1201 		if (dsa_is_user_port(ds, i) && i != port) {
1202 			if (dsa_to_port(ds, i)->bridge_dev != bridge)
1203 				continue;
1204 			if (priv->ports[i].enable)
1205 				mt7530_set(priv, MT7530_PCR_P(i),
1206 					   PCR_MATRIX(BIT(port)));
1207 			priv->ports[i].pm |= PCR_MATRIX(BIT(port));
1208 
1209 			port_bitmap |= BIT(i);
1210 		}
1211 	}
1212 
1213 	/* Add the all other ports to this port matrix. */
1214 	if (priv->ports[port].enable)
1215 		mt7530_rmw(priv, MT7530_PCR_P(port),
1216 			   PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1217 	priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1218 
1219 	mutex_unlock(&priv->reg_mutex);
1220 
1221 	return 0;
1222 }
1223 
1224 static void
1225 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1226 {
1227 	struct mt7530_priv *priv = ds->priv;
1228 	bool all_user_ports_removed = true;
1229 	int i;
1230 
1231 	/* When a port is removed from the bridge, the port would be set up
1232 	 * back to the default as is at initial boot which is a VLAN-unaware
1233 	 * port.
1234 	 */
1235 	mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1236 		   MT7530_PORT_MATRIX_MODE);
1237 	mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1238 		   VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1239 		   PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1240 
1241 	for (i = 0; i < MT7530_NUM_PORTS; i++) {
1242 		if (dsa_is_user_port(ds, i) &&
1243 		    dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1244 			all_user_ports_removed = false;
1245 			break;
1246 		}
1247 	}
1248 
1249 	/* CPU port also does the same thing until all user ports belonging to
1250 	 * the CPU port get out of VLAN filtering mode.
1251 	 */
1252 	if (all_user_ports_removed) {
1253 		mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
1254 			     PCR_MATRIX(dsa_user_ports(priv->ds)));
1255 		mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
1256 			     | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1257 	}
1258 }
1259 
1260 static void
1261 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1262 {
1263 	struct mt7530_priv *priv = ds->priv;
1264 
1265 	/* Trapped into security mode allows packet forwarding through VLAN
1266 	 * table lookup. CPU port is set to fallback mode to let untagged
1267 	 * frames pass through.
1268 	 */
1269 	if (dsa_is_cpu_port(ds, port))
1270 		mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1271 			   MT7530_PORT_FALLBACK_MODE);
1272 	else
1273 		mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1274 			   MT7530_PORT_SECURITY_MODE);
1275 
1276 	/* Set the port as a user port which is to be able to recognize VID
1277 	 * from incoming packets before fetching entry within the VLAN table.
1278 	 */
1279 	mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1280 		   VLAN_ATTR(MT7530_VLAN_USER) |
1281 		   PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
1282 }
1283 
1284 static void
1285 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1286 			 struct net_device *bridge)
1287 {
1288 	struct mt7530_priv *priv = ds->priv;
1289 	int i;
1290 
1291 	mutex_lock(&priv->reg_mutex);
1292 
1293 	for (i = 0; i < MT7530_NUM_PORTS; i++) {
1294 		/* Remove this port from the port matrix of the other ports
1295 		 * in the same bridge. If the port is disabled, port matrix
1296 		 * is kept and not being setup until the port becomes enabled.
1297 		 * And the other port's port matrix cannot be broken when the
1298 		 * other port is still a VLAN-aware port.
1299 		 */
1300 		if (dsa_is_user_port(ds, i) && i != port &&
1301 		   !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1302 			if (dsa_to_port(ds, i)->bridge_dev != bridge)
1303 				continue;
1304 			if (priv->ports[i].enable)
1305 				mt7530_clear(priv, MT7530_PCR_P(i),
1306 					     PCR_MATRIX(BIT(port)));
1307 			priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
1308 		}
1309 	}
1310 
1311 	/* Set the cpu port to be the only one in the port matrix of
1312 	 * this port.
1313 	 */
1314 	if (priv->ports[port].enable)
1315 		mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1316 			   PCR_MATRIX(BIT(MT7530_CPU_PORT)));
1317 	priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
1318 
1319 	mutex_unlock(&priv->reg_mutex);
1320 }
1321 
1322 static int
1323 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1324 		    const unsigned char *addr, u16 vid)
1325 {
1326 	struct mt7530_priv *priv = ds->priv;
1327 	int ret;
1328 	u8 port_mask = BIT(port);
1329 
1330 	mutex_lock(&priv->reg_mutex);
1331 	mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1332 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1333 	mutex_unlock(&priv->reg_mutex);
1334 
1335 	return ret;
1336 }
1337 
1338 static int
1339 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1340 		    const unsigned char *addr, u16 vid)
1341 {
1342 	struct mt7530_priv *priv = ds->priv;
1343 	int ret;
1344 	u8 port_mask = BIT(port);
1345 
1346 	mutex_lock(&priv->reg_mutex);
1347 	mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
1348 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1349 	mutex_unlock(&priv->reg_mutex);
1350 
1351 	return ret;
1352 }
1353 
1354 static int
1355 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1356 		     dsa_fdb_dump_cb_t *cb, void *data)
1357 {
1358 	struct mt7530_priv *priv = ds->priv;
1359 	struct mt7530_fdb _fdb = { 0 };
1360 	int cnt = MT7530_NUM_FDB_RECORDS;
1361 	int ret = 0;
1362 	u32 rsp = 0;
1363 
1364 	mutex_lock(&priv->reg_mutex);
1365 
1366 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1367 	if (ret < 0)
1368 		goto err;
1369 
1370 	do {
1371 		if (rsp & ATC_SRCH_HIT) {
1372 			mt7530_fdb_read(priv, &_fdb);
1373 			if (_fdb.port_mask & BIT(port)) {
1374 				ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1375 					 data);
1376 				if (ret < 0)
1377 					break;
1378 			}
1379 		}
1380 	} while (--cnt &&
1381 		 !(rsp & ATC_SRCH_END) &&
1382 		 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1383 err:
1384 	mutex_unlock(&priv->reg_mutex);
1385 
1386 	return 0;
1387 }
1388 
1389 static int
1390 mt7530_port_mdb_add(struct dsa_switch *ds, int port,
1391 		    const struct switchdev_obj_port_mdb *mdb)
1392 {
1393 	struct mt7530_priv *priv = ds->priv;
1394 	const u8 *addr = mdb->addr;
1395 	u16 vid = mdb->vid;
1396 	u8 port_mask = 0;
1397 	int ret;
1398 
1399 	mutex_lock(&priv->reg_mutex);
1400 
1401 	mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1402 	if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1403 		port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1404 			    & PORT_MAP_MASK;
1405 
1406 	port_mask |= BIT(port);
1407 	mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1408 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1409 
1410 	mutex_unlock(&priv->reg_mutex);
1411 
1412 	return ret;
1413 }
1414 
1415 static int
1416 mt7530_port_mdb_del(struct dsa_switch *ds, int port,
1417 		    const struct switchdev_obj_port_mdb *mdb)
1418 {
1419 	struct mt7530_priv *priv = ds->priv;
1420 	const u8 *addr = mdb->addr;
1421 	u16 vid = mdb->vid;
1422 	u8 port_mask = 0;
1423 	int ret;
1424 
1425 	mutex_lock(&priv->reg_mutex);
1426 
1427 	mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1428 	if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1429 		port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1430 			    & PORT_MAP_MASK;
1431 
1432 	port_mask &= ~BIT(port);
1433 	mt7530_fdb_write(priv, vid, port_mask, addr, -1,
1434 			 port_mask ? STATIC_ENT : STATIC_EMP);
1435 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1436 
1437 	mutex_unlock(&priv->reg_mutex);
1438 
1439 	return ret;
1440 }
1441 
1442 static int
1443 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1444 {
1445 	struct mt7530_dummy_poll p;
1446 	u32 val;
1447 	int ret;
1448 
1449 	val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1450 	mt7530_write(priv, MT7530_VTCR, val);
1451 
1452 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1453 	ret = readx_poll_timeout(_mt7530_read, &p, val,
1454 				 !(val & VTCR_BUSY), 20, 20000);
1455 	if (ret < 0) {
1456 		dev_err(priv->dev, "poll timeout\n");
1457 		return ret;
1458 	}
1459 
1460 	val = mt7530_read(priv, MT7530_VTCR);
1461 	if (val & VTCR_INVALID) {
1462 		dev_err(priv->dev, "read VTCR invalid\n");
1463 		return -EINVAL;
1464 	}
1465 
1466 	return 0;
1467 }
1468 
1469 static int
1470 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1471 			   struct netlink_ext_ack *extack)
1472 {
1473 	if (vlan_filtering) {
1474 		/* The port is being kept as VLAN-unaware port when bridge is
1475 		 * set up with vlan_filtering not being set, Otherwise, the
1476 		 * port and the corresponding CPU port is required the setup
1477 		 * for becoming a VLAN-aware port.
1478 		 */
1479 		mt7530_port_set_vlan_aware(ds, port);
1480 		mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
1481 	} else {
1482 		mt7530_port_set_vlan_unaware(ds, port);
1483 	}
1484 
1485 	return 0;
1486 }
1487 
1488 static void
1489 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1490 		   struct mt7530_hw_vlan_entry *entry)
1491 {
1492 	u8 new_members;
1493 	u32 val;
1494 
1495 	new_members = entry->old_members | BIT(entry->port) |
1496 		      BIT(MT7530_CPU_PORT);
1497 
1498 	/* Validate the entry with independent learning, create egress tag per
1499 	 * VLAN and joining the port as one of the port members.
1500 	 */
1501 	val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
1502 	mt7530_write(priv, MT7530_VAWD1, val);
1503 
1504 	/* Decide whether adding tag or not for those outgoing packets from the
1505 	 * port inside the VLAN.
1506 	 */
1507 	val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1508 				MT7530_VLAN_EGRESS_TAG;
1509 	mt7530_rmw(priv, MT7530_VAWD2,
1510 		   ETAG_CTRL_P_MASK(entry->port),
1511 		   ETAG_CTRL_P(entry->port, val));
1512 
1513 	/* CPU port is always taken as a tagged port for serving more than one
1514 	 * VLANs across and also being applied with egress type stack mode for
1515 	 * that VLAN tags would be appended after hardware special tag used as
1516 	 * DSA tag.
1517 	 */
1518 	mt7530_rmw(priv, MT7530_VAWD2,
1519 		   ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1520 		   ETAG_CTRL_P(MT7530_CPU_PORT,
1521 			       MT7530_VLAN_EGRESS_STACK));
1522 }
1523 
1524 static void
1525 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1526 		   struct mt7530_hw_vlan_entry *entry)
1527 {
1528 	u8 new_members;
1529 	u32 val;
1530 
1531 	new_members = entry->old_members & ~BIT(entry->port);
1532 
1533 	val = mt7530_read(priv, MT7530_VAWD1);
1534 	if (!(val & VLAN_VALID)) {
1535 		dev_err(priv->dev,
1536 			"Cannot be deleted due to invalid entry\n");
1537 		return;
1538 	}
1539 
1540 	/* If certain member apart from CPU port is still alive in the VLAN,
1541 	 * the entry would be kept valid. Otherwise, the entry is got to be
1542 	 * disabled.
1543 	 */
1544 	if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1545 		val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1546 		      VLAN_VALID;
1547 		mt7530_write(priv, MT7530_VAWD1, val);
1548 	} else {
1549 		mt7530_write(priv, MT7530_VAWD1, 0);
1550 		mt7530_write(priv, MT7530_VAWD2, 0);
1551 	}
1552 }
1553 
1554 static void
1555 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1556 		      struct mt7530_hw_vlan_entry *entry,
1557 		      mt7530_vlan_op vlan_op)
1558 {
1559 	u32 val;
1560 
1561 	/* Fetch entry */
1562 	mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1563 
1564 	val = mt7530_read(priv, MT7530_VAWD1);
1565 
1566 	entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1567 
1568 	/* Manipulate entry */
1569 	vlan_op(priv, entry);
1570 
1571 	/* Flush result to hardware */
1572 	mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1573 }
1574 
1575 static int
1576 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1577 		     const struct switchdev_obj_port_vlan *vlan,
1578 		     struct netlink_ext_ack *extack)
1579 {
1580 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1581 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1582 	struct mt7530_hw_vlan_entry new_entry;
1583 	struct mt7530_priv *priv = ds->priv;
1584 
1585 	mutex_lock(&priv->reg_mutex);
1586 
1587 	mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1588 	mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
1589 
1590 	if (pvid) {
1591 		mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1592 			   G0_PORT_VID(vlan->vid));
1593 		priv->ports[port].pvid = vlan->vid;
1594 	}
1595 
1596 	mutex_unlock(&priv->reg_mutex);
1597 
1598 	return 0;
1599 }
1600 
1601 static int
1602 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1603 		     const struct switchdev_obj_port_vlan *vlan)
1604 {
1605 	struct mt7530_hw_vlan_entry target_entry;
1606 	struct mt7530_priv *priv = ds->priv;
1607 	u16 pvid;
1608 
1609 	mutex_lock(&priv->reg_mutex);
1610 
1611 	pvid = priv->ports[port].pvid;
1612 	mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1613 	mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
1614 			      mt7530_hw_vlan_del);
1615 
1616 	/* PVID is being restored to the default whenever the PVID port
1617 	 * is being removed from the VLAN.
1618 	 */
1619 	if (pvid == vlan->vid)
1620 		pvid = G0_PORT_VID_DEF;
1621 
1622 	mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1623 	priv->ports[port].pvid = pvid;
1624 
1625 	mutex_unlock(&priv->reg_mutex);
1626 
1627 	return 0;
1628 }
1629 
1630 static int mt753x_mirror_port_get(unsigned int id, u32 val)
1631 {
1632 	return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) :
1633 				   MIRROR_PORT(val);
1634 }
1635 
1636 static int mt753x_mirror_port_set(unsigned int id, u32 val)
1637 {
1638 	return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) :
1639 				   MIRROR_PORT(val);
1640 }
1641 
1642 static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
1643 				  struct dsa_mall_mirror_tc_entry *mirror,
1644 				  bool ingress)
1645 {
1646 	struct mt7530_priv *priv = ds->priv;
1647 	int monitor_port;
1648 	u32 val;
1649 
1650 	/* Check for existent entry */
1651 	if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1652 		return -EEXIST;
1653 
1654 	val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1655 
1656 	/* MT7530 only supports one monitor port */
1657 	monitor_port = mt753x_mirror_port_get(priv->id, val);
1658 	if (val & MT753X_MIRROR_EN(priv->id) &&
1659 	    monitor_port != mirror->to_local_port)
1660 		return -EEXIST;
1661 
1662 	val |= MT753X_MIRROR_EN(priv->id);
1663 	val &= ~MT753X_MIRROR_MASK(priv->id);
1664 	val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port);
1665 	mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1666 
1667 	val = mt7530_read(priv, MT7530_PCR_P(port));
1668 	if (ingress) {
1669 		val |= PORT_RX_MIR;
1670 		priv->mirror_rx |= BIT(port);
1671 	} else {
1672 		val |= PORT_TX_MIR;
1673 		priv->mirror_tx |= BIT(port);
1674 	}
1675 	mt7530_write(priv, MT7530_PCR_P(port), val);
1676 
1677 	return 0;
1678 }
1679 
1680 static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
1681 				   struct dsa_mall_mirror_tc_entry *mirror)
1682 {
1683 	struct mt7530_priv *priv = ds->priv;
1684 	u32 val;
1685 
1686 	val = mt7530_read(priv, MT7530_PCR_P(port));
1687 	if (mirror->ingress) {
1688 		val &= ~PORT_RX_MIR;
1689 		priv->mirror_rx &= ~BIT(port);
1690 	} else {
1691 		val &= ~PORT_TX_MIR;
1692 		priv->mirror_tx &= ~BIT(port);
1693 	}
1694 	mt7530_write(priv, MT7530_PCR_P(port), val);
1695 
1696 	if (!priv->mirror_rx && !priv->mirror_tx) {
1697 		val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1698 		val &= ~MT753X_MIRROR_EN(priv->id);
1699 		mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1700 	}
1701 }
1702 
1703 static enum dsa_tag_protocol
1704 mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1705 		     enum dsa_tag_protocol mp)
1706 {
1707 	struct mt7530_priv *priv = ds->priv;
1708 
1709 	if (port != MT7530_CPU_PORT) {
1710 		dev_warn(priv->dev,
1711 			 "port not matched with tagging CPU port\n");
1712 		return DSA_TAG_PROTO_NONE;
1713 	} else {
1714 		return DSA_TAG_PROTO_MTK;
1715 	}
1716 }
1717 
1718 #ifdef CONFIG_GPIOLIB
1719 static inline u32
1720 mt7530_gpio_to_bit(unsigned int offset)
1721 {
1722 	/* Map GPIO offset to register bit
1723 	 * [ 2: 0]  port 0 LED 0..2 as GPIO 0..2
1724 	 * [ 6: 4]  port 1 LED 0..2 as GPIO 3..5
1725 	 * [10: 8]  port 2 LED 0..2 as GPIO 6..8
1726 	 * [14:12]  port 3 LED 0..2 as GPIO 9..11
1727 	 * [18:16]  port 4 LED 0..2 as GPIO 12..14
1728 	 */
1729 	return BIT(offset + offset / 3);
1730 }
1731 
1732 static int
1733 mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
1734 {
1735 	struct mt7530_priv *priv = gpiochip_get_data(gc);
1736 	u32 bit = mt7530_gpio_to_bit(offset);
1737 
1738 	return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
1739 }
1740 
1741 static void
1742 mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
1743 {
1744 	struct mt7530_priv *priv = gpiochip_get_data(gc);
1745 	u32 bit = mt7530_gpio_to_bit(offset);
1746 
1747 	if (value)
1748 		mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1749 	else
1750 		mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1751 }
1752 
1753 static int
1754 mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
1755 {
1756 	struct mt7530_priv *priv = gpiochip_get_data(gc);
1757 	u32 bit = mt7530_gpio_to_bit(offset);
1758 
1759 	return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
1760 		GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1761 }
1762 
1763 static int
1764 mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
1765 {
1766 	struct mt7530_priv *priv = gpiochip_get_data(gc);
1767 	u32 bit = mt7530_gpio_to_bit(offset);
1768 
1769 	mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
1770 	mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
1771 
1772 	return 0;
1773 }
1774 
1775 static int
1776 mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
1777 {
1778 	struct mt7530_priv *priv = gpiochip_get_data(gc);
1779 	u32 bit = mt7530_gpio_to_bit(offset);
1780 
1781 	mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
1782 
1783 	if (value)
1784 		mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1785 	else
1786 		mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1787 
1788 	mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
1789 
1790 	return 0;
1791 }
1792 
1793 static int
1794 mt7530_setup_gpio(struct mt7530_priv *priv)
1795 {
1796 	struct device *dev = priv->dev;
1797 	struct gpio_chip *gc;
1798 
1799 	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
1800 	if (!gc)
1801 		return -ENOMEM;
1802 
1803 	mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
1804 	mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
1805 	mt7530_write(priv, MT7530_LED_IO_MODE, 0);
1806 
1807 	gc->label = "mt7530";
1808 	gc->parent = dev;
1809 	gc->owner = THIS_MODULE;
1810 	gc->get_direction = mt7530_gpio_get_direction;
1811 	gc->direction_input = mt7530_gpio_direction_input;
1812 	gc->direction_output = mt7530_gpio_direction_output;
1813 	gc->get = mt7530_gpio_get;
1814 	gc->set = mt7530_gpio_set;
1815 	gc->base = -1;
1816 	gc->ngpio = 15;
1817 	gc->can_sleep = true;
1818 
1819 	return devm_gpiochip_add_data(dev, gc, priv);
1820 }
1821 #endif /* CONFIG_GPIOLIB */
1822 
1823 static int
1824 mt7530_setup(struct dsa_switch *ds)
1825 {
1826 	struct mt7530_priv *priv = ds->priv;
1827 	struct device_node *phy_node;
1828 	struct device_node *mac_np;
1829 	struct mt7530_dummy_poll p;
1830 	phy_interface_t interface;
1831 	struct device_node *dn;
1832 	u32 id, val;
1833 	int ret, i;
1834 
1835 	/* The parent node of master netdev which holds the common system
1836 	 * controller also is the container for two GMACs nodes representing
1837 	 * as two netdev instances.
1838 	 */
1839 	dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
1840 	ds->mtu_enforcement_ingress = true;
1841 
1842 	if (priv->id == ID_MT7530) {
1843 		regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1844 		ret = regulator_enable(priv->core_pwr);
1845 		if (ret < 0) {
1846 			dev_err(priv->dev,
1847 				"Failed to enable core power: %d\n", ret);
1848 			return ret;
1849 		}
1850 
1851 		regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1852 		ret = regulator_enable(priv->io_pwr);
1853 		if (ret < 0) {
1854 			dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1855 				ret);
1856 			return ret;
1857 		}
1858 	}
1859 
1860 	/* Reset whole chip through gpio pin or memory-mapped registers for
1861 	 * different type of hardware
1862 	 */
1863 	if (priv->mcm) {
1864 		reset_control_assert(priv->rstc);
1865 		usleep_range(1000, 1100);
1866 		reset_control_deassert(priv->rstc);
1867 	} else {
1868 		gpiod_set_value_cansleep(priv->reset, 0);
1869 		usleep_range(1000, 1100);
1870 		gpiod_set_value_cansleep(priv->reset, 1);
1871 	}
1872 
1873 	/* Waiting for MT7530 got to stable */
1874 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1875 	ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1876 				 20, 1000000);
1877 	if (ret < 0) {
1878 		dev_err(priv->dev, "reset timeout\n");
1879 		return ret;
1880 	}
1881 
1882 	id = mt7530_read(priv, MT7530_CREV);
1883 	id >>= CHIP_NAME_SHIFT;
1884 	if (id != MT7530_ID) {
1885 		dev_err(priv->dev, "chip %x can't be supported\n", id);
1886 		return -ENODEV;
1887 	}
1888 
1889 	/* Reset the switch through internal reset */
1890 	mt7530_write(priv, MT7530_SYS_CTRL,
1891 		     SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1892 		     SYS_CTRL_REG_RST);
1893 
1894 	/* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1895 	val = mt7530_read(priv, MT7530_MHWTRAP);
1896 	val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1897 	val |= MHWTRAP_MANUAL;
1898 	mt7530_write(priv, MT7530_MHWTRAP, val);
1899 
1900 	priv->p6_interface = PHY_INTERFACE_MODE_NA;
1901 
1902 	/* Enable and reset MIB counters */
1903 	mt7530_mib_reset(ds);
1904 
1905 	for (i = 0; i < MT7530_NUM_PORTS; i++) {
1906 		/* Disable forwarding by default on all ports */
1907 		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1908 			   PCR_MATRIX_CLR);
1909 
1910 		if (dsa_is_cpu_port(ds, i)) {
1911 			ret = mt753x_cpu_port_enable(ds, i);
1912 			if (ret)
1913 				return ret;
1914 		} else {
1915 			mt7530_port_disable(ds, i);
1916 
1917 			/* Disable learning by default on all user ports */
1918 			mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
1919 		}
1920 		/* Enable consistent egress tag */
1921 		mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
1922 			   PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1923 	}
1924 
1925 	/* Setup port 5 */
1926 	priv->p5_intf_sel = P5_DISABLED;
1927 	interface = PHY_INTERFACE_MODE_NA;
1928 
1929 	if (!dsa_is_unused_port(ds, 5)) {
1930 		priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
1931 		ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
1932 		if (ret && ret != -ENODEV)
1933 			return ret;
1934 	} else {
1935 		/* Scan the ethernet nodes. look for GMAC1, lookup used phy */
1936 		for_each_child_of_node(dn, mac_np) {
1937 			if (!of_device_is_compatible(mac_np,
1938 						     "mediatek,eth-mac"))
1939 				continue;
1940 
1941 			ret = of_property_read_u32(mac_np, "reg", &id);
1942 			if (ret < 0 || id != 1)
1943 				continue;
1944 
1945 			phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
1946 			if (!phy_node)
1947 				continue;
1948 
1949 			if (phy_node->parent == priv->dev->of_node->parent) {
1950 				ret = of_get_phy_mode(mac_np, &interface);
1951 				if (ret && ret != -ENODEV) {
1952 					of_node_put(mac_np);
1953 					return ret;
1954 				}
1955 				id = of_mdio_parse_addr(ds->dev, phy_node);
1956 				if (id == 0)
1957 					priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
1958 				if (id == 4)
1959 					priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
1960 			}
1961 			of_node_put(mac_np);
1962 			of_node_put(phy_node);
1963 			break;
1964 		}
1965 	}
1966 
1967 #ifdef CONFIG_GPIOLIB
1968 	if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
1969 		ret = mt7530_setup_gpio(priv);
1970 		if (ret)
1971 			return ret;
1972 	}
1973 #endif /* CONFIG_GPIOLIB */
1974 
1975 	mt7530_setup_port5(ds, interface);
1976 
1977 	/* Flush the FDB table */
1978 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
1979 	if (ret < 0)
1980 		return ret;
1981 
1982 	return 0;
1983 }
1984 
1985 static int
1986 mt7531_setup(struct dsa_switch *ds)
1987 {
1988 	struct mt7530_priv *priv = ds->priv;
1989 	struct mt7530_dummy_poll p;
1990 	u32 val, id;
1991 	int ret, i;
1992 
1993 	/* Reset whole chip through gpio pin or memory-mapped registers for
1994 	 * different type of hardware
1995 	 */
1996 	if (priv->mcm) {
1997 		reset_control_assert(priv->rstc);
1998 		usleep_range(1000, 1100);
1999 		reset_control_deassert(priv->rstc);
2000 	} else {
2001 		gpiod_set_value_cansleep(priv->reset, 0);
2002 		usleep_range(1000, 1100);
2003 		gpiod_set_value_cansleep(priv->reset, 1);
2004 	}
2005 
2006 	/* Waiting for MT7530 got to stable */
2007 	INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2008 	ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2009 				 20, 1000000);
2010 	if (ret < 0) {
2011 		dev_err(priv->dev, "reset timeout\n");
2012 		return ret;
2013 	}
2014 
2015 	id = mt7530_read(priv, MT7531_CREV);
2016 	id >>= CHIP_NAME_SHIFT;
2017 
2018 	if (id != MT7531_ID) {
2019 		dev_err(priv->dev, "chip %x can't be supported\n", id);
2020 		return -ENODEV;
2021 	}
2022 
2023 	/* Reset the switch through internal reset */
2024 	mt7530_write(priv, MT7530_SYS_CTRL,
2025 		     SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2026 		     SYS_CTRL_REG_RST);
2027 
2028 	if (mt7531_dual_sgmii_supported(priv)) {
2029 		priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII;
2030 
2031 		/* Let ds->slave_mii_bus be able to access external phy. */
2032 		mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
2033 			   MT7531_EXT_P_MDC_11);
2034 		mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
2035 			   MT7531_EXT_P_MDIO_12);
2036 	} else {
2037 		priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
2038 	}
2039 	dev_dbg(ds->dev, "P5 support %s interface\n",
2040 		p5_intf_modes(priv->p5_intf_sel));
2041 
2042 	mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
2043 		   MT7531_GPIO0_INTERRUPT);
2044 
2045 	/* Let phylink decide the interface later. */
2046 	priv->p5_interface = PHY_INTERFACE_MODE_NA;
2047 	priv->p6_interface = PHY_INTERFACE_MODE_NA;
2048 
2049 	/* Enable PHY core PLL, since phy_device has not yet been created
2050 	 * provided for phy_[read,write]_mmd_indirect is called, we provide
2051 	 * our own mt7531_ind_mmd_phy_[read,write] to complete this
2052 	 * function.
2053 	 */
2054 	val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR,
2055 				      MDIO_MMD_VEND2, CORE_PLL_GROUP4);
2056 	val |= MT7531_PHY_PLL_BYPASS_MODE;
2057 	val &= ~MT7531_PHY_PLL_OFF;
2058 	mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2,
2059 				 CORE_PLL_GROUP4, val);
2060 
2061 	/* BPDU to CPU port */
2062 	mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK,
2063 		   BIT(MT7530_CPU_PORT));
2064 	mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK,
2065 		   MT753X_BPDU_CPU_ONLY);
2066 
2067 	/* Enable and reset MIB counters */
2068 	mt7530_mib_reset(ds);
2069 
2070 	for (i = 0; i < MT7530_NUM_PORTS; i++) {
2071 		/* Disable forwarding by default on all ports */
2072 		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2073 			   PCR_MATRIX_CLR);
2074 
2075 		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
2076 
2077 		if (dsa_is_cpu_port(ds, i)) {
2078 			ret = mt753x_cpu_port_enable(ds, i);
2079 			if (ret)
2080 				return ret;
2081 		} else {
2082 			mt7530_port_disable(ds, i);
2083 
2084 			/* Disable learning by default on all user ports */
2085 			mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2086 		}
2087 
2088 		/* Enable consistent egress tag */
2089 		mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2090 			   PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2091 	}
2092 
2093 	ds->mtu_enforcement_ingress = true;
2094 
2095 	/* Flush the FDB table */
2096 	ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2097 	if (ret < 0)
2098 		return ret;
2099 
2100 	return 0;
2101 }
2102 
2103 static bool
2104 mt7530_phy_mode_supported(struct dsa_switch *ds, int port,
2105 			  const struct phylink_link_state *state)
2106 {
2107 	struct mt7530_priv *priv = ds->priv;
2108 
2109 	switch (port) {
2110 	case 0 ... 4: /* Internal phy */
2111 		if (state->interface != PHY_INTERFACE_MODE_GMII)
2112 			return false;
2113 		break;
2114 	case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2115 		if (!phy_interface_mode_is_rgmii(state->interface) &&
2116 		    state->interface != PHY_INTERFACE_MODE_MII &&
2117 		    state->interface != PHY_INTERFACE_MODE_GMII)
2118 			return false;
2119 		break;
2120 	case 6: /* 1st cpu port */
2121 		if (state->interface != PHY_INTERFACE_MODE_RGMII &&
2122 		    state->interface != PHY_INTERFACE_MODE_TRGMII)
2123 			return false;
2124 		break;
2125 	default:
2126 		dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2127 			port);
2128 		return false;
2129 	}
2130 
2131 	return true;
2132 }
2133 
2134 static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port)
2135 {
2136 	return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII);
2137 }
2138 
2139 static bool
2140 mt7531_phy_mode_supported(struct dsa_switch *ds, int port,
2141 			  const struct phylink_link_state *state)
2142 {
2143 	struct mt7530_priv *priv = ds->priv;
2144 
2145 	switch (port) {
2146 	case 0 ... 4: /* Internal phy */
2147 		if (state->interface != PHY_INTERFACE_MODE_GMII)
2148 			return false;
2149 		break;
2150 	case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */
2151 		if (mt7531_is_rgmii_port(priv, port))
2152 			return phy_interface_mode_is_rgmii(state->interface);
2153 		fallthrough;
2154 	case 6: /* 1st cpu port supports sgmii/8023z only */
2155 		if (state->interface != PHY_INTERFACE_MODE_SGMII &&
2156 		    !phy_interface_mode_is_8023z(state->interface))
2157 			return false;
2158 		break;
2159 	default:
2160 		dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2161 			port);
2162 		return false;
2163 	}
2164 
2165 	return true;
2166 }
2167 
2168 static bool
2169 mt753x_phy_mode_supported(struct dsa_switch *ds, int port,
2170 			  const struct phylink_link_state *state)
2171 {
2172 	struct mt7530_priv *priv = ds->priv;
2173 
2174 	return priv->info->phy_mode_supported(ds, port, state);
2175 }
2176 
2177 static int
2178 mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
2179 {
2180 	struct mt7530_priv *priv = ds->priv;
2181 
2182 	return priv->info->pad_setup(ds, state->interface);
2183 }
2184 
2185 static int
2186 mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2187 		  phy_interface_t interface)
2188 {
2189 	struct mt7530_priv *priv = ds->priv;
2190 
2191 	/* Only need to setup port5. */
2192 	if (port != 5)
2193 		return 0;
2194 
2195 	mt7530_setup_port5(priv->ds, interface);
2196 
2197 	return 0;
2198 }
2199 
2200 static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port,
2201 			      phy_interface_t interface,
2202 			      struct phy_device *phydev)
2203 {
2204 	u32 val;
2205 
2206 	if (!mt7531_is_rgmii_port(priv, port)) {
2207 		dev_err(priv->dev, "RGMII mode is not available for port %d\n",
2208 			port);
2209 		return -EINVAL;
2210 	}
2211 
2212 	val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2213 	val |= GP_CLK_EN;
2214 	val &= ~GP_MODE_MASK;
2215 	val |= GP_MODE(MT7531_GP_MODE_RGMII);
2216 	val &= ~CLK_SKEW_IN_MASK;
2217 	val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2218 	val &= ~CLK_SKEW_OUT_MASK;
2219 	val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2220 	val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2221 
2222 	/* Do not adjust rgmii delay when vendor phy driver presents. */
2223 	if (!phydev || phy_driver_is_genphy(phydev)) {
2224 		val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2225 		switch (interface) {
2226 		case PHY_INTERFACE_MODE_RGMII:
2227 			val |= TXCLK_NO_REVERSE;
2228 			val |= RXCLK_NO_DELAY;
2229 			break;
2230 		case PHY_INTERFACE_MODE_RGMII_RXID:
2231 			val |= TXCLK_NO_REVERSE;
2232 			break;
2233 		case PHY_INTERFACE_MODE_RGMII_TXID:
2234 			val |= RXCLK_NO_DELAY;
2235 			break;
2236 		case PHY_INTERFACE_MODE_RGMII_ID:
2237 			break;
2238 		default:
2239 			return -EINVAL;
2240 		}
2241 	}
2242 	mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2243 
2244 	return 0;
2245 }
2246 
2247 static void mt7531_sgmii_validate(struct mt7530_priv *priv, int port,
2248 				  unsigned long *supported)
2249 {
2250 	/* Port5 supports ethier RGMII or SGMII.
2251 	 * Port6 supports SGMII only.
2252 	 */
2253 	switch (port) {
2254 	case 5:
2255 		if (mt7531_is_rgmii_port(priv, port))
2256 			break;
2257 		fallthrough;
2258 	case 6:
2259 		phylink_set(supported, 1000baseX_Full);
2260 		phylink_set(supported, 2500baseX_Full);
2261 		phylink_set(supported, 2500baseT_Full);
2262 	}
2263 }
2264 
2265 static void
2266 mt7531_sgmii_link_up_force(struct dsa_switch *ds, int port,
2267 			   unsigned int mode, phy_interface_t interface,
2268 			   int speed, int duplex)
2269 {
2270 	struct mt7530_priv *priv = ds->priv;
2271 	unsigned int val;
2272 
2273 	/* For adjusting speed and duplex of SGMII force mode. */
2274 	if (interface != PHY_INTERFACE_MODE_SGMII ||
2275 	    phylink_autoneg_inband(mode))
2276 		return;
2277 
2278 	/* SGMII force mode setting */
2279 	val = mt7530_read(priv, MT7531_SGMII_MODE(port));
2280 	val &= ~MT7531_SGMII_IF_MODE_MASK;
2281 
2282 	switch (speed) {
2283 	case SPEED_10:
2284 		val |= MT7531_SGMII_FORCE_SPEED_10;
2285 		break;
2286 	case SPEED_100:
2287 		val |= MT7531_SGMII_FORCE_SPEED_100;
2288 		break;
2289 	case SPEED_1000:
2290 		val |= MT7531_SGMII_FORCE_SPEED_1000;
2291 		break;
2292 	}
2293 
2294 	/* MT7531 SGMII 1G force mode can only work in full duplex mode,
2295 	 * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2296 	 */
2297 	if ((speed == SPEED_10 || speed == SPEED_100) &&
2298 	    duplex != DUPLEX_FULL)
2299 		val |= MT7531_SGMII_FORCE_HALF_DUPLEX;
2300 
2301 	mt7530_write(priv, MT7531_SGMII_MODE(port), val);
2302 }
2303 
2304 static bool mt753x_is_mac_port(u32 port)
2305 {
2306 	return (port == 5 || port == 6);
2307 }
2308 
2309 static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port,
2310 					 phy_interface_t interface)
2311 {
2312 	u32 val;
2313 
2314 	if (!mt753x_is_mac_port(port))
2315 		return -EINVAL;
2316 
2317 	mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2318 		   MT7531_SGMII_PHYA_PWD);
2319 
2320 	val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port));
2321 	val &= ~MT7531_RG_TPHY_SPEED_MASK;
2322 	/* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B
2323 	 * encoding.
2324 	 */
2325 	val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ?
2326 		MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G;
2327 	mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val);
2328 
2329 	mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2330 
2331 	/* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex
2332 	 * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2333 	 */
2334 	mt7530_rmw(priv, MT7531_SGMII_MODE(port),
2335 		   MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS,
2336 		   MT7531_SGMII_FORCE_SPEED_1000);
2337 
2338 	mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2339 
2340 	return 0;
2341 }
2342 
2343 static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port,
2344 				      phy_interface_t interface)
2345 {
2346 	if (!mt753x_is_mac_port(port))
2347 		return -EINVAL;
2348 
2349 	mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2350 		   MT7531_SGMII_PHYA_PWD);
2351 
2352 	mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
2353 		   MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G);
2354 
2355 	mt7530_set(priv, MT7531_SGMII_MODE(port),
2356 		   MT7531_SGMII_REMOTE_FAULT_DIS |
2357 		   MT7531_SGMII_SPEED_DUPLEX_AN);
2358 
2359 	mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port),
2360 		   MT7531_SGMII_TX_CONFIG_MASK, 1);
2361 
2362 	mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2363 
2364 	mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART);
2365 
2366 	mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2367 
2368 	return 0;
2369 }
2370 
2371 static void mt7531_sgmii_restart_an(struct dsa_switch *ds, int port)
2372 {
2373 	struct mt7530_priv *priv = ds->priv;
2374 	u32 val;
2375 
2376 	/* Only restart AN when AN is enabled */
2377 	val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2378 	if (val & MT7531_SGMII_AN_ENABLE) {
2379 		val |= MT7531_SGMII_AN_RESTART;
2380 		mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val);
2381 	}
2382 }
2383 
2384 static int
2385 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2386 		  phy_interface_t interface)
2387 {
2388 	struct mt7530_priv *priv = ds->priv;
2389 	struct phy_device *phydev;
2390 	struct dsa_port *dp;
2391 
2392 	if (!mt753x_is_mac_port(port)) {
2393 		dev_err(priv->dev, "port %d is not a MAC port\n", port);
2394 		return -EINVAL;
2395 	}
2396 
2397 	switch (interface) {
2398 	case PHY_INTERFACE_MODE_RGMII:
2399 	case PHY_INTERFACE_MODE_RGMII_ID:
2400 	case PHY_INTERFACE_MODE_RGMII_RXID:
2401 	case PHY_INTERFACE_MODE_RGMII_TXID:
2402 		dp = dsa_to_port(ds, port);
2403 		phydev = dp->slave->phydev;
2404 		return mt7531_rgmii_setup(priv, port, interface, phydev);
2405 	case PHY_INTERFACE_MODE_SGMII:
2406 		return mt7531_sgmii_setup_mode_an(priv, port, interface);
2407 	case PHY_INTERFACE_MODE_NA:
2408 	case PHY_INTERFACE_MODE_1000BASEX:
2409 	case PHY_INTERFACE_MODE_2500BASEX:
2410 		if (phylink_autoneg_inband(mode))
2411 			return -EINVAL;
2412 
2413 		return mt7531_sgmii_setup_mode_force(priv, port, interface);
2414 	default:
2415 		return -EINVAL;
2416 	}
2417 
2418 	return -EINVAL;
2419 }
2420 
2421 static int
2422 mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2423 		  const struct phylink_link_state *state)
2424 {
2425 	struct mt7530_priv *priv = ds->priv;
2426 
2427 	return priv->info->mac_port_config(ds, port, mode, state->interface);
2428 }
2429 
2430 static void
2431 mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2432 			  const struct phylink_link_state *state)
2433 {
2434 	struct mt7530_priv *priv = ds->priv;
2435 	u32 mcr_cur, mcr_new;
2436 
2437 	if (!mt753x_phy_mode_supported(ds, port, state))
2438 		goto unsupported;
2439 
2440 	switch (port) {
2441 	case 0 ... 4: /* Internal phy */
2442 		if (state->interface != PHY_INTERFACE_MODE_GMII)
2443 			goto unsupported;
2444 		break;
2445 	case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2446 		if (priv->p5_interface == state->interface)
2447 			break;
2448 
2449 		if (mt753x_mac_config(ds, port, mode, state) < 0)
2450 			goto unsupported;
2451 
2452 		if (priv->p5_intf_sel != P5_DISABLED)
2453 			priv->p5_interface = state->interface;
2454 		break;
2455 	case 6: /* 1st cpu port */
2456 		if (priv->p6_interface == state->interface)
2457 			break;
2458 
2459 		mt753x_pad_setup(ds, state);
2460 
2461 		if (mt753x_mac_config(ds, port, mode, state) < 0)
2462 			goto unsupported;
2463 
2464 		priv->p6_interface = state->interface;
2465 		break;
2466 	default:
2467 unsupported:
2468 		dev_err(ds->dev, "%s: unsupported %s port: %i\n",
2469 			__func__, phy_modes(state->interface), port);
2470 		return;
2471 	}
2472 
2473 	if (phylink_autoneg_inband(mode) &&
2474 	    state->interface != PHY_INTERFACE_MODE_SGMII) {
2475 		dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
2476 			__func__);
2477 		return;
2478 	}
2479 
2480 	mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
2481 	mcr_new = mcr_cur;
2482 	mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
2483 	mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
2484 		   PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id);
2485 
2486 	/* Are we connected to external phy */
2487 	if (port == 5 && dsa_is_user_port(ds, 5))
2488 		mcr_new |= PMCR_EXT_PHY;
2489 
2490 	if (mcr_new != mcr_cur)
2491 		mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
2492 }
2493 
2494 static void
2495 mt753x_phylink_mac_an_restart(struct dsa_switch *ds, int port)
2496 {
2497 	struct mt7530_priv *priv = ds->priv;
2498 
2499 	if (!priv->info->mac_pcs_an_restart)
2500 		return;
2501 
2502 	priv->info->mac_pcs_an_restart(ds, port);
2503 }
2504 
2505 static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port,
2506 					 unsigned int mode,
2507 					 phy_interface_t interface)
2508 {
2509 	struct mt7530_priv *priv = ds->priv;
2510 
2511 	mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
2512 }
2513 
2514 static void mt753x_mac_pcs_link_up(struct dsa_switch *ds, int port,
2515 				   unsigned int mode, phy_interface_t interface,
2516 				   int speed, int duplex)
2517 {
2518 	struct mt7530_priv *priv = ds->priv;
2519 
2520 	if (!priv->info->mac_pcs_link_up)
2521 		return;
2522 
2523 	priv->info->mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2524 }
2525 
2526 static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port,
2527 				       unsigned int mode,
2528 				       phy_interface_t interface,
2529 				       struct phy_device *phydev,
2530 				       int speed, int duplex,
2531 				       bool tx_pause, bool rx_pause)
2532 {
2533 	struct mt7530_priv *priv = ds->priv;
2534 	u32 mcr;
2535 
2536 	mt753x_mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2537 
2538 	mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
2539 
2540 	/* MT753x MAC works in 1G full duplex mode for all up-clocked
2541 	 * variants.
2542 	 */
2543 	if (interface == PHY_INTERFACE_MODE_TRGMII ||
2544 	    (phy_interface_mode_is_8023z(interface))) {
2545 		speed = SPEED_1000;
2546 		duplex = DUPLEX_FULL;
2547 	}
2548 
2549 	switch (speed) {
2550 	case SPEED_1000:
2551 		mcr |= PMCR_FORCE_SPEED_1000;
2552 		break;
2553 	case SPEED_100:
2554 		mcr |= PMCR_FORCE_SPEED_100;
2555 		break;
2556 	}
2557 	if (duplex == DUPLEX_FULL) {
2558 		mcr |= PMCR_FORCE_FDX;
2559 		if (tx_pause)
2560 			mcr |= PMCR_TX_FC_EN;
2561 		if (rx_pause)
2562 			mcr |= PMCR_RX_FC_EN;
2563 	}
2564 
2565 	if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, 0) >= 0) {
2566 		switch (speed) {
2567 		case SPEED_1000:
2568 			mcr |= PMCR_FORCE_EEE1G;
2569 			break;
2570 		case SPEED_100:
2571 			mcr |= PMCR_FORCE_EEE100;
2572 			break;
2573 		}
2574 	}
2575 
2576 	mt7530_set(priv, MT7530_PMCR_P(port), mcr);
2577 }
2578 
2579 static int
2580 mt7531_cpu_port_config(struct dsa_switch *ds, int port)
2581 {
2582 	struct mt7530_priv *priv = ds->priv;
2583 	phy_interface_t interface;
2584 	int speed;
2585 	int ret;
2586 
2587 	switch (port) {
2588 	case 5:
2589 		if (mt7531_is_rgmii_port(priv, port))
2590 			interface = PHY_INTERFACE_MODE_RGMII;
2591 		else
2592 			interface = PHY_INTERFACE_MODE_2500BASEX;
2593 
2594 		priv->p5_interface = interface;
2595 		break;
2596 	case 6:
2597 		interface = PHY_INTERFACE_MODE_2500BASEX;
2598 
2599 		mt7531_pad_setup(ds, interface);
2600 
2601 		priv->p6_interface = interface;
2602 		break;
2603 	default:
2604 		return -EINVAL;
2605 	}
2606 
2607 	if (interface == PHY_INTERFACE_MODE_2500BASEX)
2608 		speed = SPEED_2500;
2609 	else
2610 		speed = SPEED_1000;
2611 
2612 	ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
2613 	if (ret)
2614 		return ret;
2615 	mt7530_write(priv, MT7530_PMCR_P(port),
2616 		     PMCR_CPU_PORT_SETTING(priv->id));
2617 	mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2618 				   speed, DUPLEX_FULL, true, true);
2619 
2620 	return 0;
2621 }
2622 
2623 static void
2624 mt7530_mac_port_validate(struct dsa_switch *ds, int port,
2625 			 unsigned long *supported)
2626 {
2627 	if (port == 5)
2628 		phylink_set(supported, 1000baseX_Full);
2629 }
2630 
2631 static void mt7531_mac_port_validate(struct dsa_switch *ds, int port,
2632 				     unsigned long *supported)
2633 {
2634 	struct mt7530_priv *priv = ds->priv;
2635 
2636 	mt7531_sgmii_validate(priv, port, supported);
2637 }
2638 
2639 static void
2640 mt753x_phylink_validate(struct dsa_switch *ds, int port,
2641 			unsigned long *supported,
2642 			struct phylink_link_state *state)
2643 {
2644 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
2645 	struct mt7530_priv *priv = ds->priv;
2646 
2647 	if (state->interface != PHY_INTERFACE_MODE_NA &&
2648 	    !mt753x_phy_mode_supported(ds, port, state)) {
2649 		linkmode_zero(supported);
2650 		return;
2651 	}
2652 
2653 	phylink_set_port_modes(mask);
2654 
2655 	if (state->interface != PHY_INTERFACE_MODE_TRGMII ||
2656 	    !phy_interface_mode_is_8023z(state->interface)) {
2657 		phylink_set(mask, 10baseT_Half);
2658 		phylink_set(mask, 10baseT_Full);
2659 		phylink_set(mask, 100baseT_Half);
2660 		phylink_set(mask, 100baseT_Full);
2661 		phylink_set(mask, Autoneg);
2662 	}
2663 
2664 	/* This switch only supports 1G full-duplex. */
2665 	if (state->interface != PHY_INTERFACE_MODE_MII)
2666 		phylink_set(mask, 1000baseT_Full);
2667 
2668 	priv->info->mac_port_validate(ds, port, mask);
2669 
2670 	phylink_set(mask, Pause);
2671 	phylink_set(mask, Asym_Pause);
2672 
2673 	linkmode_and(supported, supported, mask);
2674 	linkmode_and(state->advertising, state->advertising, mask);
2675 
2676 	/* We can only operate at 2500BaseX or 1000BaseX.  If requested
2677 	 * to advertise both, only report advertising at 2500BaseX.
2678 	 */
2679 	phylink_helper_basex_speed(state);
2680 }
2681 
2682 static int
2683 mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
2684 			      struct phylink_link_state *state)
2685 {
2686 	struct mt7530_priv *priv = ds->priv;
2687 	u32 pmsr;
2688 
2689 	if (port < 0 || port >= MT7530_NUM_PORTS)
2690 		return -EINVAL;
2691 
2692 	pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2693 
2694 	state->link = (pmsr & PMSR_LINK);
2695 	state->an_complete = state->link;
2696 	state->duplex = !!(pmsr & PMSR_DPX);
2697 
2698 	switch (pmsr & PMSR_SPEED_MASK) {
2699 	case PMSR_SPEED_10:
2700 		state->speed = SPEED_10;
2701 		break;
2702 	case PMSR_SPEED_100:
2703 		state->speed = SPEED_100;
2704 		break;
2705 	case PMSR_SPEED_1000:
2706 		state->speed = SPEED_1000;
2707 		break;
2708 	default:
2709 		state->speed = SPEED_UNKNOWN;
2710 		break;
2711 	}
2712 
2713 	state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2714 	if (pmsr & PMSR_RX_FC)
2715 		state->pause |= MLO_PAUSE_RX;
2716 	if (pmsr & PMSR_TX_FC)
2717 		state->pause |= MLO_PAUSE_TX;
2718 
2719 	return 1;
2720 }
2721 
2722 static int
2723 mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port,
2724 			      struct phylink_link_state *state)
2725 {
2726 	u32 status, val;
2727 	u16 config_reg;
2728 
2729 	status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2730 	state->link = !!(status & MT7531_SGMII_LINK_STATUS);
2731 	if (state->interface == PHY_INTERFACE_MODE_SGMII &&
2732 	    (status & MT7531_SGMII_AN_ENABLE)) {
2733 		val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port));
2734 		config_reg = val >> 16;
2735 
2736 		switch (config_reg & LPA_SGMII_SPD_MASK) {
2737 		case LPA_SGMII_1000:
2738 			state->speed = SPEED_1000;
2739 			break;
2740 		case LPA_SGMII_100:
2741 			state->speed = SPEED_100;
2742 			break;
2743 		case LPA_SGMII_10:
2744 			state->speed = SPEED_10;
2745 			break;
2746 		default:
2747 			dev_err(priv->dev, "invalid sgmii PHY speed\n");
2748 			state->link = false;
2749 			return -EINVAL;
2750 		}
2751 
2752 		if (config_reg & LPA_SGMII_FULL_DUPLEX)
2753 			state->duplex = DUPLEX_FULL;
2754 		else
2755 			state->duplex = DUPLEX_HALF;
2756 	}
2757 
2758 	return 0;
2759 }
2760 
2761 static int
2762 mt7531_phylink_mac_link_state(struct dsa_switch *ds, int port,
2763 			      struct phylink_link_state *state)
2764 {
2765 	struct mt7530_priv *priv = ds->priv;
2766 
2767 	if (state->interface == PHY_INTERFACE_MODE_SGMII)
2768 		return mt7531_sgmii_pcs_get_state_an(priv, port, state);
2769 
2770 	return -EOPNOTSUPP;
2771 }
2772 
2773 static int
2774 mt753x_phylink_mac_link_state(struct dsa_switch *ds, int port,
2775 			      struct phylink_link_state *state)
2776 {
2777 	struct mt7530_priv *priv = ds->priv;
2778 
2779 	return priv->info->mac_port_get_state(ds, port, state);
2780 }
2781 
2782 static int
2783 mt753x_setup(struct dsa_switch *ds)
2784 {
2785 	struct mt7530_priv *priv = ds->priv;
2786 
2787 	return priv->info->sw_setup(ds);
2788 }
2789 
2790 static int
2791 mt753x_phy_read(struct dsa_switch *ds, int port, int regnum)
2792 {
2793 	struct mt7530_priv *priv = ds->priv;
2794 
2795 	return priv->info->phy_read(ds, port, regnum);
2796 }
2797 
2798 static int
2799 mt753x_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2800 {
2801 	struct mt7530_priv *priv = ds->priv;
2802 
2803 	return priv->info->phy_write(ds, port, regnum, val);
2804 }
2805 
2806 static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
2807 			      struct ethtool_eee *e)
2808 {
2809 	struct mt7530_priv *priv = ds->priv;
2810 	u32 eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port));
2811 
2812 	e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
2813 	e->tx_lpi_timer = GET_LPI_THRESH(eeecr);
2814 
2815 	return 0;
2816 }
2817 
2818 static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
2819 			      struct ethtool_eee *e)
2820 {
2821 	struct mt7530_priv *priv = ds->priv;
2822 	u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
2823 
2824 	if (e->tx_lpi_timer > 0xFFF)
2825 		return -EINVAL;
2826 
2827 	set = SET_LPI_THRESH(e->tx_lpi_timer);
2828 	if (!e->tx_lpi_enabled)
2829 		/* Force LPI Mode without a delay */
2830 		set |= LPI_MODE_EN;
2831 	mt7530_rmw(priv, MT7530_PMEEECR_P(port), mask, set);
2832 
2833 	return 0;
2834 }
2835 
2836 static const struct dsa_switch_ops mt7530_switch_ops = {
2837 	.get_tag_protocol	= mtk_get_tag_protocol,
2838 	.setup			= mt753x_setup,
2839 	.get_strings		= mt7530_get_strings,
2840 	.phy_read		= mt753x_phy_read,
2841 	.phy_write		= mt753x_phy_write,
2842 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
2843 	.get_sset_count		= mt7530_get_sset_count,
2844 	.set_ageing_time	= mt7530_set_ageing_time,
2845 	.port_enable		= mt7530_port_enable,
2846 	.port_disable		= mt7530_port_disable,
2847 	.port_change_mtu	= mt7530_port_change_mtu,
2848 	.port_max_mtu		= mt7530_port_max_mtu,
2849 	.port_stp_state_set	= mt7530_stp_state_set,
2850 	.port_pre_bridge_flags	= mt7530_port_pre_bridge_flags,
2851 	.port_bridge_flags	= mt7530_port_bridge_flags,
2852 	.port_set_mrouter	= mt7530_port_set_mrouter,
2853 	.port_bridge_join	= mt7530_port_bridge_join,
2854 	.port_bridge_leave	= mt7530_port_bridge_leave,
2855 	.port_fdb_add		= mt7530_port_fdb_add,
2856 	.port_fdb_del		= mt7530_port_fdb_del,
2857 	.port_fdb_dump		= mt7530_port_fdb_dump,
2858 	.port_mdb_add		= mt7530_port_mdb_add,
2859 	.port_mdb_del		= mt7530_port_mdb_del,
2860 	.port_vlan_filtering	= mt7530_port_vlan_filtering,
2861 	.port_vlan_add		= mt7530_port_vlan_add,
2862 	.port_vlan_del		= mt7530_port_vlan_del,
2863 	.port_mirror_add	= mt753x_port_mirror_add,
2864 	.port_mirror_del	= mt753x_port_mirror_del,
2865 	.phylink_validate	= mt753x_phylink_validate,
2866 	.phylink_mac_link_state	= mt753x_phylink_mac_link_state,
2867 	.phylink_mac_config	= mt753x_phylink_mac_config,
2868 	.phylink_mac_an_restart	= mt753x_phylink_mac_an_restart,
2869 	.phylink_mac_link_down	= mt753x_phylink_mac_link_down,
2870 	.phylink_mac_link_up	= mt753x_phylink_mac_link_up,
2871 	.get_mac_eee		= mt753x_get_mac_eee,
2872 	.set_mac_eee		= mt753x_set_mac_eee,
2873 };
2874 
2875 static const struct mt753x_info mt753x_table[] = {
2876 	[ID_MT7621] = {
2877 		.id = ID_MT7621,
2878 		.sw_setup = mt7530_setup,
2879 		.phy_read = mt7530_phy_read,
2880 		.phy_write = mt7530_phy_write,
2881 		.pad_setup = mt7530_pad_clk_setup,
2882 		.phy_mode_supported = mt7530_phy_mode_supported,
2883 		.mac_port_validate = mt7530_mac_port_validate,
2884 		.mac_port_get_state = mt7530_phylink_mac_link_state,
2885 		.mac_port_config = mt7530_mac_config,
2886 	},
2887 	[ID_MT7530] = {
2888 		.id = ID_MT7530,
2889 		.sw_setup = mt7530_setup,
2890 		.phy_read = mt7530_phy_read,
2891 		.phy_write = mt7530_phy_write,
2892 		.pad_setup = mt7530_pad_clk_setup,
2893 		.phy_mode_supported = mt7530_phy_mode_supported,
2894 		.mac_port_validate = mt7530_mac_port_validate,
2895 		.mac_port_get_state = mt7530_phylink_mac_link_state,
2896 		.mac_port_config = mt7530_mac_config,
2897 	},
2898 	[ID_MT7531] = {
2899 		.id = ID_MT7531,
2900 		.sw_setup = mt7531_setup,
2901 		.phy_read = mt7531_ind_phy_read,
2902 		.phy_write = mt7531_ind_phy_write,
2903 		.pad_setup = mt7531_pad_setup,
2904 		.cpu_port_config = mt7531_cpu_port_config,
2905 		.phy_mode_supported = mt7531_phy_mode_supported,
2906 		.mac_port_validate = mt7531_mac_port_validate,
2907 		.mac_port_get_state = mt7531_phylink_mac_link_state,
2908 		.mac_port_config = mt7531_mac_config,
2909 		.mac_pcs_an_restart = mt7531_sgmii_restart_an,
2910 		.mac_pcs_link_up = mt7531_sgmii_link_up_force,
2911 	},
2912 };
2913 
2914 static const struct of_device_id mt7530_of_match[] = {
2915 	{ .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
2916 	{ .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
2917 	{ .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
2918 	{ /* sentinel */ },
2919 };
2920 MODULE_DEVICE_TABLE(of, mt7530_of_match);
2921 
2922 static int
2923 mt7530_probe(struct mdio_device *mdiodev)
2924 {
2925 	struct mt7530_priv *priv;
2926 	struct device_node *dn;
2927 
2928 	dn = mdiodev->dev.of_node;
2929 
2930 	priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
2931 	if (!priv)
2932 		return -ENOMEM;
2933 
2934 	priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
2935 	if (!priv->ds)
2936 		return -ENOMEM;
2937 
2938 	priv->ds->dev = &mdiodev->dev;
2939 	priv->ds->num_ports = DSA_MAX_PORTS;
2940 
2941 	/* Use medatek,mcm property to distinguish hardware type that would
2942 	 * casues a little bit differences on power-on sequence.
2943 	 */
2944 	priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
2945 	if (priv->mcm) {
2946 		dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
2947 
2948 		priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
2949 		if (IS_ERR(priv->rstc)) {
2950 			dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
2951 			return PTR_ERR(priv->rstc);
2952 		}
2953 	}
2954 
2955 	/* Get the hardware identifier from the devicetree node.
2956 	 * We will need it for some of the clock and regulator setup.
2957 	 */
2958 	priv->info = of_device_get_match_data(&mdiodev->dev);
2959 	if (!priv->info)
2960 		return -EINVAL;
2961 
2962 	/* Sanity check if these required device operations are filled
2963 	 * properly.
2964 	 */
2965 	if (!priv->info->sw_setup || !priv->info->pad_setup ||
2966 	    !priv->info->phy_read || !priv->info->phy_write ||
2967 	    !priv->info->phy_mode_supported ||
2968 	    !priv->info->mac_port_validate ||
2969 	    !priv->info->mac_port_get_state || !priv->info->mac_port_config)
2970 		return -EINVAL;
2971 
2972 	priv->id = priv->info->id;
2973 
2974 	if (priv->id == ID_MT7530) {
2975 		priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
2976 		if (IS_ERR(priv->core_pwr))
2977 			return PTR_ERR(priv->core_pwr);
2978 
2979 		priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
2980 		if (IS_ERR(priv->io_pwr))
2981 			return PTR_ERR(priv->io_pwr);
2982 	}
2983 
2984 	/* Not MCM that indicates switch works as the remote standalone
2985 	 * integrated circuit so the GPIO pin would be used to complete
2986 	 * the reset, otherwise memory-mapped register accessing used
2987 	 * through syscon provides in the case of MCM.
2988 	 */
2989 	if (!priv->mcm) {
2990 		priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
2991 						      GPIOD_OUT_LOW);
2992 		if (IS_ERR(priv->reset)) {
2993 			dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
2994 			return PTR_ERR(priv->reset);
2995 		}
2996 	}
2997 
2998 	priv->bus = mdiodev->bus;
2999 	priv->dev = &mdiodev->dev;
3000 	priv->ds->priv = priv;
3001 	priv->ds->ops = &mt7530_switch_ops;
3002 	mutex_init(&priv->reg_mutex);
3003 	dev_set_drvdata(&mdiodev->dev, priv);
3004 
3005 	return dsa_register_switch(priv->ds);
3006 }
3007 
3008 static void
3009 mt7530_remove(struct mdio_device *mdiodev)
3010 {
3011 	struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
3012 	int ret = 0;
3013 
3014 	ret = regulator_disable(priv->core_pwr);
3015 	if (ret < 0)
3016 		dev_err(priv->dev,
3017 			"Failed to disable core power: %d\n", ret);
3018 
3019 	ret = regulator_disable(priv->io_pwr);
3020 	if (ret < 0)
3021 		dev_err(priv->dev, "Failed to disable io pwr: %d\n",
3022 			ret);
3023 
3024 	dsa_unregister_switch(priv->ds);
3025 	mutex_destroy(&priv->reg_mutex);
3026 }
3027 
3028 static struct mdio_driver mt7530_mdio_driver = {
3029 	.probe  = mt7530_probe,
3030 	.remove = mt7530_remove,
3031 	.mdiodrv.driver = {
3032 		.name = "mt7530",
3033 		.of_match_table = mt7530_of_match,
3034 	},
3035 };
3036 
3037 mdio_module_driver(mt7530_mdio_driver);
3038 
3039 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
3040 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
3041 MODULE_LICENSE("GPL");
3042