blob: 34e4aadfa705bdaef1747f95061caf54adfa4b80 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Sean Wangb8f126a2017-04-07 16:45:09 +08002/*
3 * Mediatek MT7530 DSA Switch driver
4 * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
Sean Wangb8f126a2017-04-07 16:45:09 +08005 */
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>
Sean Wangb8f126a2017-04-07 16:45:09 +080013#include <linux/of_mdio.h>
14#include <linux/of_net.h>
15#include <linux/of_platform.h>
René van Dorstca366d62019-09-02 15:02:24 +020016#include <linux/phylink.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080017#include <linux/regmap.h>
18#include <linux/regulator/consumer.h>
19#include <linux/reset.h>
Florian Fainellieb976a52017-04-08 08:52:02 -070020#include <linux/gpio/consumer.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080021#include <net/dsa.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080022
23#include "mt7530.h"
24
25/* String, offset, and register size in bytes if different from 4 bytes */
26static 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
69static int
Sean Wangb8f126a2017-04-07 16:45:09 +080070core_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;
94err:
95 dev_err(&bus->dev, "failed to read mmd register\n");
96
97 return ret;
98}
99
100static int
101core_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);
124err:
125 if (ret < 0)
126 dev_err(&bus->dev,
127 "failed to write mmd register\n");
128 return ret;
129}
130
131static void
132core_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
143static void
144core_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
159static void
160core_set(struct mt7530_priv *priv, u32 reg, u32 val)
161{
162 core_rmw(priv, reg, 0, val);
163}
164
165static void
166core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
167{
168 core_rmw(priv, reg, val, 0);
169}
170
171static int
172mt7530_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);
193err:
194 if (ret < 0)
195 dev_err(&bus->dev,
196 "failed to write mt7530 register\n");
197 return ret;
198}
199
200static u32
201mt7530_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
224static void
225mt7530_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
236static 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
251static u32
252mt7530_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
260static void
261mt7530_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
277static void
278mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
279{
280 mt7530_rmw(priv, reg, 0, val);
281}
282
283static void
284mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
285{
286 mt7530_rmw(priv, reg, val, 0);
287}
288
289static int
290mt7530_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
321static void
322mt7530_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
347static void
348mt7530_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
375static int
376mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
377{
378 struct mt7530_priv *priv = ds->priv;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200379 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 }
Sean Wangb8f126a2017-04-07 16:45:09 +0800389
390 switch (mode) {
391 case PHY_INTERFACE_MODE_RGMII:
392 trgint = 0;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200393 /* PLL frequency: 125MHz */
Sean Wangb8f126a2017-04-07 16:45:09 +0800394 ncpo1 = 0x0c80;
Sean Wangb8f126a2017-04-07 16:45:09 +0800395 break;
396 case PHY_INTERFACE_MODE_TRGMII:
397 trgint = 1;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200398 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 }
Sean Wangb8f126a2017-04-07 16:45:09 +0800410 break;
411 default:
412 dev_err(priv->dev, "xMII mode %d not supported\n", mode);
413 return -EINVAL;
414 }
415
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200416 if (xtal == HWTRAP_XTAL_25MHZ)
417 ssc_delta = 0x57;
418 else
419 ssc_delta = 0x87;
420
Sean Wangb8f126a2017-04-07 16:45:09 +0800421 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));
Sean Wangb8f126a2017-04-07 16:45:09 +0800481 return 0;
482}
483
484static void
485mt7530_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
Sean Wangb8f126a2017-04-07 16:45:09 +0800493static 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
Colin Ian King360cc342017-10-03 11:46:33 +0100500static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
501 u16 val)
Sean Wangb8f126a2017-04-07 16:45:09 +0800502{
503 struct mt7530_priv *priv = ds->priv;
504
505 return mdiobus_write_nested(priv->bus, port, regnum, val);
506}
507
508static void
Florian Fainelli89f09042018-04-25 12:12:50 -0700509mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
510 uint8_t *data)
Sean Wangb8f126a2017-04-07 16:45:09 +0800511{
512 int i;
513
Florian Fainelli89f09042018-04-25 12:12:50 -0700514 if (stringset != ETH_SS_STATS)
515 return;
516
Sean Wangb8f126a2017-04-07 16:45:09 +0800517 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
522static void
523mt7530_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
543static int
Florian Fainelli89f09042018-04-25 12:12:50 -0700544mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
Sean Wangb8f126a2017-04-07 16:45:09 +0800545{
Florian Fainelli89f09042018-04-25 12:12:50 -0700546 if (sset != ETH_SS_STATS)
547 return 0;
548
Sean Wangb8f126a2017-04-07 16:45:09 +0800549 return ARRAY_SIZE(mt7530_mib);
550}
551
René van Dorst38f790a2019-09-02 15:02:26 +0200552static 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
619unlock_exit:
620 mutex_unlock(&priv->reg_mutex);
621}
622
Sean Wangb8f126a2017-04-07 16:45:09 +0800623static int
624mt7530_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
DENG Qingfang5e5502e2020-05-13 23:10:16 +0800631 /* Unknown multicast frame forwarding to the cpu port */
632 mt7530_rmw(priv, MT7530_MFC, UNM_FFP_MASK, UNM_FFP(BIT(port)));
Sean Wangb8f126a2017-04-07 16:45:09 +0800633
Greg Ungererddda1ac2019-01-30 11:24:05 +1000634 /* Set CPU port number */
635 if (priv->id == ID_MT7621)
636 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
637
Sean Wangb8f126a2017-04-07 16:45:09 +0800638 /* CPU port gets connected to all user ports of
639 * the switch
640 */
641 mt7530_write(priv, MT7530_PCR_P(port),
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400642 PCR_MATRIX(dsa_user_ports(priv->ds)));
Sean Wangb8f126a2017-04-07 16:45:09 +0800643
644 return 0;
645}
646
647static int
648mt7530_port_enable(struct dsa_switch *ds, int port,
649 struct phy_device *phy)
650{
651 struct mt7530_priv *priv = ds->priv;
652
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400653 if (!dsa_is_user_port(ds, port))
654 return 0;
655
Sean Wangb8f126a2017-04-07 16:45:09 +0800656 mutex_lock(&priv->reg_mutex);
657
Sean Wangb8f126a2017-04-07 16:45:09 +0800658 /* Allow the user port gets connected to the cpu port and also
659 * restore the port matrix if the port is the member of a certain
660 * bridge.
661 */
662 priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
663 priv->ports[port].enable = true;
664 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
665 priv->ports[port].pm);
René van Dorst1d011452020-03-27 15:44:12 +0100666 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
Sean Wangb8f126a2017-04-07 16:45:09 +0800667
668 mutex_unlock(&priv->reg_mutex);
669
670 return 0;
671}
672
673static void
Andrew Lunn75104db2019-02-24 20:44:43 +0100674mt7530_port_disable(struct dsa_switch *ds, int port)
Sean Wangb8f126a2017-04-07 16:45:09 +0800675{
676 struct mt7530_priv *priv = ds->priv;
677
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400678 if (!dsa_is_user_port(ds, port))
679 return;
680
Sean Wangb8f126a2017-04-07 16:45:09 +0800681 mutex_lock(&priv->reg_mutex);
682
683 /* Clear up all port matrix which could be restored in the next
684 * enablement for the port.
685 */
686 priv->ports[port].enable = false;
687 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
688 PCR_MATRIX_CLR);
René van Dorst1d011452020-03-27 15:44:12 +0100689 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
Sean Wangb8f126a2017-04-07 16:45:09 +0800690
691 mutex_unlock(&priv->reg_mutex);
692}
693
694static void
695mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
696{
697 struct mt7530_priv *priv = ds->priv;
698 u32 stp_state;
699
700 switch (state) {
701 case BR_STATE_DISABLED:
702 stp_state = MT7530_STP_DISABLED;
703 break;
704 case BR_STATE_BLOCKING:
705 stp_state = MT7530_STP_BLOCKING;
706 break;
707 case BR_STATE_LISTENING:
708 stp_state = MT7530_STP_LISTENING;
709 break;
710 case BR_STATE_LEARNING:
711 stp_state = MT7530_STP_LEARNING;
712 break;
713 case BR_STATE_FORWARDING:
714 default:
715 stp_state = MT7530_STP_FORWARDING;
716 break;
717 }
718
719 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
720}
721
722static int
723mt7530_port_bridge_join(struct dsa_switch *ds, int port,
724 struct net_device *bridge)
725{
726 struct mt7530_priv *priv = ds->priv;
727 u32 port_bitmap = BIT(MT7530_CPU_PORT);
728 int i;
729
730 mutex_lock(&priv->reg_mutex);
731
732 for (i = 0; i < MT7530_NUM_PORTS; i++) {
733 /* Add this port to the port matrix of the other ports in the
734 * same bridge. If the port is disabled, port matrix is kept
735 * and not being setup until the port becomes enabled.
736 */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400737 if (dsa_is_user_port(ds, i) && i != port) {
Vivien Didelotc8652c82017-10-16 11:12:19 -0400738 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +0800739 continue;
740 if (priv->ports[i].enable)
741 mt7530_set(priv, MT7530_PCR_P(i),
742 PCR_MATRIX(BIT(port)));
743 priv->ports[i].pm |= PCR_MATRIX(BIT(port));
744
745 port_bitmap |= BIT(i);
746 }
747 }
748
749 /* Add the all other ports to this port matrix. */
750 if (priv->ports[port].enable)
751 mt7530_rmw(priv, MT7530_PCR_P(port),
752 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
753 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
754
755 mutex_unlock(&priv->reg_mutex);
756
757 return 0;
758}
759
760static void
Sean Wang83163f72017-12-15 12:47:00 +0800761mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
762{
763 struct mt7530_priv *priv = ds->priv;
764 bool all_user_ports_removed = true;
765 int i;
766
767 /* When a port is removed from the bridge, the port would be set up
768 * back to the default as is at initial boot which is a VLAN-unaware
769 * port.
770 */
771 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
772 MT7530_PORT_MATRIX_MODE);
DENG Qingfange0451242020-04-14 14:34:08 +0800773 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
774 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
775 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +0800776
Sean Wang83163f72017-12-15 12:47:00 +0800777 for (i = 0; i < MT7530_NUM_PORTS; i++) {
778 if (dsa_is_user_port(ds, i) &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400779 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Sean Wang83163f72017-12-15 12:47:00 +0800780 all_user_ports_removed = false;
781 break;
782 }
783 }
784
785 /* CPU port also does the same thing until all user ports belonging to
786 * the CPU port get out of VLAN filtering mode.
787 */
788 if (all_user_ports_removed) {
789 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
790 PCR_MATRIX(dsa_user_ports(priv->ds)));
DENG Qingfange0451242020-04-14 14:34:08 +0800791 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
792 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +0800793 }
794}
795
796static void
797mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
798{
799 struct mt7530_priv *priv = ds->priv;
800
801 /* The real fabric path would be decided on the membership in the
802 * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS
803 * means potential VLAN can be consisting of certain subset of all
804 * ports.
805 */
806 mt7530_rmw(priv, MT7530_PCR_P(port),
807 PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS));
808
809 /* Trapped into security mode allows packet forwarding through VLAN
810 * table lookup.
811 */
812 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
813 MT7530_PORT_SECURITY_MODE);
814
815 /* Set the port as a user port which is to be able to recognize VID
816 * from incoming packets before fetching entry within the VLAN table.
817 */
DENG Qingfange0451242020-04-14 14:34:08 +0800818 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
819 VLAN_ATTR(MT7530_VLAN_USER) |
820 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
Sean Wang83163f72017-12-15 12:47:00 +0800821}
822
823static void
Sean Wangb8f126a2017-04-07 16:45:09 +0800824mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
825 struct net_device *bridge)
826{
827 struct mt7530_priv *priv = ds->priv;
828 int i;
829
830 mutex_lock(&priv->reg_mutex);
831
832 for (i = 0; i < MT7530_NUM_PORTS; i++) {
833 /* Remove this port from the port matrix of the other ports
834 * in the same bridge. If the port is disabled, port matrix
835 * is kept and not being setup until the port becomes enabled.
Sean Wang83163f72017-12-15 12:47:00 +0800836 * And the other port's port matrix cannot be broken when the
837 * other port is still a VLAN-aware port.
Sean Wangb8f126a2017-04-07 16:45:09 +0800838 */
Vladimir Oltean2a130552019-04-28 21:45:50 +0300839 if (dsa_is_user_port(ds, i) && i != port &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400840 !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Vivien Didelotc8652c82017-10-16 11:12:19 -0400841 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +0800842 continue;
843 if (priv->ports[i].enable)
844 mt7530_clear(priv, MT7530_PCR_P(i),
845 PCR_MATRIX(BIT(port)));
846 priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
847 }
848 }
849
850 /* Set the cpu port to be the only one in the port matrix of
851 * this port.
852 */
853 if (priv->ports[port].enable)
854 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
855 PCR_MATRIX(BIT(MT7530_CPU_PORT)));
856 priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
857
858 mutex_unlock(&priv->reg_mutex);
859}
860
861static int
Sean Wangb8f126a2017-04-07 16:45:09 +0800862mt7530_port_fdb_add(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +0300863 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +0800864{
865 struct mt7530_priv *priv = ds->priv;
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300866 int ret;
Sean Wangb8f126a2017-04-07 16:45:09 +0800867 u8 port_mask = BIT(port);
868
869 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +0300870 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
Florian Fainelli18bd5942018-04-02 16:24:14 -0700871 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +0800872 mutex_unlock(&priv->reg_mutex);
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300873
874 return ret;
Sean Wangb8f126a2017-04-07 16:45:09 +0800875}
876
877static int
878mt7530_port_fdb_del(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +0300879 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +0800880{
881 struct mt7530_priv *priv = ds->priv;
882 int ret;
883 u8 port_mask = BIT(port);
884
885 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +0300886 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
Florian Fainelli18bd5942018-04-02 16:24:14 -0700887 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +0800888 mutex_unlock(&priv->reg_mutex);
889
890 return ret;
891}
892
893static int
894mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300895 dsa_fdb_dump_cb_t *cb, void *data)
Sean Wangb8f126a2017-04-07 16:45:09 +0800896{
897 struct mt7530_priv *priv = ds->priv;
898 struct mt7530_fdb _fdb = { 0 };
899 int cnt = MT7530_NUM_FDB_RECORDS;
900 int ret = 0;
901 u32 rsp = 0;
902
903 mutex_lock(&priv->reg_mutex);
904
905 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
906 if (ret < 0)
907 goto err;
908
909 do {
910 if (rsp & ATC_SRCH_HIT) {
911 mt7530_fdb_read(priv, &_fdb);
912 if (_fdb.port_mask & BIT(port)) {
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300913 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
914 data);
Sean Wangb8f126a2017-04-07 16:45:09 +0800915 if (ret < 0)
916 break;
917 }
918 }
919 } while (--cnt &&
920 !(rsp & ATC_SRCH_END) &&
921 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
922err:
923 mutex_unlock(&priv->reg_mutex);
924
925 return 0;
926}
927
Sean Wang83163f72017-12-15 12:47:00 +0800928static int
929mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
930{
931 struct mt7530_dummy_poll p;
932 u32 val;
933 int ret;
934
935 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
936 mt7530_write(priv, MT7530_VTCR, val);
937
938 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
939 ret = readx_poll_timeout(_mt7530_read, &p, val,
940 !(val & VTCR_BUSY), 20, 20000);
941 if (ret < 0) {
942 dev_err(priv->dev, "poll timeout\n");
943 return ret;
944 }
945
946 val = mt7530_read(priv, MT7530_VTCR);
947 if (val & VTCR_INVALID) {
948 dev_err(priv->dev, "read VTCR invalid\n");
949 return -EINVAL;
950 }
951
952 return 0;
953}
954
955static int
956mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
957 bool vlan_filtering)
958{
Sean Wang83163f72017-12-15 12:47:00 +0800959 if (vlan_filtering) {
960 /* The port is being kept as VLAN-unaware port when bridge is
961 * set up with vlan_filtering not being set, Otherwise, the
962 * port and the corresponding CPU port is required the setup
963 * for becoming a VLAN-aware port.
964 */
965 mt7530_port_set_vlan_aware(ds, port);
966 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
Vladimir Olteane3ee07d2019-04-28 21:45:47 +0300967 } else {
968 mt7530_port_set_vlan_unaware(ds, port);
Sean Wang83163f72017-12-15 12:47:00 +0800969 }
970
971 return 0;
972}
973
974static int
975mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
976 const struct switchdev_obj_port_vlan *vlan)
977{
978 /* nothing needed */
979
980 return 0;
981}
982
983static void
984mt7530_hw_vlan_add(struct mt7530_priv *priv,
985 struct mt7530_hw_vlan_entry *entry)
986{
987 u8 new_members;
988 u32 val;
989
990 new_members = entry->old_members | BIT(entry->port) |
991 BIT(MT7530_CPU_PORT);
992
993 /* Validate the entry with independent learning, create egress tag per
994 * VLAN and joining the port as one of the port members.
995 */
996 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
997 mt7530_write(priv, MT7530_VAWD1, val);
998
999 /* Decide whether adding tag or not for those outgoing packets from the
1000 * port inside the VLAN.
1001 */
1002 val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1003 MT7530_VLAN_EGRESS_TAG;
1004 mt7530_rmw(priv, MT7530_VAWD2,
1005 ETAG_CTRL_P_MASK(entry->port),
1006 ETAG_CTRL_P(entry->port, val));
1007
1008 /* CPU port is always taken as a tagged port for serving more than one
1009 * VLANs across and also being applied with egress type stack mode for
1010 * that VLAN tags would be appended after hardware special tag used as
1011 * DSA tag.
1012 */
1013 mt7530_rmw(priv, MT7530_VAWD2,
1014 ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1015 ETAG_CTRL_P(MT7530_CPU_PORT,
1016 MT7530_VLAN_EGRESS_STACK));
1017}
1018
1019static void
1020mt7530_hw_vlan_del(struct mt7530_priv *priv,
1021 struct mt7530_hw_vlan_entry *entry)
1022{
1023 u8 new_members;
1024 u32 val;
1025
1026 new_members = entry->old_members & ~BIT(entry->port);
1027
1028 val = mt7530_read(priv, MT7530_VAWD1);
1029 if (!(val & VLAN_VALID)) {
1030 dev_err(priv->dev,
1031 "Cannot be deleted due to invalid entry\n");
1032 return;
1033 }
1034
1035 /* If certain member apart from CPU port is still alive in the VLAN,
1036 * the entry would be kept valid. Otherwise, the entry is got to be
1037 * disabled.
1038 */
1039 if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1040 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1041 VLAN_VALID;
1042 mt7530_write(priv, MT7530_VAWD1, val);
1043 } else {
1044 mt7530_write(priv, MT7530_VAWD1, 0);
1045 mt7530_write(priv, MT7530_VAWD2, 0);
1046 }
1047}
1048
1049static void
1050mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1051 struct mt7530_hw_vlan_entry *entry,
1052 mt7530_vlan_op vlan_op)
1053{
1054 u32 val;
1055
1056 /* Fetch entry */
1057 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1058
1059 val = mt7530_read(priv, MT7530_VAWD1);
1060
1061 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1062
1063 /* Manipulate entry */
1064 vlan_op(priv, entry);
1065
1066 /* Flush result to hardware */
1067 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1068}
1069
1070static void
1071mt7530_port_vlan_add(struct dsa_switch *ds, int port,
1072 const struct switchdev_obj_port_vlan *vlan)
1073{
1074 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1075 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1076 struct mt7530_hw_vlan_entry new_entry;
1077 struct mt7530_priv *priv = ds->priv;
1078 u16 vid;
1079
1080 /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1081 * being set.
1082 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001083 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
Sean Wang83163f72017-12-15 12:47:00 +08001084 return;
1085
1086 mutex_lock(&priv->reg_mutex);
1087
1088 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1089 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1090 mt7530_hw_vlan_update(priv, vid, &new_entry,
1091 mt7530_hw_vlan_add);
1092 }
1093
1094 if (pvid) {
1095 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1096 G0_PORT_VID(vlan->vid_end));
1097 priv->ports[port].pvid = vlan->vid_end;
1098 }
1099
1100 mutex_unlock(&priv->reg_mutex);
1101}
1102
1103static int
1104mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1105 const struct switchdev_obj_port_vlan *vlan)
1106{
1107 struct mt7530_hw_vlan_entry target_entry;
1108 struct mt7530_priv *priv = ds->priv;
1109 u16 vid, pvid;
1110
1111 /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
1112 * being set.
1113 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001114 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
Sean Wang83163f72017-12-15 12:47:00 +08001115 return 0;
1116
1117 mutex_lock(&priv->reg_mutex);
1118
1119 pvid = priv->ports[port].pvid;
1120 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1121 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1122 mt7530_hw_vlan_update(priv, vid, &target_entry,
1123 mt7530_hw_vlan_del);
1124
1125 /* PVID is being restored to the default whenever the PVID port
1126 * is being removed from the VLAN.
1127 */
1128 if (pvid == vid)
1129 pvid = G0_PORT_VID_DEF;
1130 }
1131
1132 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
1133 priv->ports[port].pvid = pvid;
1134
1135 mutex_unlock(&priv->reg_mutex);
1136
1137 return 0;
1138}
1139
DENG Qingfang37feab62020-03-06 20:35:35 +08001140static int mt7530_port_mirror_add(struct dsa_switch *ds, int port,
1141 struct dsa_mall_mirror_tc_entry *mirror,
1142 bool ingress)
1143{
1144 struct mt7530_priv *priv = ds->priv;
1145 u32 val;
1146
1147 /* Check for existent entry */
1148 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1149 return -EEXIST;
1150
1151 val = mt7530_read(priv, MT7530_MFC);
1152
1153 /* MT7530 only supports one monitor port */
1154 if (val & MIRROR_EN && MIRROR_PORT(val) != mirror->to_local_port)
1155 return -EEXIST;
1156
1157 val |= MIRROR_EN;
1158 val &= ~MIRROR_MASK;
1159 val |= mirror->to_local_port;
1160 mt7530_write(priv, MT7530_MFC, val);
1161
1162 val = mt7530_read(priv, MT7530_PCR_P(port));
1163 if (ingress) {
1164 val |= PORT_RX_MIR;
1165 priv->mirror_rx |= BIT(port);
1166 } else {
1167 val |= PORT_TX_MIR;
1168 priv->mirror_tx |= BIT(port);
1169 }
1170 mt7530_write(priv, MT7530_PCR_P(port), val);
1171
1172 return 0;
1173}
1174
1175static void mt7530_port_mirror_del(struct dsa_switch *ds, int port,
1176 struct dsa_mall_mirror_tc_entry *mirror)
1177{
1178 struct mt7530_priv *priv = ds->priv;
1179 u32 val;
1180
1181 val = mt7530_read(priv, MT7530_PCR_P(port));
1182 if (mirror->ingress) {
1183 val &= ~PORT_RX_MIR;
1184 priv->mirror_rx &= ~BIT(port);
1185 } else {
1186 val &= ~PORT_TX_MIR;
1187 priv->mirror_tx &= ~BIT(port);
1188 }
1189 mt7530_write(priv, MT7530_PCR_P(port), val);
1190
1191 if (!priv->mirror_rx && !priv->mirror_tx) {
1192 val = mt7530_read(priv, MT7530_MFC);
1193 val &= ~MIRROR_EN;
1194 mt7530_write(priv, MT7530_MFC, val);
1195 }
1196}
1197
Sean Wangb8f126a2017-04-07 16:45:09 +08001198static enum dsa_tag_protocol
Florian Fainelli4d776482020-01-07 21:06:05 -08001199mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1200 enum dsa_tag_protocol mp)
Sean Wangb8f126a2017-04-07 16:45:09 +08001201{
1202 struct mt7530_priv *priv = ds->priv;
1203
Florian Fainelli5ed4e3e2017-11-10 15:22:52 -08001204 if (port != MT7530_CPU_PORT) {
Sean Wangb8f126a2017-04-07 16:45:09 +08001205 dev_warn(priv->dev,
1206 "port not matched with tagging CPU port\n");
1207 return DSA_TAG_PROTO_NONE;
1208 } else {
1209 return DSA_TAG_PROTO_MTK;
1210 }
1211}
1212
1213static int
1214mt7530_setup(struct dsa_switch *ds)
1215{
1216 struct mt7530_priv *priv = ds->priv;
René van Dorst38f790a2019-09-02 15:02:26 +02001217 struct device_node *phy_node;
1218 struct device_node *mac_np;
Sean Wangb8f126a2017-04-07 16:45:09 +08001219 struct mt7530_dummy_poll p;
René van Dorst38f790a2019-09-02 15:02:26 +02001220 phy_interface_t interface;
René van Dorstca366d62019-09-02 15:02:24 +02001221 struct device_node *dn;
1222 u32 id, val;
1223 int ret, i;
Sean Wangb8f126a2017-04-07 16:45:09 +08001224
Vivien Didelot0abfd492017-09-20 12:28:05 -04001225 /* The parent node of master netdev which holds the common system
Sean Wangb8f126a2017-04-07 16:45:09 +08001226 * controller also is the container for two GMACs nodes representing
1227 * as two netdev instances.
1228 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001229 dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
Sean Wangb8f126a2017-04-07 16:45:09 +08001230
Greg Ungererddda1ac2019-01-30 11:24:05 +10001231 if (priv->id == ID_MT7530) {
Greg Ungererddda1ac2019-01-30 11:24:05 +10001232 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
1233 ret = regulator_enable(priv->core_pwr);
1234 if (ret < 0) {
1235 dev_err(priv->dev,
1236 "Failed to enable core power: %d\n", ret);
1237 return ret;
1238 }
1239
1240 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
1241 ret = regulator_enable(priv->io_pwr);
1242 if (ret < 0) {
1243 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
1244 ret);
1245 return ret;
1246 }
Sean Wangb8f126a2017-04-07 16:45:09 +08001247 }
1248
1249 /* Reset whole chip through gpio pin or memory-mapped registers for
1250 * different type of hardware
1251 */
1252 if (priv->mcm) {
1253 reset_control_assert(priv->rstc);
1254 usleep_range(1000, 1100);
1255 reset_control_deassert(priv->rstc);
1256 } else {
1257 gpiod_set_value_cansleep(priv->reset, 0);
1258 usleep_range(1000, 1100);
1259 gpiod_set_value_cansleep(priv->reset, 1);
1260 }
1261
1262 /* Waiting for MT7530 got to stable */
1263 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
1264 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
1265 20, 1000000);
1266 if (ret < 0) {
1267 dev_err(priv->dev, "reset timeout\n");
1268 return ret;
1269 }
1270
1271 id = mt7530_read(priv, MT7530_CREV);
1272 id >>= CHIP_NAME_SHIFT;
1273 if (id != MT7530_ID) {
1274 dev_err(priv->dev, "chip %x can't be supported\n", id);
1275 return -ENODEV;
1276 }
1277
1278 /* Reset the switch through internal reset */
1279 mt7530_write(priv, MT7530_SYS_CTRL,
1280 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
1281 SYS_CTRL_REG_RST);
1282
1283 /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
1284 val = mt7530_read(priv, MT7530_MHWTRAP);
1285 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
1286 val |= MHWTRAP_MANUAL;
1287 mt7530_write(priv, MT7530_MHWTRAP, val);
1288
René van Dorstca366d62019-09-02 15:02:24 +02001289 priv->p6_interface = PHY_INTERFACE_MODE_NA;
1290
Sean Wangb8f126a2017-04-07 16:45:09 +08001291 /* Enable and reset MIB counters */
1292 mt7530_mib_reset(ds);
1293
Sean Wangb8f126a2017-04-07 16:45:09 +08001294 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1295 /* Disable forwarding by default on all ports */
1296 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
1297 PCR_MATRIX_CLR);
1298
1299 if (dsa_is_cpu_port(ds, i))
1300 mt7530_cpu_port_enable(priv, i);
1301 else
Andrew Lunn75104db2019-02-24 20:44:43 +01001302 mt7530_port_disable(ds, i);
DENG Qingfange0451242020-04-14 14:34:08 +08001303
1304 /* Enable consistent egress tag */
1305 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
1306 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wangb8f126a2017-04-07 16:45:09 +08001307 }
1308
René van Dorst38f790a2019-09-02 15:02:26 +02001309 /* Setup port 5 */
1310 priv->p5_intf_sel = P5_DISABLED;
1311 interface = PHY_INTERFACE_MODE_NA;
1312
1313 if (!dsa_is_unused_port(ds, 5)) {
1314 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001315 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
1316 if (ret && ret != -ENODEV)
1317 return ret;
René van Dorst38f790a2019-09-02 15:02:26 +02001318 } else {
1319 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
1320 for_each_child_of_node(dn, mac_np) {
1321 if (!of_device_is_compatible(mac_np,
1322 "mediatek,eth-mac"))
1323 continue;
1324
1325 ret = of_property_read_u32(mac_np, "reg", &id);
1326 if (ret < 0 || id != 1)
1327 continue;
1328
1329 phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
Chuanhong Guo04528002020-04-03 19:28:24 +08001330 if (!phy_node)
1331 continue;
1332
René van Dorst38f790a2019-09-02 15:02:26 +02001333 if (phy_node->parent == priv->dev->of_node->parent) {
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001334 ret = of_get_phy_mode(mac_np, &interface);
1335 if (ret && ret != -ENODEV)
1336 return ret;
René van Dorst38f790a2019-09-02 15:02:26 +02001337 id = of_mdio_parse_addr(ds->dev, phy_node);
1338 if (id == 0)
1339 priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
1340 if (id == 4)
1341 priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
1342 }
1343 of_node_put(phy_node);
1344 break;
1345 }
1346 }
1347
1348 mt7530_setup_port5(ds, interface);
1349
Sean Wangb8f126a2017-04-07 16:45:09 +08001350 /* Flush the FDB table */
Florian Fainelli18bd5942018-04-02 16:24:14 -07001351 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001352 if (ret < 0)
1353 return ret;
1354
1355 return 0;
1356}
1357
René van Dorstca366d62019-09-02 15:02:24 +02001358static void mt7530_phylink_mac_config(struct dsa_switch *ds, int port,
1359 unsigned int mode,
1360 const struct phylink_link_state *state)
1361{
1362 struct mt7530_priv *priv = ds->priv;
1363 u32 mcr_cur, mcr_new;
1364
1365 switch (port) {
1366 case 0: /* Internal phy */
1367 case 1:
1368 case 2:
1369 case 3:
1370 case 4:
1371 if (state->interface != PHY_INTERFACE_MODE_GMII)
1372 return;
1373 break;
René van Dorst38f790a2019-09-02 15:02:26 +02001374 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
1375 if (priv->p5_interface == state->interface)
1376 break;
1377 if (!phy_interface_mode_is_rgmii(state->interface) &&
1378 state->interface != PHY_INTERFACE_MODE_MII &&
1379 state->interface != PHY_INTERFACE_MODE_GMII)
1380 return;
1381
1382 mt7530_setup_port5(ds, state->interface);
1383 break;
René van Dorstca366d62019-09-02 15:02:24 +02001384 case 6: /* 1st cpu port */
1385 if (priv->p6_interface == state->interface)
1386 break;
1387
1388 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
1389 state->interface != PHY_INTERFACE_MODE_TRGMII)
1390 return;
1391
1392 /* Setup TX circuit incluing relevant PAD and driving */
1393 mt7530_pad_clk_setup(ds, state->interface);
1394
René van Dorstca366d62019-09-02 15:02:24 +02001395 priv->p6_interface = state->interface;
1396 break;
1397 default:
1398 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
1399 return;
1400 }
1401
1402 if (phylink_autoneg_inband(mode)) {
1403 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
1404 __func__);
1405 return;
1406 }
1407
1408 mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
1409 mcr_new = mcr_cur;
René van Dorst1d011452020-03-27 15:44:12 +01001410 mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
René van Dorstca366d62019-09-02 15:02:24 +02001411 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
René van Dorst222594712020-03-19 14:47:56 +01001412 PMCR_BACKPR_EN | PMCR_FORCE_MODE;
René van Dorstca366d62019-09-02 15:02:24 +02001413
René van Dorst38f790a2019-09-02 15:02:26 +02001414 /* Are we connected to external phy */
1415 if (port == 5 && dsa_is_user_port(ds, 5))
1416 mcr_new |= PMCR_EXT_PHY;
1417
René van Dorstca366d62019-09-02 15:02:24 +02001418 if (mcr_new != mcr_cur)
1419 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
1420}
1421
1422static void mt7530_phylink_mac_link_down(struct dsa_switch *ds, int port,
1423 unsigned int mode,
1424 phy_interface_t interface)
1425{
1426 struct mt7530_priv *priv = ds->priv;
1427
René van Dorst1d011452020-03-27 15:44:12 +01001428 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
René van Dorstca366d62019-09-02 15:02:24 +02001429}
1430
1431static void mt7530_phylink_mac_link_up(struct dsa_switch *ds, int port,
1432 unsigned int mode,
1433 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00001434 struct phy_device *phydev,
1435 int speed, int duplex,
1436 bool tx_pause, bool rx_pause)
René van Dorstca366d62019-09-02 15:02:24 +02001437{
1438 struct mt7530_priv *priv = ds->priv;
René van Dorst1d011452020-03-27 15:44:12 +01001439 u32 mcr;
René van Dorstca366d62019-09-02 15:02:24 +02001440
René van Dorst1d011452020-03-27 15:44:12 +01001441 mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
1442
1443 switch (speed) {
1444 case SPEED_1000:
1445 mcr |= PMCR_FORCE_SPEED_1000;
1446 break;
1447 case SPEED_100:
1448 mcr |= PMCR_FORCE_SPEED_100;
1449 break;
1450 }
1451 if (duplex == DUPLEX_FULL) {
1452 mcr |= PMCR_FORCE_FDX;
1453 if (tx_pause)
1454 mcr |= PMCR_TX_FC_EN;
1455 if (rx_pause)
1456 mcr |= PMCR_RX_FC_EN;
1457 }
1458
1459 mt7530_set(priv, MT7530_PMCR_P(port), mcr);
René van Dorstca366d62019-09-02 15:02:24 +02001460}
1461
1462static void mt7530_phylink_validate(struct dsa_switch *ds, int port,
1463 unsigned long *supported,
1464 struct phylink_link_state *state)
1465{
1466 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1467
1468 switch (port) {
1469 case 0: /* Internal phy */
1470 case 1:
1471 case 2:
1472 case 3:
1473 case 4:
1474 if (state->interface != PHY_INTERFACE_MODE_NA &&
1475 state->interface != PHY_INTERFACE_MODE_GMII)
1476 goto unsupported;
1477 break;
René van Dorst38f790a2019-09-02 15:02:26 +02001478 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
1479 if (state->interface != PHY_INTERFACE_MODE_NA &&
1480 !phy_interface_mode_is_rgmii(state->interface) &&
1481 state->interface != PHY_INTERFACE_MODE_MII &&
1482 state->interface != PHY_INTERFACE_MODE_GMII)
1483 goto unsupported;
1484 break;
René van Dorstca366d62019-09-02 15:02:24 +02001485 case 6: /* 1st cpu port */
1486 if (state->interface != PHY_INTERFACE_MODE_NA &&
1487 state->interface != PHY_INTERFACE_MODE_RGMII &&
1488 state->interface != PHY_INTERFACE_MODE_TRGMII)
1489 goto unsupported;
1490 break;
1491 default:
1492 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
1493unsupported:
1494 linkmode_zero(supported);
1495 return;
1496 }
1497
1498 phylink_set_port_modes(mask);
1499 phylink_set(mask, Autoneg);
1500
René van Dorst38f790a2019-09-02 15:02:26 +02001501 if (state->interface == PHY_INTERFACE_MODE_TRGMII) {
1502 phylink_set(mask, 1000baseT_Full);
1503 } else {
René van Dorstca366d62019-09-02 15:02:24 +02001504 phylink_set(mask, 10baseT_Half);
1505 phylink_set(mask, 10baseT_Full);
1506 phylink_set(mask, 100baseT_Half);
1507 phylink_set(mask, 100baseT_Full);
René van Dorstca366d62019-09-02 15:02:24 +02001508
René van Dorst38f790a2019-09-02 15:02:26 +02001509 if (state->interface != PHY_INTERFACE_MODE_MII) {
1510 phylink_set(mask, 1000baseT_Half);
1511 phylink_set(mask, 1000baseT_Full);
1512 if (port == 5)
1513 phylink_set(mask, 1000baseX_Full);
1514 }
1515 }
René van Dorstca366d62019-09-02 15:02:24 +02001516
1517 phylink_set(mask, Pause);
1518 phylink_set(mask, Asym_Pause);
1519
1520 linkmode_and(supported, supported, mask);
1521 linkmode_and(state->advertising, state->advertising, mask);
1522}
1523
1524static int
1525mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
1526 struct phylink_link_state *state)
1527{
1528 struct mt7530_priv *priv = ds->priv;
1529 u32 pmsr;
1530
1531 if (port < 0 || port >= MT7530_NUM_PORTS)
1532 return -EINVAL;
1533
1534 pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
1535
1536 state->link = (pmsr & PMSR_LINK);
1537 state->an_complete = state->link;
1538 state->duplex = !!(pmsr & PMSR_DPX);
1539
1540 switch (pmsr & PMSR_SPEED_MASK) {
1541 case PMSR_SPEED_10:
1542 state->speed = SPEED_10;
1543 break;
1544 case PMSR_SPEED_100:
1545 state->speed = SPEED_100;
1546 break;
1547 case PMSR_SPEED_1000:
1548 state->speed = SPEED_1000;
1549 break;
1550 default:
1551 state->speed = SPEED_UNKNOWN;
1552 break;
1553 }
1554
1555 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
1556 if (pmsr & PMSR_RX_FC)
1557 state->pause |= MLO_PAUSE_RX;
1558 if (pmsr & PMSR_TX_FC)
1559 state->pause |= MLO_PAUSE_TX;
1560
1561 return 1;
1562}
1563
Bhumika Goyald78d6772017-08-09 10:34:15 +05301564static const struct dsa_switch_ops mt7530_switch_ops = {
Sean Wangb8f126a2017-04-07 16:45:09 +08001565 .get_tag_protocol = mtk_get_tag_protocol,
1566 .setup = mt7530_setup,
1567 .get_strings = mt7530_get_strings,
1568 .phy_read = mt7530_phy_read,
1569 .phy_write = mt7530_phy_write,
1570 .get_ethtool_stats = mt7530_get_ethtool_stats,
1571 .get_sset_count = mt7530_get_sset_count,
Sean Wangb8f126a2017-04-07 16:45:09 +08001572 .port_enable = mt7530_port_enable,
1573 .port_disable = mt7530_port_disable,
1574 .port_stp_state_set = mt7530_stp_state_set,
1575 .port_bridge_join = mt7530_port_bridge_join,
1576 .port_bridge_leave = mt7530_port_bridge_leave,
Sean Wangb8f126a2017-04-07 16:45:09 +08001577 .port_fdb_add = mt7530_port_fdb_add,
1578 .port_fdb_del = mt7530_port_fdb_del,
1579 .port_fdb_dump = mt7530_port_fdb_dump,
Sean Wang83163f72017-12-15 12:47:00 +08001580 .port_vlan_filtering = mt7530_port_vlan_filtering,
1581 .port_vlan_prepare = mt7530_port_vlan_prepare,
1582 .port_vlan_add = mt7530_port_vlan_add,
1583 .port_vlan_del = mt7530_port_vlan_del,
DENG Qingfang37feab62020-03-06 20:35:35 +08001584 .port_mirror_add = mt7530_port_mirror_add,
1585 .port_mirror_del = mt7530_port_mirror_del,
René van Dorstca366d62019-09-02 15:02:24 +02001586 .phylink_validate = mt7530_phylink_validate,
1587 .phylink_mac_link_state = mt7530_phylink_mac_link_state,
1588 .phylink_mac_config = mt7530_phylink_mac_config,
1589 .phylink_mac_link_down = mt7530_phylink_mac_link_down,
1590 .phylink_mac_link_up = mt7530_phylink_mac_link_up,
Sean Wangb8f126a2017-04-07 16:45:09 +08001591};
1592
Greg Ungererddda1ac2019-01-30 11:24:05 +10001593static const struct of_device_id mt7530_of_match[] = {
1594 { .compatible = "mediatek,mt7621", .data = (void *)ID_MT7621, },
1595 { .compatible = "mediatek,mt7530", .data = (void *)ID_MT7530, },
1596 { /* sentinel */ },
1597};
1598MODULE_DEVICE_TABLE(of, mt7530_of_match);
1599
Sean Wangb8f126a2017-04-07 16:45:09 +08001600static int
1601mt7530_probe(struct mdio_device *mdiodev)
1602{
1603 struct mt7530_priv *priv;
1604 struct device_node *dn;
1605
1606 dn = mdiodev->dev.of_node;
1607
1608 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1609 if (!priv)
1610 return -ENOMEM;
1611
Vivien Didelot7e99e342019-10-21 16:51:30 -04001612 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001613 if (!priv->ds)
1614 return -ENOMEM;
1615
Vivien Didelot7e99e342019-10-21 16:51:30 -04001616 priv->ds->dev = &mdiodev->dev;
1617 priv->ds->num_ports = DSA_MAX_PORTS;
1618
Sean Wangb8f126a2017-04-07 16:45:09 +08001619 /* Use medatek,mcm property to distinguish hardware type that would
1620 * casues a little bit differences on power-on sequence.
1621 */
1622 priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1623 if (priv->mcm) {
1624 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1625
1626 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1627 if (IS_ERR(priv->rstc)) {
1628 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1629 return PTR_ERR(priv->rstc);
1630 }
1631 }
1632
Greg Ungererddda1ac2019-01-30 11:24:05 +10001633 /* Get the hardware identifier from the devicetree node.
1634 * We will need it for some of the clock and regulator setup.
1635 */
1636 priv->id = (unsigned int)(unsigned long)
1637 of_device_get_match_data(&mdiodev->dev);
Sean Wangb8f126a2017-04-07 16:45:09 +08001638
Greg Ungererddda1ac2019-01-30 11:24:05 +10001639 if (priv->id == ID_MT7530) {
1640 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1641 if (IS_ERR(priv->core_pwr))
1642 return PTR_ERR(priv->core_pwr);
1643
1644 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1645 if (IS_ERR(priv->io_pwr))
1646 return PTR_ERR(priv->io_pwr);
1647 }
Sean Wangb8f126a2017-04-07 16:45:09 +08001648
1649 /* Not MCM that indicates switch works as the remote standalone
1650 * integrated circuit so the GPIO pin would be used to complete
1651 * the reset, otherwise memory-mapped register accessing used
1652 * through syscon provides in the case of MCM.
1653 */
1654 if (!priv->mcm) {
1655 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1656 GPIOD_OUT_LOW);
1657 if (IS_ERR(priv->reset)) {
1658 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1659 return PTR_ERR(priv->reset);
1660 }
1661 }
1662
1663 priv->bus = mdiodev->bus;
1664 priv->dev = &mdiodev->dev;
1665 priv->ds->priv = priv;
1666 priv->ds->ops = &mt7530_switch_ops;
1667 mutex_init(&priv->reg_mutex);
1668 dev_set_drvdata(&mdiodev->dev, priv);
1669
Vivien Didelot23c9ee42017-05-26 18:12:51 -04001670 return dsa_register_switch(priv->ds);
Sean Wangb8f126a2017-04-07 16:45:09 +08001671}
1672
1673static void
1674mt7530_remove(struct mdio_device *mdiodev)
1675{
1676 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1677 int ret = 0;
1678
1679 ret = regulator_disable(priv->core_pwr);
1680 if (ret < 0)
1681 dev_err(priv->dev,
1682 "Failed to disable core power: %d\n", ret);
1683
1684 ret = regulator_disable(priv->io_pwr);
1685 if (ret < 0)
1686 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1687 ret);
1688
1689 dsa_unregister_switch(priv->ds);
1690 mutex_destroy(&priv->reg_mutex);
1691}
1692
Sean Wangb8f126a2017-04-07 16:45:09 +08001693static struct mdio_driver mt7530_mdio_driver = {
1694 .probe = mt7530_probe,
1695 .remove = mt7530_remove,
1696 .mdiodrv.driver = {
1697 .name = "mt7530",
1698 .of_match_table = mt7530_of_match,
1699 },
1700};
1701
1702mdio_module_driver(mt7530_mdio_driver);
1703
1704MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1705MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1706MODULE_LICENSE("GPL");