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