84391c8a0e16c5e6e19fcc07cb26180b28cf516e
[platform/kernel/linux-rpi.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_read(struct mt7530_dummy_poll *p)
238 {
239         struct mii_bus          *bus = p->priv->bus;
240         u32 val;
241
242         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
243
244         val = mt7530_mii_read(p->priv, p->reg);
245
246         mutex_unlock(&bus->mdio_lock);
247
248         return val;
249 }
250
251 static u32
252 mt7530_read(struct mt7530_priv *priv, u32 reg)
253 {
254         struct mt7530_dummy_poll p;
255
256         INIT_MT7530_DUMMY_POLL(&p, priv, reg);
257         return _mt7530_read(&p);
258 }
259
260 static void
261 mt7530_rmw(struct mt7530_priv *priv, u32 reg,
262            u32 mask, u32 set)
263 {
264         struct mii_bus *bus = priv->bus;
265         u32 val;
266
267         mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
268
269         val = mt7530_mii_read(priv, reg);
270         val &= ~mask;
271         val |= set;
272         mt7530_mii_write(priv, reg, val);
273
274         mutex_unlock(&bus->mdio_lock);
275 }
276
277 static void
278 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
279 {
280         mt7530_rmw(priv, reg, 0, val);
281 }
282
283 static void
284 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
285 {
286         mt7530_rmw(priv, reg, val, 0);
287 }
288
289 static int
290 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
291 {
292         u32 val;
293         int ret;
294         struct mt7530_dummy_poll p;
295
296         /* Set the command operating upon the MAC address entries */
297         val = ATC_BUSY | ATC_MAT(0) | cmd;
298         mt7530_write(priv, MT7530_ATC, val);
299
300         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
301         ret = readx_poll_timeout(_mt7530_read, &p, val,
302                                  !(val & ATC_BUSY), 20, 20000);
303         if (ret < 0) {
304                 dev_err(priv->dev, "reset timeout\n");
305                 return ret;
306         }
307
308         /* Additional sanity for read command if the specified
309          * entry is invalid
310          */
311         val = mt7530_read(priv, MT7530_ATC);
312         if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
313                 return -EINVAL;
314
315         if (rsp)
316                 *rsp = val;
317
318         return 0;
319 }
320
321 static void
322 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
323 {
324         u32 reg[3];
325         int i;
326
327         /* Read from ARL table into an array */
328         for (i = 0; i < 3; i++) {
329                 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
330
331                 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
332                         __func__, __LINE__, i, reg[i]);
333         }
334
335         fdb->vid = (reg[1] >> CVID) & CVID_MASK;
336         fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
337         fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
338         fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
339         fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
340         fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
341         fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
342         fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
343         fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
344         fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
345 }
346
347 static void
348 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
349                  u8 port_mask, const u8 *mac,
350                  u8 aging, u8 type)
351 {
352         u32 reg[3] = { 0 };
353         int i;
354
355         reg[1] |= vid & CVID_MASK;
356         reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
357         reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
358         /* STATIC_ENT indicate that entry is static wouldn't
359          * be aged out and STATIC_EMP specified as erasing an
360          * entry
361          */
362         reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
363         reg[1] |= mac[5] << MAC_BYTE_5;
364         reg[1] |= mac[4] << MAC_BYTE_4;
365         reg[0] |= mac[3] << MAC_BYTE_3;
366         reg[0] |= mac[2] << MAC_BYTE_2;
367         reg[0] |= mac[1] << MAC_BYTE_1;
368         reg[0] |= mac[0] << MAC_BYTE_0;
369
370         /* Write array into the ARL table */
371         for (i = 0; i < 3; i++)
372                 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
373 }
374
375 static int
376 mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
377 {
378         struct mt7530_priv *priv = ds->priv;
379         u32 ncpo1, ssc_delta, trgint, i, xtal;
380
381         xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK;
382
383         if (xtal == HWTRAP_XTAL_20MHZ) {
384                 dev_err(priv->dev,
385                         "%s: MT7530 with a 20MHz XTAL is not supported!\n",
386                         __func__);
387                 return -EINVAL;
388         }
389
390         switch (mode) {
391         case PHY_INTERFACE_MODE_RGMII:
392                 trgint = 0;
393                 /* PLL frequency: 125MHz */
394                 ncpo1 = 0x0c80;
395                 break;
396         case PHY_INTERFACE_MODE_TRGMII:
397                 trgint = 1;
398                 if (priv->id == ID_MT7621) {
399                         /* PLL frequency: 150MHz: 1.2GBit */
400                         if (xtal == HWTRAP_XTAL_40MHZ)
401                                 ncpo1 = 0x0780;
402                         if (xtal == HWTRAP_XTAL_25MHZ)
403                                 ncpo1 = 0x0a00;
404                 } else { /* PLL frequency: 250MHz: 2.0Gbit */
405                         if (xtal == HWTRAP_XTAL_40MHZ)
406                                 ncpo1 = 0x0c80;
407                         if (xtal == HWTRAP_XTAL_25MHZ)
408                                 ncpo1 = 0x1400;
409                 }
410                 break;
411         default:
412                 dev_err(priv->dev, "xMII mode %d not supported\n", mode);
413                 return -EINVAL;
414         }
415
416         if (xtal == HWTRAP_XTAL_25MHZ)
417                 ssc_delta = 0x57;
418         else
419                 ssc_delta = 0x87;
420
421         mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
422                    P6_INTF_MODE(trgint));
423
424         /* Lower Tx Driving for TRGMII path */
425         for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
426                 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
427                              TD_DM_DRVP(8) | TD_DM_DRVN(8));
428
429         /* Setup core clock for MT7530 */
430         if (!trgint) {
431                 /* Disable MT7530 core clock */
432                 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
433
434                 /* Disable PLL, since phy_device has not yet been created
435                  * provided for phy_[read,write]_mmd_indirect is called, we
436                  * provide our own core_write_mmd_indirect to complete this
437                  * function.
438                  */
439                 core_write_mmd_indirect(priv,
440                                         CORE_GSWPLL_GRP1,
441                                         MDIO_MMD_VEND2,
442                                         0);
443
444                 /* Set core clock into 500Mhz */
445                 core_write(priv, CORE_GSWPLL_GRP2,
446                            RG_GSWPLL_POSDIV_500M(1) |
447                            RG_GSWPLL_FBKDIV_500M(25));
448
449                 /* Enable PLL */
450                 core_write(priv, CORE_GSWPLL_GRP1,
451                            RG_GSWPLL_EN_PRE |
452                            RG_GSWPLL_POSDIV_200M(2) |
453                            RG_GSWPLL_FBKDIV_200M(32));
454
455                 /* Enable MT7530 core clock */
456                 core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
457         }
458
459         /* Setup the MT7530 TRGMII Tx Clock */
460         core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
461         core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
462         core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
463         core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
464         core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
465         core_write(priv, CORE_PLL_GROUP4,
466                    RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
467                    RG_SYSPLL_BIAS_LPF_EN);
468         core_write(priv, CORE_PLL_GROUP2,
469                    RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
470                    RG_SYSPLL_POSDIV(1));
471         core_write(priv, CORE_PLL_GROUP7,
472                    RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
473                    RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
474         core_set(priv, CORE_TRGMII_GSW_CLK_CG,
475                  REG_GSWCK_EN | REG_TRGMIICK_EN);
476
477         if (!trgint)
478                 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
479                         mt7530_rmw(priv, MT7530_TRGMII_RD(i),
480                                    RD_TAP_MASK, RD_TAP(16));
481         return 0;
482 }
483
484 static void
485 mt7530_mib_reset(struct dsa_switch *ds)
486 {
487         struct mt7530_priv *priv = ds->priv;
488
489         mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
490         mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
491 }
492
493 static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
494 {
495         struct mt7530_priv *priv = ds->priv;
496
497         return mdiobus_read_nested(priv->bus, port, regnum);
498 }
499
500 static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
501                             u16 val)
502 {
503         struct mt7530_priv *priv = ds->priv;
504
505         return mdiobus_write_nested(priv->bus, port, regnum, val);
506 }
507
508 static void
509 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
510                    uint8_t *data)
511 {
512         int i;
513
514         if (stringset != ETH_SS_STATS)
515                 return;
516
517         for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
518                 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
519                         ETH_GSTRING_LEN);
520 }
521
522 static void
523 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
524                          uint64_t *data)
525 {
526         struct mt7530_priv *priv = ds->priv;
527         const struct mt7530_mib_desc *mib;
528         u32 reg, i;
529         u64 hi;
530
531         for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
532                 mib = &mt7530_mib[i];
533                 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
534
535                 data[i] = mt7530_read(priv, reg);
536                 if (mib->size == 2) {
537                         hi = mt7530_read(priv, reg + 4);
538                         data[i] |= hi << 32;
539                 }
540         }
541 }
542
543 static int
544 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
545 {
546         if (sset != ETH_SS_STATS)
547                 return 0;
548
549         return ARRAY_SIZE(mt7530_mib);
550 }
551
552 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
553 {
554         struct mt7530_priv *priv = ds->priv;
555         u8 tx_delay = 0;
556         int val;
557
558         mutex_lock(&priv->reg_mutex);
559
560         val = mt7530_read(priv, MT7530_MHWTRAP);
561
562         val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
563         val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
564
565         switch (priv->p5_intf_sel) {
566         case P5_INTF_SEL_PHY_P0:
567                 /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
568                 val |= MHWTRAP_PHY0_SEL;
569                 /* fall through */
570         case P5_INTF_SEL_PHY_P4:
571                 /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
572                 val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
573
574                 /* Setup the MAC by default for the cpu port */
575                 mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
576                 break;
577         case P5_INTF_SEL_GMAC5:
578                 /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
579                 val &= ~MHWTRAP_P5_DIS;
580                 break;
581         case P5_DISABLED:
582                 interface = PHY_INTERFACE_MODE_NA;
583                 break;
584         default:
585                 dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
586                         priv->p5_intf_sel);
587                 goto unlock_exit;
588         }
589
590         /* Setup RGMII settings */
591         if (phy_interface_mode_is_rgmii(interface)) {
592                 val |= MHWTRAP_P5_RGMII_MODE;
593
594                 /* P5 RGMII RX Clock Control: delay setting for 1000M */
595                 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
596
597                 /* Don't set delay in DSA mode */
598                 if (!dsa_is_dsa_port(priv->ds, 5) &&
599                     (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
600                      interface == PHY_INTERFACE_MODE_RGMII_ID))
601                         tx_delay = 4; /* n * 0.5 ns */
602
603                 /* P5 RGMII TX Clock Control: delay x */
604                 mt7530_write(priv, MT7530_P5RGMIITXCR,
605                              CSR_RGMII_TXC_CFG(0x10 + tx_delay));
606
607                 /* reduce P5 RGMII Tx driving, 8mA */
608                 mt7530_write(priv, MT7530_IO_DRV_CR,
609                              P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
610         }
611
612         mt7530_write(priv, MT7530_MHWTRAP, val);
613
614         dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
615                 val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
616
617         priv->p5_interface = interface;
618
619 unlock_exit:
620         mutex_unlock(&priv->reg_mutex);
621 }
622
623 static int
624 mt7530_cpu_port_enable(struct mt7530_priv *priv,
625                        int port)
626 {
627         /* Enable Mediatek header mode on the cpu port */
628         mt7530_write(priv, MT7530_PVC_P(port),
629                      PORT_SPEC_TAG);
630
631         /* Disable auto learning on the cpu port */
632         mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
633
634         /* Unknown unicast frame fordwarding to the cpu port */
635         mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
636
637         /* Set CPU port number */
638         if (priv->id == ID_MT7621)
639                 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
640
641         /* CPU port gets connected to all user ports of
642          * the switch
643          */
644         mt7530_write(priv, MT7530_PCR_P(port),
645                      PCR_MATRIX(dsa_user_ports(priv->ds)));
646
647         return 0;
648 }
649
650 static int
651 mt7530_port_enable(struct dsa_switch *ds, int port,
652                    struct phy_device *phy)
653 {
654         struct mt7530_priv *priv = ds->priv;
655
656         if (!dsa_is_user_port(ds, port))
657                 return 0;
658
659         mutex_lock(&priv->reg_mutex);
660
661         /* Allow the user port gets connected to the cpu port and also
662          * restore the port matrix if the port is the member of a certain
663          * bridge.
664          */
665         priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
666         priv->ports[port].enable = true;
667         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
668                    priv->ports[port].pm);
669         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
670
671         mutex_unlock(&priv->reg_mutex);
672
673         return 0;
674 }
675
676 static void
677 mt7530_port_disable(struct dsa_switch *ds, int port)
678 {
679         struct mt7530_priv *priv = ds->priv;
680
681         if (!dsa_is_user_port(ds, port))
682                 return;
683
684         mutex_lock(&priv->reg_mutex);
685
686         /* Clear up all port matrix which could be restored in the next
687          * enablement for the port.
688          */
689         priv->ports[port].enable = false;
690         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
691                    PCR_MATRIX_CLR);
692         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
693
694         mutex_unlock(&priv->reg_mutex);
695 }
696
697 static void
698 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
699 {
700         struct mt7530_priv *priv = ds->priv;
701         u32 stp_state;
702
703         switch (state) {
704         case BR_STATE_DISABLED:
705                 stp_state = MT7530_STP_DISABLED;
706                 break;
707         case BR_STATE_BLOCKING:
708                 stp_state = MT7530_STP_BLOCKING;
709                 break;
710         case BR_STATE_LISTENING:
711                 stp_state = MT7530_STP_LISTENING;
712                 break;
713         case BR_STATE_LEARNING:
714                 stp_state = MT7530_STP_LEARNING;
715                 break;
716         case BR_STATE_FORWARDING:
717         default:
718                 stp_state = MT7530_STP_FORWARDING;
719                 break;
720         }
721
722         mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
723 }
724
725 static int
726 mt7530_port_bridge_join(struct dsa_switch *ds, int port,
727                         struct net_device *bridge)
728 {
729         struct mt7530_priv *priv = ds->priv;
730         u32 port_bitmap = BIT(MT7530_CPU_PORT);
731         int i;
732
733         mutex_lock(&priv->reg_mutex);
734
735         for (i = 0; i < MT7530_NUM_PORTS; i++) {
736                 /* Add this port to the port matrix of the other ports in the
737                  * same bridge. If the port is disabled, port matrix is kept
738                  * and not being setup until the port becomes enabled.
739                  */
740                 if (dsa_is_user_port(ds, i) && i != port) {
741                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
742                                 continue;
743                         if (priv->ports[i].enable)
744                                 mt7530_set(priv, MT7530_PCR_P(i),
745                                            PCR_MATRIX(BIT(port)));
746                         priv->ports[i].pm |= PCR_MATRIX(BIT(port));
747
748                         port_bitmap |= BIT(i);
749                 }
750         }
751
752         /* Add the all other ports to this port matrix. */
753         if (priv->ports[port].enable)
754                 mt7530_rmw(priv, MT7530_PCR_P(port),
755                            PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
756         priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
757
758         mutex_unlock(&priv->reg_mutex);
759
760         return 0;
761 }
762
763 static void
764 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
765 {
766         struct mt7530_priv *priv = ds->priv;
767         bool all_user_ports_removed = true;
768         int i;
769
770         /* When a port is removed from the bridge, the port would be set up
771          * back to the default as is at initial boot which is a VLAN-unaware
772          * port.
773          */
774         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
775                    MT7530_PORT_MATRIX_MODE);
776         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
777                    VLAN_ATTR(MT7530_VLAN_TRANSPARENT));
778
779         for (i = 0; i < MT7530_NUM_PORTS; i++) {
780                 if (dsa_is_user_port(ds, i) &&
781                     dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
782                         all_user_ports_removed = false;
783                         break;
784                 }
785         }
786
787         /* CPU port also does the same thing until all user ports belonging to
788          * the CPU port get out of VLAN filtering mode.
789          */
790         if (all_user_ports_removed) {
791                 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
792                              PCR_MATRIX(dsa_user_ports(priv->ds)));
793                 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT),
794                              PORT_SPEC_TAG);
795         }
796 }
797
798 static void
799 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
800 {
801         struct mt7530_priv *priv = ds->priv;
802
803         /* The real fabric path would be decided on the membership in the
804          * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS
805          * means potential VLAN can be consisting of certain subset of all
806          * ports.
807          */
808         mt7530_rmw(priv, MT7530_PCR_P(port),
809                    PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS));
810
811         /* Trapped into security mode allows packet forwarding through VLAN
812          * table lookup.
813          */
814         mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
815                    MT7530_PORT_SECURITY_MODE);
816
817         /* Set the port as a user port which is to be able to recognize VID
818          * from incoming packets before fetching entry within the VLAN table.
819          */
820         mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
821                    VLAN_ATTR(MT7530_VLAN_USER));
822 }
823
824 static void
825 mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
826                          struct net_device *bridge)
827 {
828         struct mt7530_priv *priv = ds->priv;
829         int i;
830
831         mutex_lock(&priv->reg_mutex);
832
833         for (i = 0; i < MT7530_NUM_PORTS; i++) {
834                 /* Remove this port from the port matrix of the other ports
835                  * in the same bridge. If the port is disabled, port matrix
836                  * is kept and not being setup until the port becomes enabled.
837                  * And the other port's port matrix cannot be broken when the
838                  * other port is still a VLAN-aware port.
839                  */
840                 if (dsa_is_user_port(ds, i) && i != port &&
841                    !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
842                         if (dsa_to_port(ds, i)->bridge_dev != bridge)
843                                 continue;
844                         if (priv->ports[i].enable)
845                                 mt7530_clear(priv, MT7530_PCR_P(i),
846                                              PCR_MATRIX(BIT(port)));
847                         priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
848                 }
849         }
850
851         /* Set the cpu port to be the only one in the port matrix of
852          * this port.
853          */
854         if (priv->ports[port].enable)
855                 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
856                            PCR_MATRIX(BIT(MT7530_CPU_PORT)));
857         priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
858
859         mutex_unlock(&priv->reg_mutex);
860 }
861
862 static int
863 mt7530_port_fdb_add(struct dsa_switch *ds, int port,
864                     const unsigned char *addr, u16 vid)
865 {
866         struct mt7530_priv *priv = ds->priv;
867         int ret;
868         u8 port_mask = BIT(port);
869
870         mutex_lock(&priv->reg_mutex);
871         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
872         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
873         mutex_unlock(&priv->reg_mutex);
874
875         return ret;
876 }
877
878 static int
879 mt7530_port_fdb_del(struct dsa_switch *ds, int port,
880                     const unsigned char *addr, u16 vid)
881 {
882         struct mt7530_priv *priv = ds->priv;
883         int ret;
884         u8 port_mask = BIT(port);
885
886         mutex_lock(&priv->reg_mutex);
887         mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
888         ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
889         mutex_unlock(&priv->reg_mutex);
890
891         return ret;
892 }
893
894 static int
895 mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
896                      dsa_fdb_dump_cb_t *cb, void *data)
897 {
898         struct mt7530_priv *priv = ds->priv;
899         struct mt7530_fdb _fdb = { 0 };
900         int cnt = MT7530_NUM_FDB_RECORDS;
901         int ret = 0;
902         u32 rsp = 0;
903
904         mutex_lock(&priv->reg_mutex);
905
906         ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
907         if (ret < 0)
908                 goto err;
909
910         do {
911                 if (rsp & ATC_SRCH_HIT) {
912                         mt7530_fdb_read(priv, &_fdb);
913                         if (_fdb.port_mask & BIT(port)) {
914                                 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
915                                          data);
916                                 if (ret < 0)
917                                         break;
918                         }
919                 }
920         } while (--cnt &&
921                  !(rsp & ATC_SRCH_END) &&
922                  !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
923 err:
924         mutex_unlock(&priv->reg_mutex);
925
926         return 0;
927 }
928
929 static int
930 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
931 {
932         struct mt7530_dummy_poll p;
933         u32 val;
934         int ret;
935
936         val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
937         mt7530_write(priv, MT7530_VTCR, val);
938
939         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
940         ret = readx_poll_timeout(_mt7530_read, &p, val,
941                                  !(val & VTCR_BUSY), 20, 20000);
942         if (ret < 0) {
943                 dev_err(priv->dev, "poll timeout\n");
944                 return ret;
945         }
946
947         val = mt7530_read(priv, MT7530_VTCR);
948         if (val & VTCR_INVALID) {
949                 dev_err(priv->dev, "read VTCR invalid\n");
950                 return -EINVAL;
951         }
952
953         return 0;
954 }
955
956 static int
957 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
958                            bool vlan_filtering)
959 {
960         if (vlan_filtering) {
961                 /* The port is being kept as VLAN-unaware port when bridge is
962                  * set up with vlan_filtering not being set, Otherwise, the
963                  * port and the corresponding CPU port is required the setup
964                  * for becoming a VLAN-aware port.
965                  */
966                 mt7530_port_set_vlan_aware(ds, port);
967                 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
968         } else {
969                 mt7530_port_set_vlan_unaware(ds, port);
970         }
971
972         return 0;
973 }
974
975 static int
976 mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
977                          const struct switchdev_obj_port_vlan *vlan)
978 {
979         /* nothing needed */
980
981         return 0;
982 }
983
984 static void
985 mt7530_hw_vlan_add(struct mt7530_priv *priv,
986                    struct mt7530_hw_vlan_entry *entry)
987 {
988         u8 new_members;
989         u32 val;
990
991         new_members = entry->old_members | BIT(entry->port) |
992                       BIT(MT7530_CPU_PORT);
993
994         /* Validate the entry with independent learning, create egress tag per
995          * VLAN and joining the port as one of the port members.
996          */
997         val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
998         mt7530_write(priv, MT7530_VAWD1, val);
999
1000         /* Decide whether adding tag or not for those outgoing packets from the
1001          * port inside the VLAN.
1002          */
1003         val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1004                                 MT7530_VLAN_EGRESS_TAG;
1005         mt7530_rmw(priv, MT7530_VAWD2,
1006                    ETAG_CTRL_P_MASK(entry->port),
1007                    ETAG_CTRL_P(entry->port, val));
1008
1009         /* CPU port is always taken as a tagged port for serving more than one
1010          * VLANs across and also being applied with egress type stack mode for
1011          * that VLAN tags would be appended after hardware special tag used as
1012          * DSA tag.
1013          */
1014         mt7530_rmw(priv, MT7530_VAWD2,
1015                    ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1016                    ETAG_CTRL_P(MT7530_CPU_PORT,
1017                                MT7530_VLAN_EGRESS_STACK));
1018 }
1019
1020 static void
1021 mt7530_hw_vlan_del(struct mt7530_priv *priv,
1022                    struct mt7530_hw_vlan_entry *entry)
1023 {
1024         u8 new_members;
1025         u32 val;
1026
1027         new_members = entry->old_members & ~BIT(entry->port);
1028
1029         val = mt7530_read(priv, MT7530_VAWD1);
1030         if (!(val & VLAN_VALID)) {
1031                 dev_err(priv->dev,
1032                         "Cannot be deleted due to invalid entry\n");
1033                 return;
1034         }
1035
1036         /* If certain member apart from CPU port is still alive in the VLAN,
1037          * the entry would be kept valid. Otherwise, the entry is got to be
1038          * disabled.
1039          */
1040         if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1041                 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1042                       VLAN_VALID;
1043                 mt7530_write(priv, MT7530_VAWD1, val);
1044         } else {
1045                 mt7530_write(priv, MT7530_VAWD1, 0);
1046                 mt7530_write(priv, MT7530_VAWD2, 0);
1047         }
1048 }
1049
1050 static void
1051 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1052                       struct mt7530_hw_vlan_entry *entry,
1053                       mt7530_vlan_op vlan_op)
1054 {
1055         u32 val;
1056
1057         /* Fetch entry */
1058         mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1059
1060         val = mt7530_read(priv, MT7530_VAWD1);
1061
1062         entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1063
1064         /* Manipulate entry */
1065         vlan_op(priv, entry);
1066
1067         /* Flush result to hardware */
1068         mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1069 }
1070
1071 static void
1072 mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1073                      const struct switchdev_obj_port_vlan *vlan)
1074 {
1075         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1076         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1077         struct mt7530_hw_vlan_entry new_entry;
1078         struct mt7530_priv *priv = ds->priv;
1079         u16 vid;
1080
1081         /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1082          * being set.
1083          */
1084         if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1085                 return;
1086
1087         mutex_lock(&priv->reg_mutex);
1088
1089         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1090                 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1091                 mt7530_hw_vlan_update(priv, vid, &new_entry,
1092                                       mt7530_hw_vlan_add);
1093         }
1094
1095         if (pvid) {
1096                 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1097                            G0_PORT_VID(vlan->vid_end));
1098                 priv->ports[port].pvid = vlan->vid_end;
1099         }
1100
1101         mutex_unlock(&priv->reg_mutex);
1102 }
1103
1104 static int
1105 mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1106                      const struct switchdev_obj_port_vlan *vlan)
1107 {
1108         struct mt7530_hw_vlan_entry target_entry;
1109         struct mt7530_priv *priv = ds->priv;
1110         u16 vid, pvid;
1111
1112         /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1113          * being set.
1114          */
1115         if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1116                 return 0;
1117
1118         mutex_lock(&priv->reg_mutex);
1119
1120         pvid = priv->ports[port].pvid;
1121         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1122                 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1123                 mt7530_hw_vlan_update(priv, vid, &target_entry,
1124                                       mt7530_hw_vlan_del);
1125
1126                 /* PVID is being restored to the default whenever the PVID port
1127                  * is being removed from the VLAN.
1128                  */
1129                 if (pvid == vid)
1130                         pvid = G0_PORT_VID_DEF;
1131         }
1132
1133         mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1134         priv->ports[port].pvid = pvid;
1135
1136         mutex_unlock(&priv->reg_mutex);
1137
1138         return 0;
1139 }
1140
1141 static int mt7530_port_mirror_add(struct dsa_switch *ds, int port,
1142                                   struct dsa_mall_mirror_tc_entry *mirror,
1143                                   bool ingress)
1144 {
1145         struct mt7530_priv *priv = ds->priv;
1146         u32 val;
1147
1148         /* Check for existent entry */
1149         if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1150                 return -EEXIST;
1151
1152         val = mt7530_read(priv, MT7530_MFC);
1153
1154         /* MT7530 only supports one monitor port */
1155         if (val & MIRROR_EN && MIRROR_PORT(val) != mirror->to_local_port)
1156                 return -EEXIST;
1157
1158         val |= MIRROR_EN;
1159         val &= ~MIRROR_MASK;
1160         val |= mirror->to_local_port;
1161         mt7530_write(priv, MT7530_MFC, val);
1162
1163         val = mt7530_read(priv, MT7530_PCR_P(port));
1164         if (ingress) {
1165                 val |= PORT_RX_MIR;
1166                 priv->mirror_rx |= BIT(port);
1167         } else {
1168                 val |= PORT_TX_MIR;
1169                 priv->mirror_tx |= BIT(port);
1170         }
1171         mt7530_write(priv, MT7530_PCR_P(port), val);
1172
1173         return 0;
1174 }
1175
1176 static void mt7530_port_mirror_del(struct dsa_switch *ds, int port,
1177                                    struct dsa_mall_mirror_tc_entry *mirror)
1178 {
1179         struct mt7530_priv *priv = ds->priv;
1180         u32 val;
1181
1182         val = mt7530_read(priv, MT7530_PCR_P(port));
1183         if (mirror->ingress) {
1184                 val &= ~PORT_RX_MIR;
1185                 priv->mirror_rx &= ~BIT(port);
1186         } else {
1187                 val &= ~PORT_TX_MIR;
1188                 priv->mirror_tx &= ~BIT(port);
1189         }
1190         mt7530_write(priv, MT7530_PCR_P(port), val);
1191
1192         if (!priv->mirror_rx && !priv->mirror_tx) {
1193                 val = mt7530_read(priv, MT7530_MFC);
1194                 val &= ~MIRROR_EN;
1195                 mt7530_write(priv, MT7530_MFC, val);
1196         }
1197 }
1198
1199 static enum dsa_tag_protocol
1200 mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1201                      enum dsa_tag_protocol mp)
1202 {
1203         struct mt7530_priv *priv = ds->priv;
1204
1205         if (port != MT7530_CPU_PORT) {
1206                 dev_warn(priv->dev,
1207                          "port not matched with tagging CPU port\n");
1208                 return DSA_TAG_PROTO_NONE;
1209         } else {
1210                 return DSA_TAG_PROTO_MTK;
1211         }
1212 }
1213
1214 static int
1215 mt7530_setup(struct dsa_switch *ds)
1216 {
1217         struct mt7530_priv *priv = ds->priv;
1218         struct device_node *phy_node;
1219         struct device_node *mac_np;
1220         struct mt7530_dummy_poll p;
1221         phy_interface_t interface;
1222         struct device_node *dn;
1223         u32 id, val;
1224         int ret, i;
1225
1226         /* The parent node of master netdev which holds the common system
1227          * controller also is the container for two GMACs nodes representing
1228          * as two netdev instances.
1229          */
1230         dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
1231
1232         if (priv->id == ID_MT7530) {
1233                 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1234                 ret = regulator_enable(priv->core_pwr);
1235                 if (ret < 0) {
1236                         dev_err(priv->dev,
1237                                 "Failed to enable core power: %d\n", ret);
1238                         return ret;
1239                 }
1240
1241                 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1242                 ret = regulator_enable(priv->io_pwr);
1243                 if (ret < 0) {
1244                         dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1245                                 ret);
1246                         return ret;
1247                 }
1248         }
1249
1250         /* Reset whole chip through gpio pin or memory-mapped registers for
1251          * different type of hardware
1252          */
1253         if (priv->mcm) {
1254                 reset_control_assert(priv->rstc);
1255                 usleep_range(1000, 1100);
1256                 reset_control_deassert(priv->rstc);
1257         } else {
1258                 gpiod_set_value_cansleep(priv->reset, 0);
1259                 usleep_range(1000, 1100);
1260                 gpiod_set_value_cansleep(priv->reset, 1);
1261         }
1262
1263         /* Waiting for MT7530 got to stable */
1264         INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1265         ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1266                                  20, 1000000);
1267         if (ret < 0) {
1268                 dev_err(priv->dev, "reset timeout\n");
1269                 return ret;
1270         }
1271
1272         id = mt7530_read(priv, MT7530_CREV);
1273         id >>= CHIP_NAME_SHIFT;
1274         if (id != MT7530_ID) {
1275                 dev_err(priv->dev, "chip %x can't be supported\n", id);
1276                 return -ENODEV;
1277         }
1278
1279         /* Reset the switch through internal reset */
1280         mt7530_write(priv, MT7530_SYS_CTRL,
1281                      SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1282                      SYS_CTRL_REG_RST);
1283
1284         /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1285         val = mt7530_read(priv, MT7530_MHWTRAP);
1286         val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1287         val |= MHWTRAP_MANUAL;
1288         mt7530_write(priv, MT7530_MHWTRAP, val);
1289
1290         priv->p6_interface = PHY_INTERFACE_MODE_NA;
1291
1292         /* Enable and reset MIB counters */
1293         mt7530_mib_reset(ds);
1294
1295         mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
1296
1297         for (i = 0; i < MT7530_NUM_PORTS; i++) {
1298                 /* Disable forwarding by default on all ports */
1299                 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1300                            PCR_MATRIX_CLR);
1301
1302                 if (dsa_is_cpu_port(ds, i))
1303                         mt7530_cpu_port_enable(priv, i);
1304                 else
1305                         mt7530_port_disable(ds, i);
1306         }
1307
1308         /* Setup port 5 */
1309         priv->p5_intf_sel = P5_DISABLED;
1310         interface = PHY_INTERFACE_MODE_NA;
1311
1312         if (!dsa_is_unused_port(ds, 5)) {
1313                 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
1314                 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
1315                 if (ret && ret != -ENODEV)
1316                         return ret;
1317         } else {
1318                 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
1319                 for_each_child_of_node(dn, mac_np) {
1320                         if (!of_device_is_compatible(mac_np,
1321                                                      "mediatek,eth-mac"))
1322                                 continue;
1323
1324                         ret = of_property_read_u32(mac_np, "reg", &id);
1325                         if (ret < 0 || id != 1)
1326                                 continue;
1327
1328                         phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
1329                         if (!phy_node)
1330                                 continue;
1331
1332                         if (phy_node->parent == priv->dev->of_node->parent) {
1333                                 ret = of_get_phy_mode(mac_np, &interface);
1334                                 if (ret && ret != -ENODEV)
1335                                         return ret;
1336                                 id = of_mdio_parse_addr(ds->dev, phy_node);
1337                                 if (id == 0)
1338                                         priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
1339                                 if (id == 4)
1340                                         priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
1341                         }
1342                         of_node_put(phy_node);
1343                         break;
1344                 }
1345         }
1346
1347         mt7530_setup_port5(ds, interface);
1348
1349         /* Flush the FDB table */
1350         ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
1351         if (ret < 0)
1352                 return ret;
1353
1354         return 0;
1355 }
1356
1357 static void mt7530_phylink_mac_config(struct dsa_switch *ds, int port,
1358                                       unsigned int mode,
1359                                       const struct phylink_link_state *state)
1360 {
1361         struct mt7530_priv *priv = ds->priv;
1362         u32 mcr_cur, mcr_new;
1363
1364         switch (port) {
1365         case 0: /* Internal phy */
1366         case 1:
1367         case 2:
1368         case 3:
1369         case 4:
1370                 if (state->interface != PHY_INTERFACE_MODE_GMII)
1371                         return;
1372                 break;
1373         case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
1374                 if (priv->p5_interface == state->interface)
1375                         break;
1376                 if (!phy_interface_mode_is_rgmii(state->interface) &&
1377                     state->interface != PHY_INTERFACE_MODE_MII &&
1378                     state->interface != PHY_INTERFACE_MODE_GMII)
1379                         return;
1380
1381                 mt7530_setup_port5(ds, state->interface);
1382                 break;
1383         case 6: /* 1st cpu port */
1384                 if (priv->p6_interface == state->interface)
1385                         break;
1386
1387                 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
1388                     state->interface != PHY_INTERFACE_MODE_TRGMII)
1389                         return;
1390
1391                 /* Setup TX circuit incluing relevant PAD and driving */
1392                 mt7530_pad_clk_setup(ds, state->interface);
1393
1394                 priv->p6_interface = state->interface;
1395                 break;
1396         default:
1397                 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
1398                 return;
1399         }
1400
1401         if (phylink_autoneg_inband(mode)) {
1402                 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
1403                         __func__);
1404                 return;
1405         }
1406
1407         mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
1408         mcr_new = mcr_cur;
1409         mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
1410         mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
1411                    PMCR_BACKPR_EN | PMCR_FORCE_MODE;
1412
1413         /* Are we connected to external phy */
1414         if (port == 5 && dsa_is_user_port(ds, 5))
1415                 mcr_new |= PMCR_EXT_PHY;
1416
1417         if (mcr_new != mcr_cur)
1418                 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
1419 }
1420
1421 static void mt7530_phylink_mac_link_down(struct dsa_switch *ds, int port,
1422                                          unsigned int mode,
1423                                          phy_interface_t interface)
1424 {
1425         struct mt7530_priv *priv = ds->priv;
1426
1427         mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1428 }
1429
1430 static void mt7530_phylink_mac_link_up(struct dsa_switch *ds, int port,
1431                                        unsigned int mode,
1432                                        phy_interface_t interface,
1433                                        struct phy_device *phydev,
1434                                        int speed, int duplex,
1435                                        bool tx_pause, bool rx_pause)
1436 {
1437         struct mt7530_priv *priv = ds->priv;
1438         u32 mcr;
1439
1440         mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
1441
1442         switch (speed) {
1443         case SPEED_1000:
1444                 mcr |= PMCR_FORCE_SPEED_1000;
1445                 break;
1446         case SPEED_100:
1447                 mcr |= PMCR_FORCE_SPEED_100;
1448                 break;
1449         }
1450         if (duplex == DUPLEX_FULL) {
1451                 mcr |= PMCR_FORCE_FDX;
1452                 if (tx_pause)
1453                         mcr |= PMCR_TX_FC_EN;
1454                 if (rx_pause)
1455                         mcr |= PMCR_RX_FC_EN;
1456         }
1457
1458         mt7530_set(priv, MT7530_PMCR_P(port), mcr);
1459 }
1460
1461 static void mt7530_phylink_validate(struct dsa_switch *ds, int port,
1462                                     unsigned long *supported,
1463                                     struct phylink_link_state *state)
1464 {
1465         __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1466
1467         switch (port) {
1468         case 0: /* Internal phy */
1469         case 1:
1470         case 2:
1471         case 3:
1472         case 4:
1473                 if (state->interface != PHY_INTERFACE_MODE_NA &&
1474                     state->interface != PHY_INTERFACE_MODE_GMII)
1475                         goto unsupported;
1476                 break;
1477         case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
1478                 if (state->interface != PHY_INTERFACE_MODE_NA &&
1479                     !phy_interface_mode_is_rgmii(state->interface) &&
1480                     state->interface != PHY_INTERFACE_MODE_MII &&
1481                     state->interface != PHY_INTERFACE_MODE_GMII)
1482                         goto unsupported;
1483                 break;
1484         case 6: /* 1st cpu port */
1485                 if (state->interface != PHY_INTERFACE_MODE_NA &&
1486                     state->interface != PHY_INTERFACE_MODE_RGMII &&
1487                     state->interface != PHY_INTERFACE_MODE_TRGMII)
1488                         goto unsupported;
1489                 break;
1490         default:
1491                 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
1492 unsupported:
1493                 linkmode_zero(supported);
1494                 return;
1495         }
1496
1497         phylink_set_port_modes(mask);
1498         phylink_set(mask, Autoneg);
1499
1500         if (state->interface == PHY_INTERFACE_MODE_TRGMII) {
1501                 phylink_set(mask, 1000baseT_Full);
1502         } else {
1503                 phylink_set(mask, 10baseT_Half);
1504                 phylink_set(mask, 10baseT_Full);
1505                 phylink_set(mask, 100baseT_Half);
1506                 phylink_set(mask, 100baseT_Full);
1507
1508                 if (state->interface != PHY_INTERFACE_MODE_MII) {
1509                         phylink_set(mask, 1000baseT_Half);
1510                         phylink_set(mask, 1000baseT_Full);
1511                         if (port == 5)
1512                                 phylink_set(mask, 1000baseX_Full);
1513                 }
1514         }
1515
1516         phylink_set(mask, Pause);
1517         phylink_set(mask, Asym_Pause);
1518
1519         linkmode_and(supported, supported, mask);
1520         linkmode_and(state->advertising, state->advertising, mask);
1521 }
1522
1523 static int
1524 mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
1525                               struct phylink_link_state *state)
1526 {
1527         struct mt7530_priv *priv = ds->priv;
1528         u32 pmsr;
1529
1530         if (port < 0 || port >= MT7530_NUM_PORTS)
1531                 return -EINVAL;
1532
1533         pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
1534
1535         state->link = (pmsr & PMSR_LINK);
1536         state->an_complete = state->link;
1537         state->duplex = !!(pmsr & PMSR_DPX);
1538
1539         switch (pmsr & PMSR_SPEED_MASK) {
1540         case PMSR_SPEED_10:
1541                 state->speed = SPEED_10;
1542                 break;
1543         case PMSR_SPEED_100:
1544                 state->speed = SPEED_100;
1545                 break;
1546         case PMSR_SPEED_1000:
1547                 state->speed = SPEED_1000;
1548                 break;
1549         default:
1550                 state->speed = SPEED_UNKNOWN;
1551                 break;
1552         }
1553
1554         state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
1555         if (pmsr & PMSR_RX_FC)
1556                 state->pause |= MLO_PAUSE_RX;
1557         if (pmsr & PMSR_TX_FC)
1558                 state->pause |= MLO_PAUSE_TX;
1559
1560         return 1;
1561 }
1562
1563 static const struct dsa_switch_ops mt7530_switch_ops = {
1564         .get_tag_protocol       = mtk_get_tag_protocol,
1565         .setup                  = mt7530_setup,
1566         .get_strings            = mt7530_get_strings,
1567         .phy_read               = mt7530_phy_read,
1568         .phy_write              = mt7530_phy_write,
1569         .get_ethtool_stats      = mt7530_get_ethtool_stats,
1570         .get_sset_count         = mt7530_get_sset_count,
1571         .port_enable            = mt7530_port_enable,
1572         .port_disable           = mt7530_port_disable,
1573         .port_stp_state_set     = mt7530_stp_state_set,
1574         .port_bridge_join       = mt7530_port_bridge_join,
1575         .port_bridge_leave      = mt7530_port_bridge_leave,
1576         .port_fdb_add           = mt7530_port_fdb_add,
1577         .port_fdb_del           = mt7530_port_fdb_del,
1578         .port_fdb_dump          = mt7530_port_fdb_dump,
1579         .port_vlan_filtering    = mt7530_port_vlan_filtering,
1580         .port_vlan_prepare      = mt7530_port_vlan_prepare,
1581         .port_vlan_add          = mt7530_port_vlan_add,
1582         .port_vlan_del          = mt7530_port_vlan_del,
1583         .port_mirror_add        = mt7530_port_mirror_add,
1584         .port_mirror_del        = mt7530_port_mirror_del,
1585         .phylink_validate       = mt7530_phylink_validate,
1586         .phylink_mac_link_state = mt7530_phylink_mac_link_state,
1587         .phylink_mac_config     = mt7530_phylink_mac_config,
1588         .phylink_mac_link_down  = mt7530_phylink_mac_link_down,
1589         .phylink_mac_link_up    = mt7530_phylink_mac_link_up,
1590 };
1591
1592 static const struct of_device_id mt7530_of_match[] = {
1593         { .compatible = "mediatek,mt7621", .data = (void *)ID_MT7621, },
1594         { .compatible = "mediatek,mt7530", .data = (void *)ID_MT7530, },
1595         { /* sentinel */ },
1596 };
1597 MODULE_DEVICE_TABLE(of, mt7530_of_match);
1598
1599 static int
1600 mt7530_probe(struct mdio_device *mdiodev)
1601 {
1602         struct mt7530_priv *priv;
1603         struct device_node *dn;
1604
1605         dn = mdiodev->dev.of_node;
1606
1607         priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1608         if (!priv)
1609                 return -ENOMEM;
1610
1611         priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
1612         if (!priv->ds)
1613                 return -ENOMEM;
1614
1615         priv->ds->dev = &mdiodev->dev;
1616         priv->ds->num_ports = DSA_MAX_PORTS;
1617
1618         /* Use medatek,mcm property to distinguish hardware type that would
1619          * casues a little bit differences on power-on sequence.
1620          */
1621         priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1622         if (priv->mcm) {
1623                 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1624
1625                 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1626                 if (IS_ERR(priv->rstc)) {
1627                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1628                         return PTR_ERR(priv->rstc);
1629                 }
1630         }
1631
1632         /* Get the hardware identifier from the devicetree node.
1633          * We will need it for some of the clock and regulator setup.
1634          */
1635         priv->id = (unsigned int)(unsigned long)
1636                 of_device_get_match_data(&mdiodev->dev);
1637
1638         if (priv->id == ID_MT7530) {
1639                 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1640                 if (IS_ERR(priv->core_pwr))
1641                         return PTR_ERR(priv->core_pwr);
1642
1643                 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1644                 if (IS_ERR(priv->io_pwr))
1645                         return PTR_ERR(priv->io_pwr);
1646         }
1647
1648         /* Not MCM that indicates switch works as the remote standalone
1649          * integrated circuit so the GPIO pin would be used to complete
1650          * the reset, otherwise memory-mapped register accessing used
1651          * through syscon provides in the case of MCM.
1652          */
1653         if (!priv->mcm) {
1654                 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1655                                                       GPIOD_OUT_LOW);
1656                 if (IS_ERR(priv->reset)) {
1657                         dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1658                         return PTR_ERR(priv->reset);
1659                 }
1660         }
1661
1662         priv->bus = mdiodev->bus;
1663         priv->dev = &mdiodev->dev;
1664         priv->ds->priv = priv;
1665         priv->ds->ops = &mt7530_switch_ops;
1666         mutex_init(&priv->reg_mutex);
1667         dev_set_drvdata(&mdiodev->dev, priv);
1668
1669         return dsa_register_switch(priv->ds);
1670 }
1671
1672 static void
1673 mt7530_remove(struct mdio_device *mdiodev)
1674 {
1675         struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1676         int ret = 0;
1677
1678         ret = regulator_disable(priv->core_pwr);
1679         if (ret < 0)
1680                 dev_err(priv->dev,
1681                         "Failed to disable core power: %d\n", ret);
1682
1683         ret = regulator_disable(priv->io_pwr);
1684         if (ret < 0)
1685                 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1686                         ret);
1687
1688         dsa_unregister_switch(priv->ds);
1689         mutex_destroy(&priv->reg_mutex);
1690 }
1691
1692 static struct mdio_driver mt7530_mdio_driver = {
1693         .probe  = mt7530_probe,
1694         .remove = mt7530_remove,
1695         .mdiodrv.driver = {
1696                 .name = "mt7530",
1697                 .of_match_table = mt7530_of_match,
1698         },
1699 };
1700
1701 mdio_module_driver(mt7530_mdio_driver);
1702
1703 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1704 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1705 MODULE_LICENSE("GPL");