GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / dsa / mt7530.c
1 /*
2  * Mediatek MT7530 DSA Switch driver
3  * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <linux/etherdevice.h>
15 #include <linux/if_bridge.h>
16 #include <linux/iopoll.h>
17 #include <linux/mdio.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_mdio.h>
23 #include <linux/of_net.h>
24 #include <linux/of_platform.h>
25 #include <linux/phy.h>
26 #include <linux/regmap.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/reset.h>
29 #include <linux/gpio/consumer.h>
30 #include <net/dsa.h>
31
32 #include "mt7530.h"
33
34 /* String, offset, and register size in bytes if different from 4 bytes */
35 static const struct mt7530_mib_desc mt7530_mib[] = {
36         MIB_DESC(1, 0x00, "TxDrop"),
37         MIB_DESC(1, 0x04, "TxCrcErr"),
38         MIB_DESC(1, 0x08, "TxUnicast"),
39         MIB_DESC(1, 0x0c, "TxMulticast"),
40         MIB_DESC(1, 0x10, "TxBroadcast"),
41         MIB_DESC(1, 0x14, "TxCollision"),
42         MIB_DESC(1, 0x18, "TxSingleCollision"),
43         MIB_DESC(1, 0x1c, "TxMultipleCollision"),
44         MIB_DESC(1, 0x20, "TxDeferred"),
45         MIB_DESC(1, 0x24, "TxLateCollision"),
46         MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
47         MIB_DESC(1, 0x2c, "TxPause"),
48         MIB_DESC(1, 0x30, "TxPktSz64"),
49         MIB_DESC(1, 0x34, "TxPktSz65To127"),
50         MIB_DESC(1, 0x38, "TxPktSz128To255"),
51         MIB_DESC(1, 0x3c, "TxPktSz256To511"),
52         MIB_DESC(1, 0x40, "TxPktSz512To1023"),
53         MIB_DESC(1, 0x44, "Tx1024ToMax"),
54         MIB_DESC(2, 0x48, "TxBytes"),
55         MIB_DESC(1, 0x60, "RxDrop"),
56         MIB_DESC(1, 0x64, "RxFiltering"),
57         MIB_DESC(1, 0x68, "RxUnicast"),
58         MIB_DESC(1, 0x6c, "RxMulticast"),
59         MIB_DESC(1, 0x70, "RxBroadcast"),
60         MIB_DESC(1, 0x74, "RxAlignErr"),
61         MIB_DESC(1, 0x78, "RxCrcErr"),
62         MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
63         MIB_DESC(1, 0x80, "RxFragErr"),
64         MIB_DESC(1, 0x84, "RxOverSzErr"),
65         MIB_DESC(1, 0x88, "RxJabberErr"),
66         MIB_DESC(1, 0x8c, "RxPause"),
67         MIB_DESC(1, 0x90, "RxPktSz64"),
68         MIB_DESC(1, 0x94, "RxPktSz65To127"),
69         MIB_DESC(1, 0x98, "RxPktSz128To255"),
70         MIB_DESC(1, 0x9c, "RxPktSz256To511"),
71         MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
72         MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
73         MIB_DESC(2, 0xa8, "RxBytes"),
74         MIB_DESC(1, 0xb0, "RxCtrlDrop"),
75         MIB_DESC(1, 0xb4, "RxIngressDrop"),
76         MIB_DESC(1, 0xb8, "RxArlDrop"),
77 };
78
79 static int
80 mt7623_trgmii_write(struct mt7530_priv *priv,  u32 reg, u32 val)
81 {
82         int ret;
83
84         ret =  regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
85         if (ret < 0)
86                 dev_err(priv->dev,
87                         "failed to priv write register\n");
88         return ret;
89 }
90
91 static u32
92 mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
93 {
94         int ret;
95         u32 val;
96
97         ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
98         if (ret < 0) {
99                 dev_err(priv->dev,
100                         "failed to priv read register\n");
101                 return ret;
102         }
103
104         return val;
105 }
106
107 static void
108 mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
109                   u32 mask, u32 set)
110 {
111         u32 val;
112
113         val = mt7623_trgmii_read(priv, reg);
114         val &= ~mask;
115         val |= set;
116         mt7623_trgmii_write(priv, reg, val);
117 }
118
119 static void
120 mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
121 {
122         mt7623_trgmii_rmw(priv, reg, 0, val);
123 }
124
125 static void
126 mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
127 {
128         mt7623_trgmii_rmw(priv, reg, val, 0);
129 }
130
131 static int
132 core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
133 {
134         struct mii_bus *bus = priv->bus;
135         int value, ret;
136
137         /* Write the desired MMD Devad */
138         ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
139         if (ret < 0)
140                 goto err;
141
142         /* Write the desired MMD register address */
143         ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
144         if (ret < 0)
145                 goto err;
146
147         /* Select the Function : DATA with no post increment */
148         ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
149         if (ret < 0)
150                 goto err;
151
152         /* Read the content of the MMD's selected register */
153         value = bus->read(bus, 0, MII_MMD_DATA);
154
155         return value;
156 err:
157         dev_err(&bus->dev,  "failed to read mmd register\n");
158
159         return ret;
160 }
161
162 static int
163 core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
164                         int devad, u32 data)
165 {
166         struct mii_bus *bus = priv->bus;
167         int ret;
168
169         /* Write the desired MMD Devad */
170         ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
171         if (ret < 0)
172                 goto err;
173
174         /* Write the desired MMD register address */
175         ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
176         if (ret < 0)
177                 goto err;
178
179         /* Select the Function : DATA with no post increment */
180         ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
181         if (ret < 0)
182                 goto err;
183
184         /* Write the data into MMD's selected register */
185         ret = bus->write(bus, 0, MII_MMD_DATA, data);
186 err:
187         if (ret < 0)
188                 dev_err(&bus->dev,
189                         "failed to write mmd register\n");
190         return ret;
191 }
192
193 static void
194 core_write(struct mt7530_priv *priv, u32 reg, u32 val)
195 {
196         struct mii_bus *bus = priv->bus;
197
198         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
199
200         core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
201
202         mutex_unlock(&bus->mdio_lock);
203 }
204
205 static void
206 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
207 {
208         struct mii_bus *bus = priv->bus;
209         u32 val;
210
211         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
212
213         val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
214         val &= ~mask;
215         val |= set;
216         core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
217
218         mutex_unlock(&bus->mdio_lock);
219 }
220
221 static void
222 core_set(struct mt7530_priv *priv, u32 reg, u32 val)
223 {
224         core_rmw(priv, reg, 0, val);
225 }
226
227 static void
228 core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
229 {
230         core_rmw(priv, reg, val, 0);
231 }
232
233 static int
234 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
235 {
236         struct mii_bus *bus = priv->bus;
237         u16 page, r, lo, hi;
238         int ret;
239
240         page = (reg >> 6) & 0x3ff;
241         r  = (reg >> 2) & 0xf;
242         lo = val & 0xffff;
243         hi = val >> 16;
244
245         /* MT7530 uses 31 as the pseudo port */
246         ret = bus->write(bus, 0x1f, 0x1f, page);
247         if (ret < 0)
248                 goto err;
249
250         ret = bus->write(bus, 0x1f, r,  lo);
251         if (ret < 0)
252                 goto err;
253
254         ret = bus->write(bus, 0x1f, 0x10, hi);
255 err:
256         if (ret < 0)
257                 dev_err(&bus->dev,
258                         "failed to write mt7530 register\n");
259         return ret;
260 }
261
262 static u32
263 mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
264 {
265         struct mii_bus *bus = priv->bus;
266         u16 page, r, lo, hi;
267         int ret;
268
269         page = (reg >> 6) & 0x3ff;
270         r = (reg >> 2) & 0xf;
271
272         /* MT7530 uses 31 as the pseudo port */
273         ret = bus->write(bus, 0x1f, 0x1f, page);
274         if (ret < 0) {
275                 dev_err(&bus->dev,
276                         "failed to read mt7530 register\n");
277                 return ret;
278         }
279
280         lo = bus->read(bus, 0x1f, r);
281         hi = bus->read(bus, 0x1f, 0x10);
282
283         return (hi << 16) | (lo & 0xffff);
284 }
285
286 static void
287 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
288 {
289         struct mii_bus *bus = priv->bus;
290
291         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
292
293         mt7530_mii_write(priv, reg, val);
294
295         mutex_unlock(&bus->mdio_lock);
296 }
297
298 static u32
299 _mt7530_read(struct mt7530_dummy_poll *p)
300 {
301         struct mii_bus          *bus = p->priv->bus;
302         u32 val;
303
304         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
305
306         val = mt7530_mii_read(p->priv, p->reg);
307
308         mutex_unlock(&bus->mdio_lock);
309
310         return val;
311 }
312
313 static u32
314 mt7530_read(struct mt7530_priv *priv, u32 reg)
315 {
316         struct mt7530_dummy_poll p;
317
318         INIT_MT7530_DUMMY_POLL(&p, priv, reg);
319         return _mt7530_read(&p);
320 }
321
322 static void
323 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
324            u32 mask, u32 set)
325 {
326         struct mii_bus *bus = priv->bus;
327         u32 val;
328
329         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
330
331         val = mt7530_mii_read(priv, reg);
332         val &= ~mask;
333         val |= set;
334         mt7530_mii_write(priv, reg, val);
335
336         mutex_unlock(&bus->mdio_lock);
337 }
338
339 static void
340 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
341 {
342         mt7530_rmw(priv, reg, 0, val);
343 }
344
345 static void
346 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
347 {
348         mt7530_rmw(priv, reg, val, 0);
349 }
350
351 static int
352 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
353 {
354         u32 val;
355         int ret;
356         struct mt7530_dummy_poll p;
357
358         /* Set the command operating upon the MAC address entries */
359         val = ATC_BUSY | ATC_MAT(0) | cmd;
360         mt7530_write(priv, MT7530_ATC, val);
361
362         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
363         ret = readx_poll_timeout(_mt7530_read, &p, val,
364                                  !(val & ATC_BUSY), 20, 20000);
365         if (ret < 0) {
366                 dev_err(priv->dev, "reset timeout\n");
367                 return ret;
368         }
369
370         /* Additional sanity for read command if the specified
371          * entry is invalid
372          */
373         val = mt7530_read(priv, MT7530_ATC);
374         if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
375                 return -EINVAL;
376
377         if (rsp)
378                 *rsp = val;
379
380         return 0;
381 }
382
383 static void
384 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
385 {
386         u32 reg[3];
387         int i;
388
389         /* Read from ARL table into an array */
390         for (i = 0; i < 3; i++) {
391                 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
392
393                 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
394                         __func__, __LINE__, i, reg[i]);
395         }
396
397         fdb->vid = (reg[1] >> CVID) & CVID_MASK;
398         fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
399         fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
400         fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
401         fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
402         fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
403         fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
404         fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
405         fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
406         fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
407 }
408
409 static void
410 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
411                  u8 port_mask, const u8 *mac,
412                  u8 aging, u8 type)
413 {
414         u32 reg[3] = { 0 };
415         int i;
416
417         reg[1] |= vid & CVID_MASK;
418         reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
419         reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
420         /* STATIC_ENT indicate that entry is static wouldn't
421          * be aged out and STATIC_EMP specified as erasing an
422          * entry
423          */
424         reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
425         reg[1] |= mac[5] << MAC_BYTE_5;
426         reg[1] |= mac[4] << MAC_BYTE_4;
427         reg[0] |= mac[3] << MAC_BYTE_3;
428         reg[0] |= mac[2] << MAC_BYTE_2;
429         reg[0] |= mac[1] << MAC_BYTE_1;
430         reg[0] |= mac[0] << MAC_BYTE_0;
431
432         /* Write array into the ARL table */
433         for (i = 0; i < 3; i++)
434                 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
435 }
436
437 static int
438 mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
439 {
440         struct mt7530_priv *priv = ds->priv;
441         u32 ncpo1, ssc_delta, trgint, i;
442
443         switch (mode) {
444         case PHY_INTERFACE_MODE_RGMII:
445                 trgint = 0;
446                 ncpo1 = 0x0c80;
447                 ssc_delta = 0x87;
448                 break;
449         case PHY_INTERFACE_MODE_TRGMII:
450                 trgint = 1;
451                 ncpo1 = 0x1400;
452                 ssc_delta = 0x57;
453                 break;
454         default:
455                 dev_err(priv->dev, "xMII mode %d not supported\n", mode);
456                 return -EINVAL;
457         }
458
459         mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
460                    P6_INTF_MODE(trgint));
461
462         /* Lower Tx Driving for TRGMII path */
463         for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
464                 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
465                              TD_DM_DRVP(8) | TD_DM_DRVN(8));
466
467         /* Setup core clock for MT7530 */
468         if (!trgint) {
469                 /* Disable MT7530 core clock */
470                 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
471
472                 /* Disable PLL, since phy_device has not yet been created
473                  * provided for phy_[read,write]_mmd_indirect is called, we
474                  * provide our own core_write_mmd_indirect to complete this
475                  * function.
476                  */
477                 core_write_mmd_indirect(priv,
478                                         CORE_GSWPLL_GRP1,
479                                         MDIO_MMD_VEND2,
480                                         0);
481
482                 /* Set core clock into 500Mhz */
483                 core_write(priv, CORE_GSWPLL_GRP2,
484                            RG_GSWPLL_POSDIV_500M(1) |
485                            RG_GSWPLL_FBKDIV_500M(25));
486
487                 /* Enable PLL */
488                 core_write(priv, CORE_GSWPLL_GRP1,
489                            RG_GSWPLL_EN_PRE |
490                            RG_GSWPLL_POSDIV_200M(2) |
491                            RG_GSWPLL_FBKDIV_200M(32));
492
493                 /* Enable MT7530 core clock */
494                 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
495         }
496
497         /* Setup the MT7530 TRGMII Tx Clock */
498         core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
499         core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
500         core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
501         core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
502         core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
503         core_write(priv, CORE_PLL_GROUP4,
504                    RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
505                    RG_SYSPLL_BIAS_LPF_EN);
506         core_write(priv, CORE_PLL_GROUP2,
507                    RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
508                    RG_SYSPLL_POSDIV(1));
509         core_write(priv, CORE_PLL_GROUP7,
510                    RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
511                    RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
512         core_set(priv, CORE_TRGMII_GSW_CLK_CG,
513                  REG_GSWCK_EN | REG_TRGMIICK_EN);
514
515         if (!trgint)
516                 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
517                         mt7530_rmw(priv, MT7530_TRGMII_RD(i),
518                                    RD_TAP_MASK, RD_TAP(16));
519         else
520                 mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
521
522         return 0;
523 }
524
525 static int
526 mt7623_pad_clk_setup(struct dsa_switch *ds)
527 {
528         struct mt7530_priv *priv = ds->priv;
529         int i;
530
531         for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
532                 mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
533                                     TD_DM_DRVP(8) | TD_DM_DRVN(8));
534
535         mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
536         mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
537
538         return 0;
539 }
540
541 static void
542 mt7530_mib_reset(struct dsa_switch *ds)
543 {
544         struct mt7530_priv *priv = ds->priv;
545
546         mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
547         mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
548 }
549
550 static void
551 mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
552 {
553         u32 mask = PMCR_TX_EN | PMCR_RX_EN | PMCR_FORCE_LNK;
554
555         if (enable)
556                 mt7530_set(priv, MT7530_PMCR_P(port), mask);
557         else
558                 mt7530_clear(priv, MT7530_PMCR_P(port), mask);
559 }
560
561 static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
562 {
563         struct mt7530_priv *priv = ds->priv;
564
565         return mdiobus_read_nested(priv->bus, port, regnum);
566 }
567
568 static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
569                             u16 val)
570 {
571         struct mt7530_priv *priv = ds->priv;
572
573         return mdiobus_write_nested(priv->bus, port, regnum, val);
574 }
575
576 static void
577 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
578                    uint8_t *data)
579 {
580         int i;
581
582         if (stringset != ETH_SS_STATS)
583                 return;
584
585         for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
586                 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
587                         ETH_GSTRING_LEN);
588 }
589
590 static void
591 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
592                          uint64_t *data)
593 {
594         struct mt7530_priv *priv = ds->priv;
595         const struct mt7530_mib_desc *mib;
596         u32 reg, i;
597         u64 hi;
598
599         for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
600                 mib = &mt7530_mib[i];
601                 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
602
603                 data[i] = mt7530_read(priv, reg);
604                 if (mib->size == 2) {
605                         hi = mt7530_read(priv, reg + 4);
606                         data[i] |= hi << 32;
607                 }
608         }
609 }
610
611 static int
612 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
613 {
614         if (sset != ETH_SS_STATS)
615                 return 0;
616
617         return ARRAY_SIZE(mt7530_mib);
618 }
619
620 static void mt7530_adjust_link(struct dsa_switch *ds, int port,
621                                struct phy_device *phydev)
622 {
623         struct mt7530_priv *priv = ds->priv;
624
625         if (phy_is_pseudo_fixed_link(phydev)) {
626                 dev_dbg(priv->dev, "phy-mode for master device = %x\n",
627                         phydev->interface);
628
629                 /* Setup TX circuit incluing relevant PAD and driving */
630                 mt7530_pad_clk_setup(ds, phydev->interface);
631
632                 /* Setup RX circuit, relevant PAD and driving on the host
633                  * which must be placed after the setup on the device side is
634                  * all finished.
635                  */
636                 mt7623_pad_clk_setup(ds);
637         } else {
638                 u16 lcl_adv = 0, rmt_adv = 0;
639                 u8 flowctrl;
640                 u32 mcr = PMCR_USERP_LINK | PMCR_FORCE_MODE;
641
642                 switch (phydev->speed) {
643                 case SPEED_1000:
644                         mcr |= PMCR_FORCE_SPEED_1000;
645                         break;
646                 case SPEED_100:
647                         mcr |= PMCR_FORCE_SPEED_100;
648                         break;
649                 };
650
651                 if (phydev->link)
652                         mcr |= PMCR_FORCE_LNK;
653
654                 if (phydev->duplex) {
655                         mcr |= PMCR_FORCE_FDX;
656
657                         if (phydev->pause)
658                                 rmt_adv = LPA_PAUSE_CAP;
659                         if (phydev->asym_pause)
660                                 rmt_adv |= LPA_PAUSE_ASYM;
661
662                         if (phydev->advertising & ADVERTISED_Pause)
663                                 lcl_adv |= ADVERTISE_PAUSE_CAP;
664                         if (phydev->advertising & ADVERTISED_Asym_Pause)
665                                 lcl_adv |= ADVERTISE_PAUSE_ASYM;
666
667                         flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
668
669                         if (flowctrl & FLOW_CTRL_TX)
670                                 mcr |= PMCR_TX_FC_EN;
671                         if (flowctrl & FLOW_CTRL_RX)
672                                 mcr |= PMCR_RX_FC_EN;
673                 }
674                 mt7530_write(priv, MT7530_PMCR_P(port), mcr);
675         }
676 }
677
678 static int
679 mt7530_cpu_port_enable(struct mt7530_priv *priv,
680                        int port)
681 {
682         /* Enable Mediatek header mode on the cpu port */
683         mt7530_write(priv, MT7530_PVC_P(port),
684                      PORT_SPEC_TAG);
685
686         /* Setup the MAC by default for the cpu port */
687         mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
688
689         /* Unknown multicast frame forwarding to the cpu port */
690         mt7530_rmw(priv, MT7530_MFC, UNM_FFP_MASK, UNM_FFP(BIT(port)));
691
692         /* CPU port gets connected to all user ports of
693          * the switch
694          */
695         mt7530_write(priv, MT7530_PCR_P(port),
696                      PCR_MATRIX(dsa_user_ports(priv->ds)));
697
698         return 0;
699 }
700
701 static int
702 mt7530_port_enable(struct dsa_switch *ds, int port,
703                    struct phy_device *phy)
704 {
705         struct mt7530_priv *priv = ds->priv;
706
707         mutex_lock(&priv->reg_mutex);
708
709         /* Setup the MAC for the user port */
710         mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
711
712         /* Allow the user port gets connected to the cpu port and also
713          * restore the port matrix if the port is the member of a certain
714          * bridge.
715          */
716         priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
717         priv->ports[port].enable = true;
718         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
719                    priv->ports[port].pm);
720         mt7530_port_set_status(priv, port, 1);
721
722         mutex_unlock(&priv->reg_mutex);
723
724         return 0;
725 }
726
727 static void
728 mt7530_port_disable(struct dsa_switch *ds, int port,
729                     struct phy_device *phy)
730 {
731         struct mt7530_priv *priv = ds->priv;
732
733         mutex_lock(&priv->reg_mutex);
734
735         /* Clear up all port matrix which could be restored in the next
736          * enablement for the port.
737          */
738         priv->ports[port].enable = false;
739         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
740                    PCR_MATRIX_CLR);
741         mt7530_port_set_status(priv, port, 0);
742
743         mutex_unlock(&priv->reg_mutex);
744 }
745
746 static void
747 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
748 {
749         struct mt7530_priv *priv = ds->priv;
750         u32 stp_state;
751
752         switch (state) {
753         case BR_STATE_DISABLED:
754                 stp_state = MT7530_STP_DISABLED;
755                 break;
756         case BR_STATE_BLOCKING:
757                 stp_state = MT7530_STP_BLOCKING;
758                 break;
759         case BR_STATE_LISTENING:
760                 stp_state = MT7530_STP_LISTENING;
761                 break;
762         case BR_STATE_LEARNING:
763                 stp_state = MT7530_STP_LEARNING;
764                 break;
765         case BR_STATE_FORWARDING:
766         default:
767                 stp_state = MT7530_STP_FORWARDING;
768                 break;
769         }
770
771         mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
772 }
773
774 static int
775 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
776                         struct net_device *bridge)
777 {
778         struct mt7530_priv *priv = ds->priv;
779         u32 port_bitmap = BIT(MT7530_CPU_PORT);
780         int i;
781
782         mutex_lock(&priv->reg_mutex);
783
784         for (i = 0; i < MT7530_NUM_PORTS; i++) {
785                 /* Add this port to the port matrix of the other ports in the
786                  * same bridge. If the port is disabled, port matrix is kept
787                  * and not being setup until the port becomes enabled.
788                  */
789                 if (dsa_is_user_port(ds, i) && i != port) {
790                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
791                                 continue;
792                         if (priv->ports[i].enable)
793                                 mt7530_set(priv, MT7530_PCR_P(i),
794                                            PCR_MATRIX(BIT(port)));
795                         priv->ports[i].pm |= PCR_MATRIX(BIT(port));
796
797                         port_bitmap |= BIT(i);
798                 }
799         }
800
801         /* Add the all other ports to this port matrix. */
802         if (priv->ports[port].enable)
803                 mt7530_rmw(priv, MT7530_PCR_P(port),
804                            PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
805         priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
806
807         mutex_unlock(&priv->reg_mutex);
808
809         return 0;
810 }
811
812 static void
813 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
814 {
815         struct mt7530_priv *priv = ds->priv;
816         bool all_user_ports_removed = true;
817         int i;
818
819         /* When a port is removed from the bridge, the port would be set up
820          * back to the default as is at initial boot which is a VLAN-unaware
821          * port.
822          */
823         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
824                    MT7530_PORT_MATRIX_MODE);
825         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
826                    VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
827                    PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
828
829         priv->ports[port].vlan_filtering = false;
830
831         for (i = 0; i < MT7530_NUM_PORTS; i++) {
832                 if (dsa_is_user_port(ds, i) &&
833                     priv->ports[i].vlan_filtering) {
834                         all_user_ports_removed = false;
835                         break;
836                 }
837         }
838
839         /* CPU port also does the same thing until all user ports belonging to
840          * the CPU port get out of VLAN filtering mode.
841          */
842         if (all_user_ports_removed) {
843                 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
844                              PCR_MATRIX(dsa_user_ports(priv->ds)));
845                 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
846                              | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
847         }
848 }
849
850 static void
851 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
852 {
853         struct mt7530_priv *priv = ds->priv;
854
855         /* Trapped into security mode allows packet forwarding through VLAN
856          * table lookup. CPU port is set to fallback mode to let untagged
857          * frames pass through.
858          */
859         if (dsa_is_cpu_port(ds, port))
860                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
861                            MT7530_PORT_FALLBACK_MODE);
862         else
863                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
864                            MT7530_PORT_SECURITY_MODE);
865
866         /* Set the port as a user port which is to be able to recognize VID
867          * from incoming packets before fetching entry within the VLAN table.
868          */
869         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
870                    VLAN_ATTR(MT7530_VLAN_USER) |
871                    PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
872 }
873
874 static void
875 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
876                          struct net_device *bridge)
877 {
878         struct mt7530_priv *priv = ds->priv;
879         int i;
880
881         mutex_lock(&priv->reg_mutex);
882
883         for (i = 0; i < MT7530_NUM_PORTS; i++) {
884                 /* Remove this port from the port matrix of the other ports
885                  * in the same bridge. If the port is disabled, port matrix
886                  * is kept and not being setup until the port becomes enabled.
887                  * And the other port's port matrix cannot be broken when the
888                  * other port is still a VLAN-aware port.
889                  */
890                 if (!priv->ports[i].vlan_filtering &&
891                     dsa_is_user_port(ds, i) && i != port) {
892                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
893                                 continue;
894                         if (priv->ports[i].enable)
895                                 mt7530_clear(priv, MT7530_PCR_P(i),
896                                              PCR_MATRIX(BIT(port)));
897                         priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
898                 }
899         }
900
901         /* Set the cpu port to be the only one in the port matrix of
902          * this port.
903          */
904         if (priv->ports[port].enable)
905                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
906                            PCR_MATRIX(BIT(MT7530_CPU_PORT)));
907         priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
908
909         mt7530_port_set_vlan_unaware(ds, port);
910
911         mutex_unlock(&priv->reg_mutex);
912 }
913
914 static int
915 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
916                     const unsigned char *addr, u16 vid)
917 {
918         struct mt7530_priv *priv = ds->priv;
919         int ret;
920         u8 port_mask = BIT(port);
921
922         mutex_lock(&priv->reg_mutex);
923         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
924         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
925         mutex_unlock(&priv->reg_mutex);
926
927         return ret;
928 }
929
930 static int
931 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
932                     const unsigned char *addr, u16 vid)
933 {
934         struct mt7530_priv *priv = ds->priv;
935         int ret;
936         u8 port_mask = BIT(port);
937
938         mutex_lock(&priv->reg_mutex);
939         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
940         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
941         mutex_unlock(&priv->reg_mutex);
942
943         return ret;
944 }
945
946 static int
947 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
948                      dsa_fdb_dump_cb_t *cb, void *data)
949 {
950         struct mt7530_priv *priv = ds->priv;
951         struct mt7530_fdb _fdb = { 0 };
952         int cnt = MT7530_NUM_FDB_RECORDS;
953         int ret = 0;
954         u32 rsp = 0;
955
956         mutex_lock(&priv->reg_mutex);
957
958         ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
959         if (ret < 0)
960                 goto err;
961
962         do {
963                 if (rsp & ATC_SRCH_HIT) {
964                         mt7530_fdb_read(priv, &_fdb);
965                         if (_fdb.port_mask & BIT(port)) {
966                                 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
967                                          data);
968                                 if (ret < 0)
969                                         break;
970                         }
971                 }
972         } while (--cnt &&
973                  !(rsp & ATC_SRCH_END) &&
974                  !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
975 err:
976         mutex_unlock(&priv->reg_mutex);
977
978         return 0;
979 }
980
981 static int
982 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
983 {
984         struct mt7530_dummy_poll p;
985         u32 val;
986         int ret;
987
988         val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
989         mt7530_write(priv, MT7530_VTCR, val);
990
991         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
992         ret = readx_poll_timeout(_mt7530_read, &p, val,
993                                  !(val & VTCR_BUSY), 20, 20000);
994         if (ret < 0) {
995                 dev_err(priv->dev, "poll timeout\n");
996                 return ret;
997         }
998
999         val = mt7530_read(priv, MT7530_VTCR);
1000         if (val & VTCR_INVALID) {
1001                 dev_err(priv->dev, "read VTCR invalid\n");
1002                 return -EINVAL;
1003         }
1004
1005         return 0;
1006 }
1007
1008 static int
1009 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
1010                            bool vlan_filtering)
1011 {
1012         struct mt7530_priv *priv = ds->priv;
1013
1014         priv->ports[port].vlan_filtering = vlan_filtering;
1015
1016         if (vlan_filtering) {
1017                 /* The port is being kept as VLAN-unaware port when bridge is
1018                  * set up with vlan_filtering not being set, Otherwise, the
1019                  * port and the corresponding CPU port is required the setup
1020                  * for becoming a VLAN-aware port.
1021                  */
1022                 mt7530_port_set_vlan_aware(ds, port);
1023                 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
1024         }
1025
1026         return 0;
1027 }
1028
1029 static int
1030 mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
1031                          const struct switchdev_obj_port_vlan *vlan)
1032 {
1033         /* nothing needed */
1034
1035         return 0;
1036 }
1037
1038 static void
1039 mt7530_hw_vlan_add(struct mt7530_priv *priv,
1040                    struct mt7530_hw_vlan_entry *entry)
1041 {
1042         u8 new_members;
1043         u32 val;
1044
1045         new_members = entry->old_members | BIT(entry->port) |
1046                       BIT(MT7530_CPU_PORT);
1047
1048         /* Validate the entry with independent learning, create egress tag per
1049          * VLAN and joining the port as one of the port members.
1050          */
1051         val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
1052         mt7530_write(priv, MT7530_VAWD1, val);
1053
1054         /* Decide whether adding tag or not for those outgoing packets from the
1055          * port inside the VLAN.
1056          */
1057         val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1058                                 MT7530_VLAN_EGRESS_TAG;
1059         mt7530_rmw(priv, MT7530_VAWD2,
1060                    ETAG_CTRL_P_MASK(entry->port),
1061                    ETAG_CTRL_P(entry->port, val));
1062
1063         /* CPU port is always taken as a tagged port for serving more than one
1064          * VLANs across and also being applied with egress type stack mode for
1065          * that VLAN tags would be appended after hardware special tag used as
1066          * DSA tag.
1067          */
1068         mt7530_rmw(priv, MT7530_VAWD2,
1069                    ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1070                    ETAG_CTRL_P(MT7530_CPU_PORT,
1071                                MT7530_VLAN_EGRESS_STACK));
1072 }
1073
1074 static void
1075 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1076                    struct mt7530_hw_vlan_entry *entry)
1077 {
1078         u8 new_members;
1079         u32 val;
1080
1081         new_members = entry->old_members & ~BIT(entry->port);
1082
1083         val = mt7530_read(priv, MT7530_VAWD1);
1084         if (!(val & VLAN_VALID)) {
1085                 dev_err(priv->dev,
1086                         "Cannot be deleted due to invalid entry\n");
1087                 return;
1088         }
1089
1090         /* If certain member apart from CPU port is still alive in the VLAN,
1091          * the entry would be kept valid. Otherwise, the entry is got to be
1092          * disabled.
1093          */
1094         if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1095                 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1096                       VLAN_VALID;
1097                 mt7530_write(priv, MT7530_VAWD1, val);
1098         } else {
1099                 mt7530_write(priv, MT7530_VAWD1, 0);
1100                 mt7530_write(priv, MT7530_VAWD2, 0);
1101         }
1102 }
1103
1104 static void
1105 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1106                       struct mt7530_hw_vlan_entry *entry,
1107                       mt7530_vlan_op vlan_op)
1108 {
1109         u32 val;
1110
1111         /* Fetch entry */
1112         mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1113
1114         val = mt7530_read(priv, MT7530_VAWD1);
1115
1116         entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1117
1118         /* Manipulate entry */
1119         vlan_op(priv, entry);
1120
1121         /* Flush result to hardware */
1122         mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1123 }
1124
1125 static void
1126 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1127                      const struct switchdev_obj_port_vlan *vlan)
1128 {
1129         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1130         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1131         struct mt7530_hw_vlan_entry new_entry;
1132         struct mt7530_priv *priv = ds->priv;
1133         u16 vid;
1134
1135         /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1136          * being set.
1137          */
1138         if (!priv->ports[port].vlan_filtering)
1139                 return;
1140
1141         mutex_lock(&priv->reg_mutex);
1142
1143         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1144                 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1145                 mt7530_hw_vlan_update(priv, vid, &new_entry,
1146                                       mt7530_hw_vlan_add);
1147         }
1148
1149         if (pvid) {
1150                 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1151                            G0_PORT_VID(vlan->vid_end));
1152                 priv->ports[port].pvid = vlan->vid_end;
1153         }
1154
1155         mutex_unlock(&priv->reg_mutex);
1156 }
1157
1158 static int
1159 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1160                      const struct switchdev_obj_port_vlan *vlan)
1161 {
1162         struct mt7530_hw_vlan_entry target_entry;
1163         struct mt7530_priv *priv = ds->priv;
1164         u16 vid, pvid;
1165
1166         /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1167          * being set.
1168          */
1169         if (!priv->ports[port].vlan_filtering)
1170                 return 0;
1171
1172         mutex_lock(&priv->reg_mutex);
1173
1174         pvid = priv->ports[port].pvid;
1175         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1176                 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1177                 mt7530_hw_vlan_update(priv, vid, &target_entry,
1178                                       mt7530_hw_vlan_del);
1179
1180                 /* PVID is being restored to the default whenever the PVID port
1181                  * is being removed from the VLAN.
1182                  */
1183                 if (pvid == vid)
1184                         pvid = G0_PORT_VID_DEF;
1185         }
1186
1187         mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1188         priv->ports[port].pvid = pvid;
1189
1190         mutex_unlock(&priv->reg_mutex);
1191
1192         return 0;
1193 }
1194
1195 static enum dsa_tag_protocol
1196 mtk_get_tag_protocol(struct dsa_switch *ds, int port)
1197 {
1198         struct mt7530_priv *priv = ds->priv;
1199
1200         if (port != MT7530_CPU_PORT) {
1201                 dev_warn(priv->dev,
1202                          "port not matched with tagging CPU port\n");
1203                 return DSA_TAG_PROTO_NONE;
1204         } else {
1205                 return DSA_TAG_PROTO_MTK;
1206         }
1207 }
1208
1209 static int
1210 mt7530_setup(struct dsa_switch *ds)
1211 {
1212         struct mt7530_priv *priv = ds->priv;
1213         int ret, i;
1214         u32 id, val;
1215         struct device_node *dn;
1216         struct mt7530_dummy_poll p;
1217
1218         /* The parent node of master netdev which holds the common system
1219          * controller also is the container for two GMACs nodes representing
1220          * as two netdev instances.
1221          */
1222         dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent;
1223         priv->ethernet = syscon_node_to_regmap(dn);
1224         if (IS_ERR(priv->ethernet))
1225                 return PTR_ERR(priv->ethernet);
1226
1227         regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1228         ret = regulator_enable(priv->core_pwr);
1229         if (ret < 0) {
1230                 dev_err(priv->dev,
1231                         "Failed to enable core power: %d\n", ret);
1232                 return ret;
1233         }
1234
1235         regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1236         ret = regulator_enable(priv->io_pwr);
1237         if (ret < 0) {
1238                 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1239                         ret);
1240                 return ret;
1241         }
1242
1243         /* Reset whole chip through gpio pin or memory-mapped registers for
1244          * different type of hardware
1245          */
1246         if (priv->mcm) {
1247                 reset_control_assert(priv->rstc);
1248                 usleep_range(1000, 1100);
1249                 reset_control_deassert(priv->rstc);
1250         } else {
1251                 gpiod_set_value_cansleep(priv->reset, 0);
1252                 usleep_range(1000, 1100);
1253                 gpiod_set_value_cansleep(priv->reset, 1);
1254         }
1255
1256         /* Waiting for MT7530 got to stable */
1257         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1258         ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1259                                  20, 1000000);
1260         if (ret < 0) {
1261                 dev_err(priv->dev, "reset timeout\n");
1262                 return ret;
1263         }
1264
1265         id = mt7530_read(priv, MT7530_CREV);
1266         id >>= CHIP_NAME_SHIFT;
1267         if (id != MT7530_ID) {
1268                 dev_err(priv->dev, "chip %x can't be supported\n", id);
1269                 return -ENODEV;
1270         }
1271
1272         /* Reset the switch through internal reset */
1273         mt7530_write(priv, MT7530_SYS_CTRL,
1274                      SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1275                      SYS_CTRL_REG_RST);
1276
1277         /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1278         val = mt7530_read(priv, MT7530_MHWTRAP);
1279         val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1280         val |= MHWTRAP_MANUAL;
1281         mt7530_write(priv, MT7530_MHWTRAP, val);
1282
1283         /* Enable and reset MIB counters */
1284         mt7530_mib_reset(ds);
1285
1286         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1287                 /* Disable forwarding by default on all ports */
1288                 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1289                            PCR_MATRIX_CLR);
1290
1291                 if (dsa_is_cpu_port(ds, i))
1292                         mt7530_cpu_port_enable(priv, i);
1293                 else
1294                         mt7530_port_disable(ds, i, NULL);
1295
1296                 /* Enable consistent egress tag */
1297                 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
1298                            PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
1299         }
1300
1301         /* Flush the FDB table */
1302         ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
1303         if (ret < 0)
1304                 return ret;
1305
1306         return 0;
1307 }
1308
1309 static const struct dsa_switch_ops mt7530_switch_ops = {
1310         .get_tag_protocol       = mtk_get_tag_protocol,
1311         .setup                  = mt7530_setup,
1312         .get_strings            = mt7530_get_strings,
1313         .phy_read               = mt7530_phy_read,
1314         .phy_write              = mt7530_phy_write,
1315         .get_ethtool_stats      = mt7530_get_ethtool_stats,
1316         .get_sset_count         = mt7530_get_sset_count,
1317         .adjust_link            = mt7530_adjust_link,
1318         .port_enable            = mt7530_port_enable,
1319         .port_disable           = mt7530_port_disable,
1320         .port_stp_state_set     = mt7530_stp_state_set,
1321         .port_bridge_join       = mt7530_port_bridge_join,
1322         .port_bridge_leave      = mt7530_port_bridge_leave,
1323         .port_fdb_add           = mt7530_port_fdb_add,
1324         .port_fdb_del           = mt7530_port_fdb_del,
1325         .port_fdb_dump          = mt7530_port_fdb_dump,
1326         .port_vlan_filtering    = mt7530_port_vlan_filtering,
1327         .port_vlan_prepare      = mt7530_port_vlan_prepare,
1328         .port_vlan_add          = mt7530_port_vlan_add,
1329         .port_vlan_del          = mt7530_port_vlan_del,
1330 };
1331
1332 static int
1333 mt7530_probe(struct mdio_device *mdiodev)
1334 {
1335         struct mt7530_priv *priv;
1336         struct device_node *dn;
1337
1338         dn = mdiodev->dev.of_node;
1339
1340         priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1341         if (!priv)
1342                 return -ENOMEM;
1343
1344         priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
1345         if (!priv->ds)
1346                 return -ENOMEM;
1347
1348         /* Use medatek,mcm property to distinguish hardware type that would
1349          * casues a little bit differences on power-on sequence.
1350          */
1351         priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1352         if (priv->mcm) {
1353                 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1354
1355                 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1356                 if (IS_ERR(priv->rstc)) {
1357                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1358                         return PTR_ERR(priv->rstc);
1359                 }
1360         }
1361
1362         priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1363         if (IS_ERR(priv->core_pwr))
1364                 return PTR_ERR(priv->core_pwr);
1365
1366         priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1367         if (IS_ERR(priv->io_pwr))
1368                 return PTR_ERR(priv->io_pwr);
1369
1370         /* Not MCM that indicates switch works as the remote standalone
1371          * integrated circuit so the GPIO pin would be used to complete
1372          * the reset, otherwise memory-mapped register accessing used
1373          * through syscon provides in the case of MCM.
1374          */
1375         if (!priv->mcm) {
1376                 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1377                                                       GPIOD_OUT_LOW);
1378                 if (IS_ERR(priv->reset)) {
1379                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1380                         return PTR_ERR(priv->reset);
1381                 }
1382         }
1383
1384         priv->bus = mdiodev->bus;
1385         priv->dev = &mdiodev->dev;
1386         priv->ds->priv = priv;
1387         priv->ds->ops = &mt7530_switch_ops;
1388         mutex_init(&priv->reg_mutex);
1389         dev_set_drvdata(&mdiodev->dev, priv);
1390
1391         return dsa_register_switch(priv->ds);
1392 }
1393
1394 static void
1395 mt7530_remove(struct mdio_device *mdiodev)
1396 {
1397         struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1398         int ret = 0;
1399
1400         ret = regulator_disable(priv->core_pwr);
1401         if (ret < 0)
1402                 dev_err(priv->dev,
1403                         "Failed to disable core power: %d\n", ret);
1404
1405         ret = regulator_disable(priv->io_pwr);
1406         if (ret < 0)
1407                 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1408                         ret);
1409
1410         dsa_unregister_switch(priv->ds);
1411         mutex_destroy(&priv->reg_mutex);
1412 }
1413
1414 static const struct of_device_id mt7530_of_match[] = {
1415         { .compatible = "mediatek,mt7530" },
1416         { /* sentinel */ },
1417 };
1418 MODULE_DEVICE_TABLE(of, mt7530_of_match);
1419
1420 static struct mdio_driver mt7530_mdio_driver = {
1421         .probe  = mt7530_probe,
1422         .remove = mt7530_remove,
1423         .mdiodrv.driver = {
1424                 .name = "mt7530",
1425                 .of_match_table = mt7530_of_match,
1426         },
1427 };
1428
1429 mdio_module_driver(mt7530_mdio_driver);
1430
1431 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1432 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1433 MODULE_LICENSE("GPL");