net: dsa: mt7530: support setting ageing time
[linux-2.6-microblaze.git] / drivers / net / dsa / mt7530.c
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 int
874 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
875 {
876         struct mt7530_priv *priv = ds->priv;
877         unsigned int secs = msecs / 1000;
878         unsigned int tmp_age_count;
879         unsigned int error = -1;
880         unsigned int age_count;
881         unsigned int age_unit;
882
883         /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
884         if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
885                 return -ERANGE;
886
887         /* iterate through all possible age_count to find the closest pair */
888         for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
889                 unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
890
891                 if (tmp_age_unit <= AGE_UNIT_MAX) {
892                         unsigned int tmp_error = secs -
893                                 (tmp_age_count + 1) * (tmp_age_unit + 1);
894
895                         /* found a closer pair */
896                         if (error > tmp_error) {
897                                 error = tmp_error;
898                                 age_count = tmp_age_count;
899                                 age_unit = tmp_age_unit;
900                         }
901
902                         /* found the exact match, so break the loop */
903                         if (!error)
904                                 break;
905                 }
906         }
907
908         mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
909
910         return 0;
911 }
912
913 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
914 {
915         struct mt7530_priv *priv = ds->priv;
916         u8 tx_delay = 0;
917         int val;
918
919         mutex_lock(&priv->reg_mutex);
920
921         val = mt7530_read(priv, MT7530_MHWTRAP);
922
923         val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
924         val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
925
926         switch (priv->p5_intf_sel) {
927         case P5_INTF_SEL_PHY_P0:
928                 /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
929                 val |= MHWTRAP_PHY0_SEL;
930                 fallthrough;
931         case P5_INTF_SEL_PHY_P4:
932                 /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
933                 val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
934
935                 /* Setup the MAC by default for the cpu port */
936                 mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
937                 break;
938         case P5_INTF_SEL_GMAC5:
939                 /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
940                 val &= ~MHWTRAP_P5_DIS;
941                 break;
942         case P5_DISABLED:
943                 interface = PHY_INTERFACE_MODE_NA;
944                 break;
945         default:
946                 dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
947                         priv->p5_intf_sel);
948                 goto unlock_exit;
949         }
950
951         /* Setup RGMII settings */
952         if (phy_interface_mode_is_rgmii(interface)) {
953                 val |= MHWTRAP_P5_RGMII_MODE;
954
955                 /* P5 RGMII RX Clock Control: delay setting for 1000M */
956                 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
957
958                 /* Don't set delay in DSA mode */
959                 if (!dsa_is_dsa_port(priv->ds, 5) &&
960                     (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
961                      interface == PHY_INTERFACE_MODE_RGMII_ID))
962                         tx_delay = 4; /* n * 0.5 ns */
963
964                 /* P5 RGMII TX Clock Control: delay x */
965                 mt7530_write(priv, MT7530_P5RGMIITXCR,
966                              CSR_RGMII_TXC_CFG(0x10 + tx_delay));
967
968                 /* reduce P5 RGMII Tx driving, 8mA */
969                 mt7530_write(priv, MT7530_IO_DRV_CR,
970                              P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
971         }
972
973         mt7530_write(priv, MT7530_MHWTRAP, val);
974
975         dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
976                 val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
977
978         priv->p5_interface = interface;
979
980 unlock_exit:
981         mutex_unlock(&priv->reg_mutex);
982 }
983
984 static int
985 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
986 {
987         struct mt7530_priv *priv = ds->priv;
988         int ret;
989
990         /* Setup max capability of CPU port at first */
991         if (priv->info->cpu_port_config) {
992                 ret = priv->info->cpu_port_config(ds, port);
993                 if (ret)
994                         return ret;
995         }
996
997         /* Enable Mediatek header mode on the cpu port */
998         mt7530_write(priv, MT7530_PVC_P(port),
999                      PORT_SPEC_TAG);
1000
1001         /* Unknown multicast frame forwarding to the cpu port */
1002         mt7530_rmw(priv, MT7530_MFC, UNM_FFP_MASK, UNM_FFP(BIT(port)));
1003
1004         /* Set CPU port number */
1005         if (priv->id == ID_MT7621)
1006                 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
1007
1008         /* CPU port gets connected to all user ports of
1009          * the switch.
1010          */
1011         mt7530_write(priv, MT7530_PCR_P(port),
1012                      PCR_MATRIX(dsa_user_ports(priv->ds)));
1013
1014         return 0;
1015 }
1016
1017 static int
1018 mt7530_port_enable(struct dsa_switch *ds, int port,
1019                    struct phy_device *phy)
1020 {
1021         struct mt7530_priv *priv = ds->priv;
1022
1023         if (!dsa_is_user_port(ds, port))
1024                 return 0;
1025
1026         mutex_lock(&priv->reg_mutex);
1027
1028         /* Allow the user port gets connected to the cpu port and also
1029          * restore the port matrix if the port is the member of a certain
1030          * bridge.
1031          */
1032         priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
1033         priv->ports[port].enable = true;
1034         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1035                    priv->ports[port].pm);
1036         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1037
1038         mutex_unlock(&priv->reg_mutex);
1039
1040         return 0;
1041 }
1042
1043 static void
1044 mt7530_port_disable(struct dsa_switch *ds, int port)
1045 {
1046         struct mt7530_priv *priv = ds->priv;
1047
1048         if (!dsa_is_user_port(ds, port))
1049                 return;
1050
1051         mutex_lock(&priv->reg_mutex);
1052
1053         /* Clear up all port matrix which could be restored in the next
1054          * enablement for the port.
1055          */
1056         priv->ports[port].enable = false;
1057         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1058                    PCR_MATRIX_CLR);
1059         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1060
1061         mutex_unlock(&priv->reg_mutex);
1062 }
1063
1064 static int
1065 mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1066 {
1067         struct mt7530_priv *priv = ds->priv;
1068         struct mii_bus *bus = priv->bus;
1069         int length;
1070         u32 val;
1071
1072         /* When a new MTU is set, DSA always set the CPU port's MTU to the
1073          * largest MTU of the slave ports. Because the switch only has a global
1074          * RX length register, only allowing CPU port here is enough.
1075          */
1076         if (!dsa_is_cpu_port(ds, port))
1077                 return 0;
1078
1079         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1080
1081         val = mt7530_mii_read(priv, MT7530_GMACCR);
1082         val &= ~MAX_RX_PKT_LEN_MASK;
1083
1084         /* RX length also includes Ethernet header, MTK tag, and FCS length */
1085         length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
1086         if (length <= 1522) {
1087                 val |= MAX_RX_PKT_LEN_1522;
1088         } else if (length <= 1536) {
1089                 val |= MAX_RX_PKT_LEN_1536;
1090         } else if (length <= 1552) {
1091                 val |= MAX_RX_PKT_LEN_1552;
1092         } else {
1093                 val &= ~MAX_RX_JUMBO_MASK;
1094                 val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
1095                 val |= MAX_RX_PKT_LEN_JUMBO;
1096         }
1097
1098         mt7530_mii_write(priv, MT7530_GMACCR, val);
1099
1100         mutex_unlock(&bus->mdio_lock);
1101
1102         return 0;
1103 }
1104
1105 static int
1106 mt7530_port_max_mtu(struct dsa_switch *ds, int port)
1107 {
1108         return MT7530_MAX_MTU;
1109 }
1110
1111 static void
1112 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1113 {
1114         struct mt7530_priv *priv = ds->priv;
1115         u32 stp_state;
1116
1117         switch (state) {
1118         case BR_STATE_DISABLED:
1119                 stp_state = MT7530_STP_DISABLED;
1120                 break;
1121         case BR_STATE_BLOCKING:
1122                 stp_state = MT7530_STP_BLOCKING;
1123                 break;
1124         case BR_STATE_LISTENING:
1125                 stp_state = MT7530_STP_LISTENING;
1126                 break;
1127         case BR_STATE_LEARNING:
1128                 stp_state = MT7530_STP_LEARNING;
1129                 break;
1130         case BR_STATE_FORWARDING:
1131         default:
1132                 stp_state = MT7530_STP_FORWARDING;
1133                 break;
1134         }
1135
1136         mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
1137 }
1138
1139 static int
1140 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1141                         struct net_device *bridge)
1142 {
1143         struct mt7530_priv *priv = ds->priv;
1144         u32 port_bitmap = BIT(MT7530_CPU_PORT);
1145         int i;
1146
1147         mutex_lock(&priv->reg_mutex);
1148
1149         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1150                 /* Add this port to the port matrix of the other ports in the
1151                  * same bridge. If the port is disabled, port matrix is kept
1152                  * and not being setup until the port becomes enabled.
1153                  */
1154                 if (dsa_is_user_port(ds, i) && i != port) {
1155                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
1156                                 continue;
1157                         if (priv->ports[i].enable)
1158                                 mt7530_set(priv, MT7530_PCR_P(i),
1159                                            PCR_MATRIX(BIT(port)));
1160                         priv->ports[i].pm |= PCR_MATRIX(BIT(port));
1161
1162                         port_bitmap |= BIT(i);
1163                 }
1164         }
1165
1166         /* Add the all other ports to this port matrix. */
1167         if (priv->ports[port].enable)
1168                 mt7530_rmw(priv, MT7530_PCR_P(port),
1169                            PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1170         priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1171
1172         mutex_unlock(&priv->reg_mutex);
1173
1174         return 0;
1175 }
1176
1177 static void
1178 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1179 {
1180         struct mt7530_priv *priv = ds->priv;
1181         bool all_user_ports_removed = true;
1182         int i;
1183
1184         /* When a port is removed from the bridge, the port would be set up
1185          * back to the default as is at initial boot which is a VLAN-unaware
1186          * port.
1187          */
1188         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1189                    MT7530_PORT_MATRIX_MODE);
1190         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1191                    VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1192                    PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1193
1194         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1195                 if (dsa_is_user_port(ds, i) &&
1196                     dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1197                         all_user_ports_removed = false;
1198                         break;
1199                 }
1200         }
1201
1202         /* CPU port also does the same thing until all user ports belonging to
1203          * the CPU port get out of VLAN filtering mode.
1204          */
1205         if (all_user_ports_removed) {
1206                 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
1207                              PCR_MATRIX(dsa_user_ports(priv->ds)));
1208                 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
1209                              | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1210         }
1211 }
1212
1213 static void
1214 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1215 {
1216         struct mt7530_priv *priv = ds->priv;
1217
1218         /* The real fabric path would be decided on the membership in the
1219          * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS
1220          * means potential VLAN can be consisting of certain subset of all
1221          * ports.
1222          */
1223         mt7530_rmw(priv, MT7530_PCR_P(port),
1224                    PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS));
1225
1226         /* Trapped into security mode allows packet forwarding through VLAN
1227          * table lookup. CPU port is set to fallback mode to let untagged
1228          * frames pass through.
1229          */
1230         if (dsa_is_cpu_port(ds, port))
1231                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1232                            MT7530_PORT_FALLBACK_MODE);
1233         else
1234                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1235                            MT7530_PORT_SECURITY_MODE);
1236
1237         /* Set the port as a user port which is to be able to recognize VID
1238          * from incoming packets before fetching entry within the VLAN table.
1239          */
1240         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1241                    VLAN_ATTR(MT7530_VLAN_USER) |
1242                    PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
1243 }
1244
1245 static void
1246 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1247                          struct net_device *bridge)
1248 {
1249         struct mt7530_priv *priv = ds->priv;
1250         int i;
1251
1252         mutex_lock(&priv->reg_mutex);
1253
1254         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1255                 /* Remove this port from the port matrix of the other ports
1256                  * in the same bridge. If the port is disabled, port matrix
1257                  * is kept and not being setup until the port becomes enabled.
1258                  * And the other port's port matrix cannot be broken when the
1259                  * other port is still a VLAN-aware port.
1260                  */
1261                 if (dsa_is_user_port(ds, i) && i != port &&
1262                    !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
1263                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
1264                                 continue;
1265                         if (priv->ports[i].enable)
1266                                 mt7530_clear(priv, MT7530_PCR_P(i),
1267                                              PCR_MATRIX(BIT(port)));
1268                         priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
1269                 }
1270         }
1271
1272         /* Set the cpu port to be the only one in the port matrix of
1273          * this port.
1274          */
1275         if (priv->ports[port].enable)
1276                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1277                            PCR_MATRIX(BIT(MT7530_CPU_PORT)));
1278         priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
1279
1280         mutex_unlock(&priv->reg_mutex);
1281 }
1282
1283 static int
1284 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1285                     const unsigned char *addr, u16 vid)
1286 {
1287         struct mt7530_priv *priv = ds->priv;
1288         int ret;
1289         u8 port_mask = BIT(port);
1290
1291         mutex_lock(&priv->reg_mutex);
1292         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1293         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1294         mutex_unlock(&priv->reg_mutex);
1295
1296         return ret;
1297 }
1298
1299 static int
1300 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1301                     const unsigned char *addr, u16 vid)
1302 {
1303         struct mt7530_priv *priv = ds->priv;
1304         int ret;
1305         u8 port_mask = BIT(port);
1306
1307         mutex_lock(&priv->reg_mutex);
1308         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
1309         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1310         mutex_unlock(&priv->reg_mutex);
1311
1312         return ret;
1313 }
1314
1315 static int
1316 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1317                      dsa_fdb_dump_cb_t *cb, void *data)
1318 {
1319         struct mt7530_priv *priv = ds->priv;
1320         struct mt7530_fdb _fdb = { 0 };
1321         int cnt = MT7530_NUM_FDB_RECORDS;
1322         int ret = 0;
1323         u32 rsp = 0;
1324
1325         mutex_lock(&priv->reg_mutex);
1326
1327         ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1328         if (ret < 0)
1329                 goto err;
1330
1331         do {
1332                 if (rsp & ATC_SRCH_HIT) {
1333                         mt7530_fdb_read(priv, &_fdb);
1334                         if (_fdb.port_mask & BIT(port)) {
1335                                 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1336                                          data);
1337                                 if (ret < 0)
1338                                         break;
1339                         }
1340                 }
1341         } while (--cnt &&
1342                  !(rsp & ATC_SRCH_END) &&
1343                  !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1344 err:
1345         mutex_unlock(&priv->reg_mutex);
1346
1347         return 0;
1348 }
1349
1350 static int
1351 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1352 {
1353         struct mt7530_dummy_poll p;
1354         u32 val;
1355         int ret;
1356
1357         val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1358         mt7530_write(priv, MT7530_VTCR, val);
1359
1360         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1361         ret = readx_poll_timeout(_mt7530_read, &p, val,
1362                                  !(val & VTCR_BUSY), 20, 20000);
1363         if (ret < 0) {
1364                 dev_err(priv->dev, "poll timeout\n");
1365                 return ret;
1366         }
1367
1368         val = mt7530_read(priv, MT7530_VTCR);
1369         if (val & VTCR_INVALID) {
1370                 dev_err(priv->dev, "read VTCR invalid\n");
1371                 return -EINVAL;
1372         }
1373
1374         return 0;
1375 }
1376
1377 static int
1378 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
1379                            bool vlan_filtering,
1380                            struct switchdev_trans *trans)
1381 {
1382         if (switchdev_trans_ph_prepare(trans))
1383                 return 0;
1384
1385         if (vlan_filtering) {
1386                 /* The port is being kept as VLAN-unaware port when bridge is
1387                  * set up with vlan_filtering not being set, Otherwise, the
1388                  * port and the corresponding CPU port is required the setup
1389                  * for becoming a VLAN-aware port.
1390                  */
1391                 mt7530_port_set_vlan_aware(ds, port);
1392                 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
1393         } else {
1394                 mt7530_port_set_vlan_unaware(ds, port);
1395         }
1396
1397         return 0;
1398 }
1399
1400 static int
1401 mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
1402                          const struct switchdev_obj_port_vlan *vlan)
1403 {
1404         /* nothing needed */
1405
1406         return 0;
1407 }
1408
1409 static void
1410 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1411                    struct mt7530_hw_vlan_entry *entry)
1412 {
1413         u8 new_members;
1414         u32 val;
1415
1416         new_members = entry->old_members | BIT(entry->port) |
1417                       BIT(MT7530_CPU_PORT);
1418
1419         /* Validate the entry with independent learning, create egress tag per
1420          * VLAN and joining the port as one of the port members.
1421          */
1422         val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
1423         mt7530_write(priv, MT7530_VAWD1, val);
1424
1425         /* Decide whether adding tag or not for those outgoing packets from the
1426          * port inside the VLAN.
1427          */
1428         val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1429                                 MT7530_VLAN_EGRESS_TAG;
1430         mt7530_rmw(priv, MT7530_VAWD2,
1431                    ETAG_CTRL_P_MASK(entry->port),
1432                    ETAG_CTRL_P(entry->port, val));
1433
1434         /* CPU port is always taken as a tagged port for serving more than one
1435          * VLANs across and also being applied with egress type stack mode for
1436          * that VLAN tags would be appended after hardware special tag used as
1437          * DSA tag.
1438          */
1439         mt7530_rmw(priv, MT7530_VAWD2,
1440                    ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1441                    ETAG_CTRL_P(MT7530_CPU_PORT,
1442                                MT7530_VLAN_EGRESS_STACK));
1443 }
1444
1445 static void
1446 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1447                    struct mt7530_hw_vlan_entry *entry)
1448 {
1449         u8 new_members;
1450         u32 val;
1451
1452         new_members = entry->old_members & ~BIT(entry->port);
1453
1454         val = mt7530_read(priv, MT7530_VAWD1);
1455         if (!(val & VLAN_VALID)) {
1456                 dev_err(priv->dev,
1457                         "Cannot be deleted due to invalid entry\n");
1458                 return;
1459         }
1460
1461         /* If certain member apart from CPU port is still alive in the VLAN,
1462          * the entry would be kept valid. Otherwise, the entry is got to be
1463          * disabled.
1464          */
1465         if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1466                 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1467                       VLAN_VALID;
1468                 mt7530_write(priv, MT7530_VAWD1, val);
1469         } else {
1470                 mt7530_write(priv, MT7530_VAWD1, 0);
1471                 mt7530_write(priv, MT7530_VAWD2, 0);
1472         }
1473 }
1474
1475 static void
1476 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1477                       struct mt7530_hw_vlan_entry *entry,
1478                       mt7530_vlan_op vlan_op)
1479 {
1480         u32 val;
1481
1482         /* Fetch entry */
1483         mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1484
1485         val = mt7530_read(priv, MT7530_VAWD1);
1486
1487         entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1488
1489         /* Manipulate entry */
1490         vlan_op(priv, entry);
1491
1492         /* Flush result to hardware */
1493         mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1494 }
1495
1496 static void
1497 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1498                      const struct switchdev_obj_port_vlan *vlan)
1499 {
1500         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1501         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1502         struct mt7530_hw_vlan_entry new_entry;
1503         struct mt7530_priv *priv = ds->priv;
1504         u16 vid;
1505
1506         mutex_lock(&priv->reg_mutex);
1507
1508         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1509                 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1510                 mt7530_hw_vlan_update(priv, vid, &new_entry,
1511                                       mt7530_hw_vlan_add);
1512         }
1513
1514         if (pvid) {
1515                 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1516                            G0_PORT_VID(vlan->vid_end));
1517                 priv->ports[port].pvid = vlan->vid_end;
1518         }
1519
1520         mutex_unlock(&priv->reg_mutex);
1521 }
1522
1523 static int
1524 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1525                      const struct switchdev_obj_port_vlan *vlan)
1526 {
1527         struct mt7530_hw_vlan_entry target_entry;
1528         struct mt7530_priv *priv = ds->priv;
1529         u16 vid, pvid;
1530
1531         mutex_lock(&priv->reg_mutex);
1532
1533         pvid = priv->ports[port].pvid;
1534         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1535                 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1536                 mt7530_hw_vlan_update(priv, vid, &target_entry,
1537                                       mt7530_hw_vlan_del);
1538
1539                 /* PVID is being restored to the default whenever the PVID port
1540                  * is being removed from the VLAN.
1541                  */
1542                 if (pvid == vid)
1543                         pvid = G0_PORT_VID_DEF;
1544         }
1545
1546         mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1547         priv->ports[port].pvid = pvid;
1548
1549         mutex_unlock(&priv->reg_mutex);
1550
1551         return 0;
1552 }
1553
1554 static int mt753x_mirror_port_get(unsigned int id, u32 val)
1555 {
1556         return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) :
1557                                    MIRROR_PORT(val);
1558 }
1559
1560 static int mt753x_mirror_port_set(unsigned int id, u32 val)
1561 {
1562         return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) :
1563                                    MIRROR_PORT(val);
1564 }
1565
1566 static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
1567                                   struct dsa_mall_mirror_tc_entry *mirror,
1568                                   bool ingress)
1569 {
1570         struct mt7530_priv *priv = ds->priv;
1571         int monitor_port;
1572         u32 val;
1573
1574         /* Check for existent entry */
1575         if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1576                 return -EEXIST;
1577
1578         val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1579
1580         /* MT7530 only supports one monitor port */
1581         monitor_port = mt753x_mirror_port_get(priv->id, val);
1582         if (val & MT753X_MIRROR_EN(priv->id) &&
1583             monitor_port != mirror->to_local_port)
1584                 return -EEXIST;
1585
1586         val |= MT753X_MIRROR_EN(priv->id);
1587         val &= ~MT753X_MIRROR_MASK(priv->id);
1588         val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port);
1589         mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1590
1591         val = mt7530_read(priv, MT7530_PCR_P(port));
1592         if (ingress) {
1593                 val |= PORT_RX_MIR;
1594                 priv->mirror_rx |= BIT(port);
1595         } else {
1596                 val |= PORT_TX_MIR;
1597                 priv->mirror_tx |= BIT(port);
1598         }
1599         mt7530_write(priv, MT7530_PCR_P(port), val);
1600
1601         return 0;
1602 }
1603
1604 static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
1605                                    struct dsa_mall_mirror_tc_entry *mirror)
1606 {
1607         struct mt7530_priv *priv = ds->priv;
1608         u32 val;
1609
1610         val = mt7530_read(priv, MT7530_PCR_P(port));
1611         if (mirror->ingress) {
1612                 val &= ~PORT_RX_MIR;
1613                 priv->mirror_rx &= ~BIT(port);
1614         } else {
1615                 val &= ~PORT_TX_MIR;
1616                 priv->mirror_tx &= ~BIT(port);
1617         }
1618         mt7530_write(priv, MT7530_PCR_P(port), val);
1619
1620         if (!priv->mirror_rx && !priv->mirror_tx) {
1621                 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1622                 val &= ~MT753X_MIRROR_EN(priv->id);
1623                 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
1624         }
1625 }
1626
1627 static enum dsa_tag_protocol
1628 mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1629                      enum dsa_tag_protocol mp)
1630 {
1631         struct mt7530_priv *priv = ds->priv;
1632
1633         if (port != MT7530_CPU_PORT) {
1634                 dev_warn(priv->dev,
1635                          "port not matched with tagging CPU port\n");
1636                 return DSA_TAG_PROTO_NONE;
1637         } else {
1638                 return DSA_TAG_PROTO_MTK;
1639         }
1640 }
1641
1642 static int
1643 mt7530_setup(struct dsa_switch *ds)
1644 {
1645         struct mt7530_priv *priv = ds->priv;
1646         struct device_node *phy_node;
1647         struct device_node *mac_np;
1648         struct mt7530_dummy_poll p;
1649         phy_interface_t interface;
1650         struct device_node *dn;
1651         u32 id, val;
1652         int ret, i;
1653
1654         /* The parent node of master netdev which holds the common system
1655          * controller also is the container for two GMACs nodes representing
1656          * as two netdev instances.
1657          */
1658         dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
1659         ds->configure_vlan_while_not_filtering = true;
1660
1661         if (priv->id == ID_MT7530) {
1662                 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1663                 ret = regulator_enable(priv->core_pwr);
1664                 if (ret < 0) {
1665                         dev_err(priv->dev,
1666                                 "Failed to enable core power: %d\n", ret);
1667                         return ret;
1668                 }
1669
1670                 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1671                 ret = regulator_enable(priv->io_pwr);
1672                 if (ret < 0) {
1673                         dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1674                                 ret);
1675                         return ret;
1676                 }
1677         }
1678
1679         /* Reset whole chip through gpio pin or memory-mapped registers for
1680          * different type of hardware
1681          */
1682         if (priv->mcm) {
1683                 reset_control_assert(priv->rstc);
1684                 usleep_range(1000, 1100);
1685                 reset_control_deassert(priv->rstc);
1686         } else {
1687                 gpiod_set_value_cansleep(priv->reset, 0);
1688                 usleep_range(1000, 1100);
1689                 gpiod_set_value_cansleep(priv->reset, 1);
1690         }
1691
1692         /* Waiting for MT7530 got to stable */
1693         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1694         ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1695                                  20, 1000000);
1696         if (ret < 0) {
1697                 dev_err(priv->dev, "reset timeout\n");
1698                 return ret;
1699         }
1700
1701         id = mt7530_read(priv, MT7530_CREV);
1702         id >>= CHIP_NAME_SHIFT;
1703         if (id != MT7530_ID) {
1704                 dev_err(priv->dev, "chip %x can't be supported\n", id);
1705                 return -ENODEV;
1706         }
1707
1708         /* Reset the switch through internal reset */
1709         mt7530_write(priv, MT7530_SYS_CTRL,
1710                      SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1711                      SYS_CTRL_REG_RST);
1712
1713         /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1714         val = mt7530_read(priv, MT7530_MHWTRAP);
1715         val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1716         val |= MHWTRAP_MANUAL;
1717         mt7530_write(priv, MT7530_MHWTRAP, val);
1718
1719         priv->p6_interface = PHY_INTERFACE_MODE_NA;
1720
1721         /* Enable and reset MIB counters */
1722         mt7530_mib_reset(ds);
1723
1724         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1725                 /* Disable forwarding by default on all ports */
1726                 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1727                            PCR_MATRIX_CLR);
1728
1729                 if (dsa_is_cpu_port(ds, i)) {
1730                         ret = mt753x_cpu_port_enable(ds, i);
1731                         if (ret)
1732                                 return ret;
1733                 } else
1734                         mt7530_port_disable(ds, i);
1735
1736                 /* Enable consistent egress tag */
1737                 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
1738                            PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1739         }
1740
1741         /* Setup port 5 */
1742         priv->p5_intf_sel = P5_DISABLED;
1743         interface = PHY_INTERFACE_MODE_NA;
1744
1745         if (!dsa_is_unused_port(ds, 5)) {
1746                 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
1747                 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
1748                 if (ret && ret != -ENODEV)
1749                         return ret;
1750         } else {
1751                 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
1752                 for_each_child_of_node(dn, mac_np) {
1753                         if (!of_device_is_compatible(mac_np,
1754                                                      "mediatek,eth-mac"))
1755                                 continue;
1756
1757                         ret = of_property_read_u32(mac_np, "reg", &id);
1758                         if (ret < 0 || id != 1)
1759                                 continue;
1760
1761                         phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
1762                         if (!phy_node)
1763                                 continue;
1764
1765                         if (phy_node->parent == priv->dev->of_node->parent) {
1766                                 ret = of_get_phy_mode(mac_np, &interface);
1767                                 if (ret && ret != -ENODEV) {
1768                                         of_node_put(mac_np);
1769                                         return ret;
1770                                 }
1771                                 id = of_mdio_parse_addr(ds->dev, phy_node);
1772                                 if (id == 0)
1773                                         priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
1774                                 if (id == 4)
1775                                         priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
1776                         }
1777                         of_node_put(mac_np);
1778                         of_node_put(phy_node);
1779                         break;
1780                 }
1781         }
1782
1783         mt7530_setup_port5(ds, interface);
1784
1785         /* Flush the FDB table */
1786         ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
1787         if (ret < 0)
1788                 return ret;
1789
1790         return 0;
1791 }
1792
1793 static int
1794 mt7531_setup(struct dsa_switch *ds)
1795 {
1796         struct mt7530_priv *priv = ds->priv;
1797         struct mt7530_dummy_poll p;
1798         u32 val, id;
1799         int ret, i;
1800
1801         /* Reset whole chip through gpio pin or memory-mapped registers for
1802          * different type of hardware
1803          */
1804         if (priv->mcm) {
1805                 reset_control_assert(priv->rstc);
1806                 usleep_range(1000, 1100);
1807                 reset_control_deassert(priv->rstc);
1808         } else {
1809                 gpiod_set_value_cansleep(priv->reset, 0);
1810                 usleep_range(1000, 1100);
1811                 gpiod_set_value_cansleep(priv->reset, 1);
1812         }
1813
1814         /* Waiting for MT7530 got to stable */
1815         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1816         ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1817                                  20, 1000000);
1818         if (ret < 0) {
1819                 dev_err(priv->dev, "reset timeout\n");
1820                 return ret;
1821         }
1822
1823         id = mt7530_read(priv, MT7531_CREV);
1824         id >>= CHIP_NAME_SHIFT;
1825
1826         if (id != MT7531_ID) {
1827                 dev_err(priv->dev, "chip %x can't be supported\n", id);
1828                 return -ENODEV;
1829         }
1830
1831         /* Reset the switch through internal reset */
1832         mt7530_write(priv, MT7530_SYS_CTRL,
1833                      SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1834                      SYS_CTRL_REG_RST);
1835
1836         if (mt7531_dual_sgmii_supported(priv)) {
1837                 priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII;
1838
1839                 /* Let ds->slave_mii_bus be able to access external phy. */
1840                 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
1841                            MT7531_EXT_P_MDC_11);
1842                 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
1843                            MT7531_EXT_P_MDIO_12);
1844         } else {
1845                 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
1846         }
1847         dev_dbg(ds->dev, "P5 support %s interface\n",
1848                 p5_intf_modes(priv->p5_intf_sel));
1849
1850         mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
1851                    MT7531_GPIO0_INTERRUPT);
1852
1853         /* Let phylink decide the interface later. */
1854         priv->p5_interface = PHY_INTERFACE_MODE_NA;
1855         priv->p6_interface = PHY_INTERFACE_MODE_NA;
1856
1857         /* Enable PHY core PLL, since phy_device has not yet been created
1858          * provided for phy_[read,write]_mmd_indirect is called, we provide
1859          * our own mt7531_ind_mmd_phy_[read,write] to complete this
1860          * function.
1861          */
1862         val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR,
1863                                       MDIO_MMD_VEND2, CORE_PLL_GROUP4);
1864         val |= MT7531_PHY_PLL_BYPASS_MODE;
1865         val &= ~MT7531_PHY_PLL_OFF;
1866         mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2,
1867                                  CORE_PLL_GROUP4, val);
1868
1869         /* BPDU to CPU port */
1870         mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK,
1871                    BIT(MT7530_CPU_PORT));
1872         mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK,
1873                    MT753X_BPDU_CPU_ONLY);
1874
1875         /* Enable and reset MIB counters */
1876         mt7530_mib_reset(ds);
1877
1878         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1879                 /* Disable forwarding by default on all ports */
1880                 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1881                            PCR_MATRIX_CLR);
1882
1883                 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
1884
1885                 if (dsa_is_cpu_port(ds, i)) {
1886                         ret = mt753x_cpu_port_enable(ds, i);
1887                         if (ret)
1888                                 return ret;
1889                 } else
1890                         mt7530_port_disable(ds, i);
1891
1892                 /* Enable consistent egress tag */
1893                 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
1894                            PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1895         }
1896
1897         ds->configure_vlan_while_not_filtering = true;
1898
1899         /* Flush the FDB table */
1900         ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
1901         if (ret < 0)
1902                 return ret;
1903
1904         return 0;
1905 }
1906
1907 static bool
1908 mt7530_phy_mode_supported(struct dsa_switch *ds, int port,
1909                           const struct phylink_link_state *state)
1910 {
1911         struct mt7530_priv *priv = ds->priv;
1912
1913         switch (port) {
1914         case 0 ... 4: /* Internal phy */
1915                 if (state->interface != PHY_INTERFACE_MODE_GMII)
1916                         return false;
1917                 break;
1918         case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
1919                 if (!phy_interface_mode_is_rgmii(state->interface) &&
1920                     state->interface != PHY_INTERFACE_MODE_MII &&
1921                     state->interface != PHY_INTERFACE_MODE_GMII)
1922                         return false;
1923                 break;
1924         case 6: /* 1st cpu port */
1925                 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
1926                     state->interface != PHY_INTERFACE_MODE_TRGMII)
1927                         return false;
1928                 break;
1929         default:
1930                 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
1931                         port);
1932                 return false;
1933         }
1934
1935         return true;
1936 }
1937
1938 static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port)
1939 {
1940         return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII);
1941 }
1942
1943 static bool
1944 mt7531_phy_mode_supported(struct dsa_switch *ds, int port,
1945                           const struct phylink_link_state *state)
1946 {
1947         struct mt7530_priv *priv = ds->priv;
1948
1949         switch (port) {
1950         case 0 ... 4: /* Internal phy */
1951                 if (state->interface != PHY_INTERFACE_MODE_GMII)
1952                         return false;
1953                 break;
1954         case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */
1955                 if (mt7531_is_rgmii_port(priv, port))
1956                         return phy_interface_mode_is_rgmii(state->interface);
1957                 fallthrough;
1958         case 6: /* 1st cpu port supports sgmii/8023z only */
1959                 if (state->interface != PHY_INTERFACE_MODE_SGMII &&
1960                     !phy_interface_mode_is_8023z(state->interface))
1961                         return false;
1962                 break;
1963         default:
1964                 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
1965                         port);
1966                 return false;
1967         }
1968
1969         return true;
1970 }
1971
1972 static bool
1973 mt753x_phy_mode_supported(struct dsa_switch *ds, int port,
1974                           const struct phylink_link_state *state)
1975 {
1976         struct mt7530_priv *priv = ds->priv;
1977
1978         return priv->info->phy_mode_supported(ds, port, state);
1979 }
1980
1981 static int
1982 mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
1983 {
1984         struct mt7530_priv *priv = ds->priv;
1985
1986         return priv->info->pad_setup(ds, state->interface);
1987 }
1988
1989 static int
1990 mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
1991                   phy_interface_t interface)
1992 {
1993         struct mt7530_priv *priv = ds->priv;
1994
1995         /* Only need to setup port5. */
1996         if (port != 5)
1997                 return 0;
1998
1999         mt7530_setup_port5(priv->ds, interface);
2000
2001         return 0;
2002 }
2003
2004 static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port,
2005                               phy_interface_t interface,
2006                               struct phy_device *phydev)
2007 {
2008         u32 val;
2009
2010         if (!mt7531_is_rgmii_port(priv, port)) {
2011                 dev_err(priv->dev, "RGMII mode is not available for port %d\n",
2012                         port);
2013                 return -EINVAL;
2014         }
2015
2016         val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2017         val |= GP_CLK_EN;
2018         val &= ~GP_MODE_MASK;
2019         val |= GP_MODE(MT7531_GP_MODE_RGMII);
2020         val &= ~CLK_SKEW_IN_MASK;
2021         val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2022         val &= ~CLK_SKEW_OUT_MASK;
2023         val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2024         val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2025
2026         /* Do not adjust rgmii delay when vendor phy driver presents. */
2027         if (!phydev || phy_driver_is_genphy(phydev)) {
2028                 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2029                 switch (interface) {
2030                 case PHY_INTERFACE_MODE_RGMII:
2031                         val |= TXCLK_NO_REVERSE;
2032                         val |= RXCLK_NO_DELAY;
2033                         break;
2034                 case PHY_INTERFACE_MODE_RGMII_RXID:
2035                         val |= TXCLK_NO_REVERSE;
2036                         break;
2037                 case PHY_INTERFACE_MODE_RGMII_TXID:
2038                         val |= RXCLK_NO_DELAY;
2039                         break;
2040                 case PHY_INTERFACE_MODE_RGMII_ID:
2041                         break;
2042                 default:
2043                         return -EINVAL;
2044                 }
2045         }
2046         mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2047
2048         return 0;
2049 }
2050
2051 static void mt7531_sgmii_validate(struct mt7530_priv *priv, int port,
2052                                   unsigned long *supported)
2053 {
2054         /* Port5 supports ethier RGMII or SGMII.
2055          * Port6 supports SGMII only.
2056          */
2057         switch (port) {
2058         case 5:
2059                 if (mt7531_is_rgmii_port(priv, port))
2060                         break;
2061                 fallthrough;
2062         case 6:
2063                 phylink_set(supported, 1000baseX_Full);
2064                 phylink_set(supported, 2500baseX_Full);
2065                 phylink_set(supported, 2500baseT_Full);
2066         }
2067 }
2068
2069 static void
2070 mt7531_sgmii_link_up_force(struct dsa_switch *ds, int port,
2071                            unsigned int mode, phy_interface_t interface,
2072                            int speed, int duplex)
2073 {
2074         struct mt7530_priv *priv = ds->priv;
2075         unsigned int val;
2076
2077         /* For adjusting speed and duplex of SGMII force mode. */
2078         if (interface != PHY_INTERFACE_MODE_SGMII ||
2079             phylink_autoneg_inband(mode))
2080                 return;
2081
2082         /* SGMII force mode setting */
2083         val = mt7530_read(priv, MT7531_SGMII_MODE(port));
2084         val &= ~MT7531_SGMII_IF_MODE_MASK;
2085
2086         switch (speed) {
2087         case SPEED_10:
2088                 val |= MT7531_SGMII_FORCE_SPEED_10;
2089                 break;
2090         case SPEED_100:
2091                 val |= MT7531_SGMII_FORCE_SPEED_100;
2092                 break;
2093         case SPEED_1000:
2094                 val |= MT7531_SGMII_FORCE_SPEED_1000;
2095                 break;
2096         }
2097
2098         /* MT7531 SGMII 1G force mode can only work in full duplex mode,
2099          * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2100          */
2101         if ((speed == SPEED_10 || speed == SPEED_100) &&
2102             duplex != DUPLEX_FULL)
2103                 val |= MT7531_SGMII_FORCE_HALF_DUPLEX;
2104
2105         mt7530_write(priv, MT7531_SGMII_MODE(port), val);
2106 }
2107
2108 static bool mt753x_is_mac_port(u32 port)
2109 {
2110         return (port == 5 || port == 6);
2111 }
2112
2113 static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port,
2114                                          phy_interface_t interface)
2115 {
2116         u32 val;
2117
2118         if (!mt753x_is_mac_port(port))
2119                 return -EINVAL;
2120
2121         mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2122                    MT7531_SGMII_PHYA_PWD);
2123
2124         val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port));
2125         val &= ~MT7531_RG_TPHY_SPEED_MASK;
2126         /* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B
2127          * encoding.
2128          */
2129         val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ?
2130                 MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G;
2131         mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val);
2132
2133         mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2134
2135         /* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex
2136          * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2137          */
2138         mt7530_rmw(priv, MT7531_SGMII_MODE(port),
2139                    MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS,
2140                    MT7531_SGMII_FORCE_SPEED_1000);
2141
2142         mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2143
2144         return 0;
2145 }
2146
2147 static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port,
2148                                       phy_interface_t interface)
2149 {
2150         if (!mt753x_is_mac_port(port))
2151                 return -EINVAL;
2152
2153         mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2154                    MT7531_SGMII_PHYA_PWD);
2155
2156         mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
2157                    MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G);
2158
2159         mt7530_set(priv, MT7531_SGMII_MODE(port),
2160                    MT7531_SGMII_REMOTE_FAULT_DIS |
2161                    MT7531_SGMII_SPEED_DUPLEX_AN);
2162
2163         mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port),
2164                    MT7531_SGMII_TX_CONFIG_MASK, 1);
2165
2166         mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2167
2168         mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART);
2169
2170         mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2171
2172         return 0;
2173 }
2174
2175 static void mt7531_sgmii_restart_an(struct dsa_switch *ds, int port)
2176 {
2177         struct mt7530_priv *priv = ds->priv;
2178         u32 val;
2179
2180         /* Only restart AN when AN is enabled */
2181         val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2182         if (val & MT7531_SGMII_AN_ENABLE) {
2183                 val |= MT7531_SGMII_AN_RESTART;
2184                 mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val);
2185         }
2186 }
2187
2188 static int
2189 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2190                   phy_interface_t interface)
2191 {
2192         struct mt7530_priv *priv = ds->priv;
2193         struct phy_device *phydev;
2194         struct dsa_port *dp;
2195
2196         if (!mt753x_is_mac_port(port)) {
2197                 dev_err(priv->dev, "port %d is not a MAC port\n", port);
2198                 return -EINVAL;
2199         }
2200
2201         switch (interface) {
2202         case PHY_INTERFACE_MODE_RGMII:
2203         case PHY_INTERFACE_MODE_RGMII_ID:
2204         case PHY_INTERFACE_MODE_RGMII_RXID:
2205         case PHY_INTERFACE_MODE_RGMII_TXID:
2206                 dp = dsa_to_port(ds, port);
2207                 phydev = dp->slave->phydev;
2208                 return mt7531_rgmii_setup(priv, port, interface, phydev);
2209         case PHY_INTERFACE_MODE_SGMII:
2210                 return mt7531_sgmii_setup_mode_an(priv, port, interface);
2211         case PHY_INTERFACE_MODE_NA:
2212         case PHY_INTERFACE_MODE_1000BASEX:
2213         case PHY_INTERFACE_MODE_2500BASEX:
2214                 if (phylink_autoneg_inband(mode))
2215                         return -EINVAL;
2216
2217                 return mt7531_sgmii_setup_mode_force(priv, port, interface);
2218         default:
2219                 return -EINVAL;
2220         }
2221
2222         return -EINVAL;
2223 }
2224
2225 static int
2226 mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2227                   const struct phylink_link_state *state)
2228 {
2229         struct mt7530_priv *priv = ds->priv;
2230
2231         return priv->info->mac_port_config(ds, port, mode, state->interface);
2232 }
2233
2234 static void
2235 mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2236                           const struct phylink_link_state *state)
2237 {
2238         struct mt7530_priv *priv = ds->priv;
2239         u32 mcr_cur, mcr_new;
2240
2241         if (!mt753x_phy_mode_supported(ds, port, state))
2242                 goto unsupported;
2243
2244         switch (port) {
2245         case 0 ... 4: /* Internal phy */
2246                 if (state->interface != PHY_INTERFACE_MODE_GMII)
2247                         goto unsupported;
2248                 break;
2249         case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2250                 if (priv->p5_interface == state->interface)
2251                         break;
2252
2253                 if (mt753x_mac_config(ds, port, mode, state) < 0)
2254                         goto unsupported;
2255
2256                 if (priv->p5_intf_sel != P5_DISABLED)
2257                         priv->p5_interface = state->interface;
2258                 break;
2259         case 6: /* 1st cpu port */
2260                 if (priv->p6_interface == state->interface)
2261                         break;
2262
2263                 mt753x_pad_setup(ds, state);
2264
2265                 if (mt753x_mac_config(ds, port, mode, state) < 0)
2266                         goto unsupported;
2267
2268                 priv->p6_interface = state->interface;
2269                 break;
2270         default:
2271 unsupported:
2272                 dev_err(ds->dev, "%s: unsupported %s port: %i\n",
2273                         __func__, phy_modes(state->interface), port);
2274                 return;
2275         }
2276
2277         if (phylink_autoneg_inband(mode) &&
2278             state->interface != PHY_INTERFACE_MODE_SGMII) {
2279                 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
2280                         __func__);
2281                 return;
2282         }
2283
2284         mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
2285         mcr_new = mcr_cur;
2286         mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
2287         mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
2288                    PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id);
2289
2290         /* Are we connected to external phy */
2291         if (port == 5 && dsa_is_user_port(ds, 5))
2292                 mcr_new |= PMCR_EXT_PHY;
2293
2294         if (mcr_new != mcr_cur)
2295                 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
2296 }
2297
2298 static void
2299 mt753x_phylink_mac_an_restart(struct dsa_switch *ds, int port)
2300 {
2301         struct mt7530_priv *priv = ds->priv;
2302
2303         if (!priv->info->mac_pcs_an_restart)
2304                 return;
2305
2306         priv->info->mac_pcs_an_restart(ds, port);
2307 }
2308
2309 static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port,
2310                                          unsigned int mode,
2311                                          phy_interface_t interface)
2312 {
2313         struct mt7530_priv *priv = ds->priv;
2314
2315         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
2316 }
2317
2318 static void mt753x_mac_pcs_link_up(struct dsa_switch *ds, int port,
2319                                    unsigned int mode, phy_interface_t interface,
2320                                    int speed, int duplex)
2321 {
2322         struct mt7530_priv *priv = ds->priv;
2323
2324         if (!priv->info->mac_pcs_link_up)
2325                 return;
2326
2327         priv->info->mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2328 }
2329
2330 static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port,
2331                                        unsigned int mode,
2332                                        phy_interface_t interface,
2333                                        struct phy_device *phydev,
2334                                        int speed, int duplex,
2335                                        bool tx_pause, bool rx_pause)
2336 {
2337         struct mt7530_priv *priv = ds->priv;
2338         u32 mcr;
2339
2340         mt753x_mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2341
2342         mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
2343
2344         /* MT753x MAC works in 1G full duplex mode for all up-clocked
2345          * variants.
2346          */
2347         if (interface == PHY_INTERFACE_MODE_TRGMII ||
2348             (phy_interface_mode_is_8023z(interface))) {
2349                 speed = SPEED_1000;
2350                 duplex = DUPLEX_FULL;
2351         }
2352
2353         switch (speed) {
2354         case SPEED_1000:
2355                 mcr |= PMCR_FORCE_SPEED_1000;
2356                 break;
2357         case SPEED_100:
2358                 mcr |= PMCR_FORCE_SPEED_100;
2359                 break;
2360         }
2361         if (duplex == DUPLEX_FULL) {
2362                 mcr |= PMCR_FORCE_FDX;
2363                 if (tx_pause)
2364                         mcr |= PMCR_TX_FC_EN;
2365                 if (rx_pause)
2366                         mcr |= PMCR_RX_FC_EN;
2367         }
2368
2369         mt7530_set(priv, MT7530_PMCR_P(port), mcr);
2370 }
2371
2372 static int
2373 mt7531_cpu_port_config(struct dsa_switch *ds, int port)
2374 {
2375         struct mt7530_priv *priv = ds->priv;
2376         phy_interface_t interface;
2377         int speed;
2378         int ret;
2379
2380         switch (port) {
2381         case 5:
2382                 if (mt7531_is_rgmii_port(priv, port))
2383                         interface = PHY_INTERFACE_MODE_RGMII;
2384                 else
2385                         interface = PHY_INTERFACE_MODE_2500BASEX;
2386
2387                 priv->p5_interface = interface;
2388                 break;
2389         case 6:
2390                 interface = PHY_INTERFACE_MODE_2500BASEX;
2391
2392                 mt7531_pad_setup(ds, interface);
2393
2394                 priv->p6_interface = interface;
2395                 break;
2396         default:
2397                 return -EINVAL;
2398         }
2399
2400         if (interface == PHY_INTERFACE_MODE_2500BASEX)
2401                 speed = SPEED_2500;
2402         else
2403                 speed = SPEED_1000;
2404
2405         ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
2406         if (ret)
2407                 return ret;
2408         mt7530_write(priv, MT7530_PMCR_P(port),
2409                      PMCR_CPU_PORT_SETTING(priv->id));
2410         mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2411                                    speed, DUPLEX_FULL, true, true);
2412
2413         return 0;
2414 }
2415
2416 static void
2417 mt7530_mac_port_validate(struct dsa_switch *ds, int port,
2418                          unsigned long *supported)
2419 {
2420         if (port == 5)
2421                 phylink_set(supported, 1000baseX_Full);
2422 }
2423
2424 static void mt7531_mac_port_validate(struct dsa_switch *ds, int port,
2425                                      unsigned long *supported)
2426 {
2427         struct mt7530_priv *priv = ds->priv;
2428
2429         mt7531_sgmii_validate(priv, port, supported);
2430 }
2431
2432 static void
2433 mt753x_phylink_validate(struct dsa_switch *ds, int port,
2434                         unsigned long *supported,
2435                         struct phylink_link_state *state)
2436 {
2437         __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
2438         struct mt7530_priv *priv = ds->priv;
2439
2440         if (state->interface != PHY_INTERFACE_MODE_NA &&
2441             !mt753x_phy_mode_supported(ds, port, state)) {
2442                 linkmode_zero(supported);
2443                 return;
2444         }
2445
2446         phylink_set_port_modes(mask);
2447
2448         if (state->interface != PHY_INTERFACE_MODE_TRGMII ||
2449             !phy_interface_mode_is_8023z(state->interface)) {
2450                 phylink_set(mask, 10baseT_Half);
2451                 phylink_set(mask, 10baseT_Full);
2452                 phylink_set(mask, 100baseT_Half);
2453                 phylink_set(mask, 100baseT_Full);
2454                 phylink_set(mask, Autoneg);
2455         }
2456
2457         /* This switch only supports 1G full-duplex. */
2458         if (state->interface != PHY_INTERFACE_MODE_MII)
2459                 phylink_set(mask, 1000baseT_Full);
2460
2461         priv->info->mac_port_validate(ds, port, mask);
2462
2463         phylink_set(mask, Pause);
2464         phylink_set(mask, Asym_Pause);
2465
2466         linkmode_and(supported, supported, mask);
2467         linkmode_and(state->advertising, state->advertising, mask);
2468
2469         /* We can only operate at 2500BaseX or 1000BaseX.  If requested
2470          * to advertise both, only report advertising at 2500BaseX.
2471          */
2472         phylink_helper_basex_speed(state);
2473 }
2474
2475 static int
2476 mt7530_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         u32 pmsr;
2481
2482         if (port < 0 || port >= MT7530_NUM_PORTS)
2483                 return -EINVAL;
2484
2485         pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2486
2487         state->link = (pmsr & PMSR_LINK);
2488         state->an_complete = state->link;
2489         state->duplex = !!(pmsr & PMSR_DPX);
2490
2491         switch (pmsr & PMSR_SPEED_MASK) {
2492         case PMSR_SPEED_10:
2493                 state->speed = SPEED_10;
2494                 break;
2495         case PMSR_SPEED_100:
2496                 state->speed = SPEED_100;
2497                 break;
2498         case PMSR_SPEED_1000:
2499                 state->speed = SPEED_1000;
2500                 break;
2501         default:
2502                 state->speed = SPEED_UNKNOWN;
2503                 break;
2504         }
2505
2506         state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2507         if (pmsr & PMSR_RX_FC)
2508                 state->pause |= MLO_PAUSE_RX;
2509         if (pmsr & PMSR_TX_FC)
2510                 state->pause |= MLO_PAUSE_TX;
2511
2512         return 1;
2513 }
2514
2515 static int
2516 mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port,
2517                               struct phylink_link_state *state)
2518 {
2519         u32 status, val;
2520         u16 config_reg;
2521
2522         status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2523         state->link = !!(status & MT7531_SGMII_LINK_STATUS);
2524         if (state->interface == PHY_INTERFACE_MODE_SGMII &&
2525             (status & MT7531_SGMII_AN_ENABLE)) {
2526                 val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port));
2527                 config_reg = val >> 16;
2528
2529                 switch (config_reg & LPA_SGMII_SPD_MASK) {
2530                 case LPA_SGMII_1000:
2531                         state->speed = SPEED_1000;
2532                         break;
2533                 case LPA_SGMII_100:
2534                         state->speed = SPEED_100;
2535                         break;
2536                 case LPA_SGMII_10:
2537                         state->speed = SPEED_10;
2538                         break;
2539                 default:
2540                         dev_err(priv->dev, "invalid sgmii PHY speed\n");
2541                         state->link = false;
2542                         return -EINVAL;
2543                 }
2544
2545                 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2546                         state->duplex = DUPLEX_FULL;
2547                 else
2548                         state->duplex = DUPLEX_HALF;
2549         }
2550
2551         return 0;
2552 }
2553
2554 static int
2555 mt7531_phylink_mac_link_state(struct dsa_switch *ds, int port,
2556                               struct phylink_link_state *state)
2557 {
2558         struct mt7530_priv *priv = ds->priv;
2559
2560         if (state->interface == PHY_INTERFACE_MODE_SGMII)
2561                 return mt7531_sgmii_pcs_get_state_an(priv, port, state);
2562
2563         return -EOPNOTSUPP;
2564 }
2565
2566 static int
2567 mt753x_phylink_mac_link_state(struct dsa_switch *ds, int port,
2568                               struct phylink_link_state *state)
2569 {
2570         struct mt7530_priv *priv = ds->priv;
2571
2572         return priv->info->mac_port_get_state(ds, port, state);
2573 }
2574
2575 static int
2576 mt753x_setup(struct dsa_switch *ds)
2577 {
2578         struct mt7530_priv *priv = ds->priv;
2579
2580         return priv->info->sw_setup(ds);
2581 }
2582
2583 static int
2584 mt753x_phy_read(struct dsa_switch *ds, int port, int regnum)
2585 {
2586         struct mt7530_priv *priv = ds->priv;
2587
2588         return priv->info->phy_read(ds, port, regnum);
2589 }
2590
2591 static int
2592 mt753x_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2593 {
2594         struct mt7530_priv *priv = ds->priv;
2595
2596         return priv->info->phy_write(ds, port, regnum, val);
2597 }
2598
2599 static const struct dsa_switch_ops mt7530_switch_ops = {
2600         .get_tag_protocol       = mtk_get_tag_protocol,
2601         .setup                  = mt753x_setup,
2602         .get_strings            = mt7530_get_strings,
2603         .phy_read               = mt753x_phy_read,
2604         .phy_write              = mt753x_phy_write,
2605         .get_ethtool_stats      = mt7530_get_ethtool_stats,
2606         .get_sset_count         = mt7530_get_sset_count,
2607         .set_ageing_time        = mt7530_set_ageing_time,
2608         .port_enable            = mt7530_port_enable,
2609         .port_disable           = mt7530_port_disable,
2610         .port_change_mtu        = mt7530_port_change_mtu,
2611         .port_max_mtu           = mt7530_port_max_mtu,
2612         .port_stp_state_set     = mt7530_stp_state_set,
2613         .port_bridge_join       = mt7530_port_bridge_join,
2614         .port_bridge_leave      = mt7530_port_bridge_leave,
2615         .port_fdb_add           = mt7530_port_fdb_add,
2616         .port_fdb_del           = mt7530_port_fdb_del,
2617         .port_fdb_dump          = mt7530_port_fdb_dump,
2618         .port_vlan_filtering    = mt7530_port_vlan_filtering,
2619         .port_vlan_prepare      = mt7530_port_vlan_prepare,
2620         .port_vlan_add          = mt7530_port_vlan_add,
2621         .port_vlan_del          = mt7530_port_vlan_del,
2622         .port_mirror_add        = mt753x_port_mirror_add,
2623         .port_mirror_del        = mt753x_port_mirror_del,
2624         .phylink_validate       = mt753x_phylink_validate,
2625         .phylink_mac_link_state = mt753x_phylink_mac_link_state,
2626         .phylink_mac_config     = mt753x_phylink_mac_config,
2627         .phylink_mac_an_restart = mt753x_phylink_mac_an_restart,
2628         .phylink_mac_link_down  = mt753x_phylink_mac_link_down,
2629         .phylink_mac_link_up    = mt753x_phylink_mac_link_up,
2630 };
2631
2632 static const struct mt753x_info mt753x_table[] = {
2633         [ID_MT7621] = {
2634                 .id = ID_MT7621,
2635                 .sw_setup = mt7530_setup,
2636                 .phy_read = mt7530_phy_read,
2637                 .phy_write = mt7530_phy_write,
2638                 .pad_setup = mt7530_pad_clk_setup,
2639                 .phy_mode_supported = mt7530_phy_mode_supported,
2640                 .mac_port_validate = mt7530_mac_port_validate,
2641                 .mac_port_get_state = mt7530_phylink_mac_link_state,
2642                 .mac_port_config = mt7530_mac_config,
2643         },
2644         [ID_MT7530] = {
2645                 .id = ID_MT7530,
2646                 .sw_setup = mt7530_setup,
2647                 .phy_read = mt7530_phy_read,
2648                 .phy_write = mt7530_phy_write,
2649                 .pad_setup = mt7530_pad_clk_setup,
2650                 .phy_mode_supported = mt7530_phy_mode_supported,
2651                 .mac_port_validate = mt7530_mac_port_validate,
2652                 .mac_port_get_state = mt7530_phylink_mac_link_state,
2653                 .mac_port_config = mt7530_mac_config,
2654         },
2655         [ID_MT7531] = {
2656                 .id = ID_MT7531,
2657                 .sw_setup = mt7531_setup,
2658                 .phy_read = mt7531_ind_phy_read,
2659                 .phy_write = mt7531_ind_phy_write,
2660                 .pad_setup = mt7531_pad_setup,
2661                 .cpu_port_config = mt7531_cpu_port_config,
2662                 .phy_mode_supported = mt7531_phy_mode_supported,
2663                 .mac_port_validate = mt7531_mac_port_validate,
2664                 .mac_port_get_state = mt7531_phylink_mac_link_state,
2665                 .mac_port_config = mt7531_mac_config,
2666                 .mac_pcs_an_restart = mt7531_sgmii_restart_an,
2667                 .mac_pcs_link_up = mt7531_sgmii_link_up_force,
2668         },
2669 };
2670
2671 static const struct of_device_id mt7530_of_match[] = {
2672         { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
2673         { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
2674         { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
2675         { /* sentinel */ },
2676 };
2677 MODULE_DEVICE_TABLE(of, mt7530_of_match);
2678
2679 static int
2680 mt7530_probe(struct mdio_device *mdiodev)
2681 {
2682         struct mt7530_priv *priv;
2683         struct device_node *dn;
2684
2685         dn = mdiodev->dev.of_node;
2686
2687         priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
2688         if (!priv)
2689                 return -ENOMEM;
2690
2691         priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
2692         if (!priv->ds)
2693                 return -ENOMEM;
2694
2695         priv->ds->dev = &mdiodev->dev;
2696         priv->ds->num_ports = DSA_MAX_PORTS;
2697
2698         /* Use medatek,mcm property to distinguish hardware type that would
2699          * casues a little bit differences on power-on sequence.
2700          */
2701         priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
2702         if (priv->mcm) {
2703                 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
2704
2705                 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
2706                 if (IS_ERR(priv->rstc)) {
2707                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
2708                         return PTR_ERR(priv->rstc);
2709                 }
2710         }
2711
2712         /* Get the hardware identifier from the devicetree node.
2713          * We will need it for some of the clock and regulator setup.
2714          */
2715         priv->info = of_device_get_match_data(&mdiodev->dev);
2716         if (!priv->info)
2717                 return -EINVAL;
2718
2719         /* Sanity check if these required device operations are filled
2720          * properly.
2721          */
2722         if (!priv->info->sw_setup || !priv->info->pad_setup ||
2723             !priv->info->phy_read || !priv->info->phy_write ||
2724             !priv->info->phy_mode_supported ||
2725             !priv->info->mac_port_validate ||
2726             !priv->info->mac_port_get_state || !priv->info->mac_port_config)
2727                 return -EINVAL;
2728
2729         priv->id = priv->info->id;
2730
2731         if (priv->id == ID_MT7530) {
2732                 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
2733                 if (IS_ERR(priv->core_pwr))
2734                         return PTR_ERR(priv->core_pwr);
2735
2736                 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
2737                 if (IS_ERR(priv->io_pwr))
2738                         return PTR_ERR(priv->io_pwr);
2739         }
2740
2741         /* Not MCM that indicates switch works as the remote standalone
2742          * integrated circuit so the GPIO pin would be used to complete
2743          * the reset, otherwise memory-mapped register accessing used
2744          * through syscon provides in the case of MCM.
2745          */
2746         if (!priv->mcm) {
2747                 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
2748                                                       GPIOD_OUT_LOW);
2749                 if (IS_ERR(priv->reset)) {
2750                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
2751                         return PTR_ERR(priv->reset);
2752                 }
2753         }
2754
2755         priv->bus = mdiodev->bus;
2756         priv->dev = &mdiodev->dev;
2757         priv->ds->priv = priv;
2758         priv->ds->ops = &mt7530_switch_ops;
2759         mutex_init(&priv->reg_mutex);
2760         dev_set_drvdata(&mdiodev->dev, priv);
2761
2762         return dsa_register_switch(priv->ds);
2763 }
2764
2765 static void
2766 mt7530_remove(struct mdio_device *mdiodev)
2767 {
2768         struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
2769         int ret = 0;
2770
2771         ret = regulator_disable(priv->core_pwr);
2772         if (ret < 0)
2773                 dev_err(priv->dev,
2774                         "Failed to disable core power: %d\n", ret);
2775
2776         ret = regulator_disable(priv->io_pwr);
2777         if (ret < 0)
2778                 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
2779                         ret);
2780
2781         dsa_unregister_switch(priv->ds);
2782         mutex_destroy(&priv->reg_mutex);
2783 }
2784
2785 static struct mdio_driver mt7530_mdio_driver = {
2786         .probe  = mt7530_probe,
2787         .remove = mt7530_remove,
2788         .mdiodrv.driver = {
2789                 .name = "mt7530",
2790                 .of_match_table = mt7530_of_match,
2791         },
2792 };
2793
2794 mdio_module_driver(mt7530_mdio_driver);
2795
2796 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
2797 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
2798 MODULE_LICENSE("GPL");