blob: 9b39ccd9dd4c156ec3b881999279a8a05b1ed2c6 [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>
DENG Qingfangba751e22021-05-19 11:32:00 +080013#include <linux/of_irq.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080014#include <linux/of_mdio.h>
15#include <linux/of_net.h>
16#include <linux/of_platform.h>
René van Dorstca366d62019-09-02 15:02:24 +020017#include <linux/phylink.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080018#include <linux/regmap.h>
19#include <linux/regulator/consumer.h>
20#include <linux/reset.h>
Florian Fainellieb976a52017-04-08 08:52:02 -070021#include <linux/gpio/consumer.h>
DENG Qingfang429a0ed2021-01-25 12:43:22 +080022#include <linux/gpio/driver.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080023#include <net/dsa.h>
Sean Wangb8f126a2017-04-07 16:45:09 +080024
25#include "mt7530.h"
26
27/* String, offset, and register size in bytes if different from 4 bytes */
28static const struct mt7530_mib_desc mt7530_mib[] = {
29 MIB_DESC(1, 0x00, "TxDrop"),
30 MIB_DESC(1, 0x04, "TxCrcErr"),
31 MIB_DESC(1, 0x08, "TxUnicast"),
32 MIB_DESC(1, 0x0c, "TxMulticast"),
33 MIB_DESC(1, 0x10, "TxBroadcast"),
34 MIB_DESC(1, 0x14, "TxCollision"),
35 MIB_DESC(1, 0x18, "TxSingleCollision"),
36 MIB_DESC(1, 0x1c, "TxMultipleCollision"),
37 MIB_DESC(1, 0x20, "TxDeferred"),
38 MIB_DESC(1, 0x24, "TxLateCollision"),
39 MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
40 MIB_DESC(1, 0x2c, "TxPause"),
41 MIB_DESC(1, 0x30, "TxPktSz64"),
42 MIB_DESC(1, 0x34, "TxPktSz65To127"),
43 MIB_DESC(1, 0x38, "TxPktSz128To255"),
44 MIB_DESC(1, 0x3c, "TxPktSz256To511"),
45 MIB_DESC(1, 0x40, "TxPktSz512To1023"),
46 MIB_DESC(1, 0x44, "Tx1024ToMax"),
47 MIB_DESC(2, 0x48, "TxBytes"),
48 MIB_DESC(1, 0x60, "RxDrop"),
49 MIB_DESC(1, 0x64, "RxFiltering"),
50 MIB_DESC(1, 0x6c, "RxMulticast"),
51 MIB_DESC(1, 0x70, "RxBroadcast"),
52 MIB_DESC(1, 0x74, "RxAlignErr"),
53 MIB_DESC(1, 0x78, "RxCrcErr"),
54 MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
55 MIB_DESC(1, 0x80, "RxFragErr"),
56 MIB_DESC(1, 0x84, "RxOverSzErr"),
57 MIB_DESC(1, 0x88, "RxJabberErr"),
58 MIB_DESC(1, 0x8c, "RxPause"),
59 MIB_DESC(1, 0x90, "RxPktSz64"),
60 MIB_DESC(1, 0x94, "RxPktSz65To127"),
61 MIB_DESC(1, 0x98, "RxPktSz128To255"),
62 MIB_DESC(1, 0x9c, "RxPktSz256To511"),
63 MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
64 MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
65 MIB_DESC(2, 0xa8, "RxBytes"),
66 MIB_DESC(1, 0xb0, "RxCtrlDrop"),
67 MIB_DESC(1, 0xb4, "RxIngressDrop"),
68 MIB_DESC(1, 0xb8, "RxArlDrop"),
69};
70
Ilya Lipnitskiy47323152021-03-26 23:07:52 -070071/* Since phy_device has not yet been created and
72 * phy_{read,write}_mmd_indirect is not available, we provide our own
73 * core_{read,write}_mmd_indirect with core_{clear,write,set} wrappers
74 * to complete this function.
75 */
Sean Wangb8f126a2017-04-07 16:45:09 +080076static int
Sean Wangb8f126a2017-04-07 16:45:09 +080077core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
78{
79 struct mii_bus *bus = priv->bus;
80 int value, ret;
81
82 /* Write the desired MMD Devad */
83 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
84 if (ret < 0)
85 goto err;
86
87 /* Write the desired MMD register address */
88 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
89 if (ret < 0)
90 goto err;
91
92 /* Select the Function : DATA with no post increment */
93 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
94 if (ret < 0)
95 goto err;
96
97 /* Read the content of the MMD's selected register */
98 value = bus->read(bus, 0, MII_MMD_DATA);
99
100 return value;
101err:
102 dev_err(&bus->dev, "failed to read mmd register\n");
103
104 return ret;
105}
106
107static int
108core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
109 int devad, u32 data)
110{
111 struct mii_bus *bus = priv->bus;
112 int ret;
113
114 /* Write the desired MMD Devad */
115 ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
116 if (ret < 0)
117 goto err;
118
119 /* Write the desired MMD register address */
120 ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
121 if (ret < 0)
122 goto err;
123
124 /* Select the Function : DATA with no post increment */
125 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
126 if (ret < 0)
127 goto err;
128
129 /* Write the data into MMD's selected register */
130 ret = bus->write(bus, 0, MII_MMD_DATA, data);
131err:
132 if (ret < 0)
133 dev_err(&bus->dev,
134 "failed to write mmd register\n");
135 return ret;
136}
137
138static void
139core_write(struct mt7530_priv *priv, u32 reg, u32 val)
140{
141 struct mii_bus *bus = priv->bus;
142
143 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
144
145 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
146
147 mutex_unlock(&bus->mdio_lock);
148}
149
150static void
151core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
152{
153 struct mii_bus *bus = priv->bus;
154 u32 val;
155
156 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
157
158 val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
159 val &= ~mask;
160 val |= set;
161 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
162
163 mutex_unlock(&bus->mdio_lock);
164}
165
166static void
167core_set(struct mt7530_priv *priv, u32 reg, u32 val)
168{
169 core_rmw(priv, reg, 0, val);
170}
171
172static void
173core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
174{
175 core_rmw(priv, reg, val, 0);
176}
177
178static int
179mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
180{
181 struct mii_bus *bus = priv->bus;
182 u16 page, r, lo, hi;
183 int ret;
184
185 page = (reg >> 6) & 0x3ff;
186 r = (reg >> 2) & 0xf;
187 lo = val & 0xffff;
188 hi = val >> 16;
189
190 /* MT7530 uses 31 as the pseudo port */
191 ret = bus->write(bus, 0x1f, 0x1f, page);
192 if (ret < 0)
193 goto err;
194
195 ret = bus->write(bus, 0x1f, r, lo);
196 if (ret < 0)
197 goto err;
198
199 ret = bus->write(bus, 0x1f, 0x10, hi);
200err:
201 if (ret < 0)
202 dev_err(&bus->dev,
203 "failed to write mt7530 register\n");
204 return ret;
205}
206
207static u32
208mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
209{
210 struct mii_bus *bus = priv->bus;
211 u16 page, r, lo, hi;
212 int ret;
213
214 page = (reg >> 6) & 0x3ff;
215 r = (reg >> 2) & 0xf;
216
217 /* MT7530 uses 31 as the pseudo port */
218 ret = bus->write(bus, 0x1f, 0x1f, page);
219 if (ret < 0) {
220 dev_err(&bus->dev,
221 "failed to read mt7530 register\n");
222 return ret;
223 }
224
225 lo = bus->read(bus, 0x1f, r);
226 hi = bus->read(bus, 0x1f, 0x10);
227
228 return (hi << 16) | (lo & 0xffff);
229}
230
231static void
232mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
233{
234 struct mii_bus *bus = priv->bus;
235
236 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
237
238 mt7530_mii_write(priv, reg, val);
239
240 mutex_unlock(&bus->mdio_lock);
241}
242
243static u32
Landen Chaoc2885752020-09-11 21:48:54 +0800244_mt7530_unlocked_read(struct mt7530_dummy_poll *p)
245{
246 return mt7530_mii_read(p->priv, p->reg);
247}
248
249static u32
Sean Wangb8f126a2017-04-07 16:45:09 +0800250_mt7530_read(struct mt7530_dummy_poll *p)
251{
252 struct mii_bus *bus = p->priv->bus;
253 u32 val;
254
255 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
256
257 val = mt7530_mii_read(p->priv, p->reg);
258
259 mutex_unlock(&bus->mdio_lock);
260
261 return val;
262}
263
264static u32
265mt7530_read(struct mt7530_priv *priv, u32 reg)
266{
267 struct mt7530_dummy_poll p;
268
269 INIT_MT7530_DUMMY_POLL(&p, priv, reg);
270 return _mt7530_read(&p);
271}
272
273static void
274mt7530_rmw(struct mt7530_priv *priv, u32 reg,
275 u32 mask, u32 set)
276{
277 struct mii_bus *bus = priv->bus;
278 u32 val;
279
280 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
281
282 val = mt7530_mii_read(priv, reg);
283 val &= ~mask;
284 val |= set;
285 mt7530_mii_write(priv, reg, val);
286
287 mutex_unlock(&bus->mdio_lock);
288}
289
290static void
291mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
292{
293 mt7530_rmw(priv, reg, 0, val);
294}
295
296static void
297mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
298{
299 mt7530_rmw(priv, reg, val, 0);
300}
301
302static int
303mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
304{
305 u32 val;
306 int ret;
307 struct mt7530_dummy_poll p;
308
309 /* Set the command operating upon the MAC address entries */
310 val = ATC_BUSY | ATC_MAT(0) | cmd;
311 mt7530_write(priv, MT7530_ATC, val);
312
313 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
314 ret = readx_poll_timeout(_mt7530_read, &p, val,
315 !(val & ATC_BUSY), 20, 20000);
316 if (ret < 0) {
317 dev_err(priv->dev, "reset timeout\n");
318 return ret;
319 }
320
321 /* Additional sanity for read command if the specified
322 * entry is invalid
323 */
324 val = mt7530_read(priv, MT7530_ATC);
325 if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
326 return -EINVAL;
327
328 if (rsp)
329 *rsp = val;
330
331 return 0;
332}
333
334static void
335mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
336{
337 u32 reg[3];
338 int i;
339
340 /* Read from ARL table into an array */
341 for (i = 0; i < 3; i++) {
342 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
343
344 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
345 __func__, __LINE__, i, reg[i]);
346 }
347
348 fdb->vid = (reg[1] >> CVID) & CVID_MASK;
349 fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
350 fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
351 fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
352 fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
353 fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
354 fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
355 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
356 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
357 fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
358}
359
360static void
361mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
362 u8 port_mask, const u8 *mac,
363 u8 aging, u8 type)
364{
365 u32 reg[3] = { 0 };
366 int i;
367
368 reg[1] |= vid & CVID_MASK;
Eric Woudstra7e777022021-07-19 20:23:57 +0200369 if (vid > 1)
370 reg[1] |= ATA2_IVL;
Sean Wangb8f126a2017-04-07 16:45:09 +0800371 reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
372 reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
373 /* STATIC_ENT indicate that entry is static wouldn't
374 * be aged out and STATIC_EMP specified as erasing an
375 * entry
376 */
377 reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
378 reg[1] |= mac[5] << MAC_BYTE_5;
379 reg[1] |= mac[4] << MAC_BYTE_4;
380 reg[0] |= mac[3] << MAC_BYTE_3;
381 reg[0] |= mac[2] << MAC_BYTE_2;
382 reg[0] |= mac[1] << MAC_BYTE_1;
383 reg[0] |= mac[0] << MAC_BYTE_0;
384
385 /* Write array into the ARL table */
386 for (i = 0; i < 3; i++)
387 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
388}
389
Landen Chao88bdef82020-09-11 21:48:52 +0800390/* Setup TX circuit including relevant PAD and driving */
Sean Wangb8f126a2017-04-07 16:45:09 +0800391static int
Landen Chao88bdef82020-09-11 21:48:52 +0800392mt7530_pad_clk_setup(struct dsa_switch *ds, phy_interface_t interface)
Sean Wangb8f126a2017-04-07 16:45:09 +0800393{
394 struct mt7530_priv *priv = ds->priv;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200395 u32 ncpo1, ssc_delta, trgint, i, xtal;
396
397 xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK;
398
399 if (xtal == HWTRAP_XTAL_20MHZ) {
400 dev_err(priv->dev,
401 "%s: MT7530 with a 20MHz XTAL is not supported!\n",
402 __func__);
403 return -EINVAL;
404 }
Sean Wangb8f126a2017-04-07 16:45:09 +0800405
Landen Chao88bdef82020-09-11 21:48:52 +0800406 switch (interface) {
Sean Wangb8f126a2017-04-07 16:45:09 +0800407 case PHY_INTERFACE_MODE_RGMII:
408 trgint = 0;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200409 /* PLL frequency: 125MHz */
Sean Wangb8f126a2017-04-07 16:45:09 +0800410 ncpo1 = 0x0c80;
Sean Wangb8f126a2017-04-07 16:45:09 +0800411 break;
412 case PHY_INTERFACE_MODE_TRGMII:
413 trgint = 1;
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200414 if (priv->id == ID_MT7621) {
415 /* PLL frequency: 150MHz: 1.2GBit */
416 if (xtal == HWTRAP_XTAL_40MHZ)
417 ncpo1 = 0x0780;
418 if (xtal == HWTRAP_XTAL_25MHZ)
419 ncpo1 = 0x0a00;
420 } else { /* PLL frequency: 250MHz: 2.0Gbit */
421 if (xtal == HWTRAP_XTAL_40MHZ)
422 ncpo1 = 0x0c80;
423 if (xtal == HWTRAP_XTAL_25MHZ)
424 ncpo1 = 0x1400;
425 }
Sean Wangb8f126a2017-04-07 16:45:09 +0800426 break;
427 default:
Landen Chao88bdef82020-09-11 21:48:52 +0800428 dev_err(priv->dev, "xMII interface %d not supported\n",
429 interface);
Sean Wangb8f126a2017-04-07 16:45:09 +0800430 return -EINVAL;
431 }
432
René van Dorst7ef6f6f2019-06-20 14:21:55 +0200433 if (xtal == HWTRAP_XTAL_25MHZ)
434 ssc_delta = 0x57;
435 else
436 ssc_delta = 0x87;
437
Sean Wangb8f126a2017-04-07 16:45:09 +0800438 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
439 P6_INTF_MODE(trgint));
440
441 /* Lower Tx Driving for TRGMII path */
442 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
443 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
444 TD_DM_DRVP(8) | TD_DM_DRVN(8));
445
Ilya Lipnitskiy47323152021-03-26 23:07:52 -0700446 /* Disable MT7530 core and TRGMII Tx clocks */
447 core_clear(priv, CORE_TRGMII_GSW_CLK_CG,
448 REG_GSWCK_EN | REG_TRGMIICK_EN);
Sean Wangb8f126a2017-04-07 16:45:09 +0800449
Ilya Lipnitskiy47323152021-03-26 23:07:52 -0700450 /* Setup core clock for MT7530 */
451 /* Disable PLL */
452 core_write(priv, CORE_GSWPLL_GRP1, 0);
Sean Wangb8f126a2017-04-07 16:45:09 +0800453
Ilya Lipnitskiyc3b8e072021-03-12 00:07:03 -0800454 /* Set core clock into 500Mhz */
455 core_write(priv, CORE_GSWPLL_GRP2,
456 RG_GSWPLL_POSDIV_500M(1) |
457 RG_GSWPLL_FBKDIV_500M(25));
Sean Wangb8f126a2017-04-07 16:45:09 +0800458
Ilya Lipnitskiyc3b8e072021-03-12 00:07:03 -0800459 /* Enable PLL */
460 core_write(priv, CORE_GSWPLL_GRP1,
461 RG_GSWPLL_EN_PRE |
462 RG_GSWPLL_POSDIV_200M(2) |
463 RG_GSWPLL_FBKDIV_200M(32));
Sean Wangb8f126a2017-04-07 16:45:09 +0800464
Sean Wangb8f126a2017-04-07 16:45:09 +0800465 /* Setup the MT7530 TRGMII Tx Clock */
Sean Wangb8f126a2017-04-07 16:45:09 +0800466 core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
467 core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
468 core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
469 core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
470 core_write(priv, CORE_PLL_GROUP4,
471 RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
472 RG_SYSPLL_BIAS_LPF_EN);
473 core_write(priv, CORE_PLL_GROUP2,
474 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
475 RG_SYSPLL_POSDIV(1));
476 core_write(priv, CORE_PLL_GROUP7,
477 RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
478 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
Ilya Lipnitskiy47323152021-03-26 23:07:52 -0700479
480 /* Enable MT7530 core and TRGMII Tx clocks */
Sean Wangb8f126a2017-04-07 16:45:09 +0800481 core_set(priv, CORE_TRGMII_GSW_CLK_CG,
482 REG_GSWCK_EN | REG_TRGMIICK_EN);
483
484 if (!trgint)
485 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
486 mt7530_rmw(priv, MT7530_TRGMII_RD(i),
487 RD_TAP_MASK, RD_TAP(16));
Sean Wangb8f126a2017-04-07 16:45:09 +0800488 return 0;
489}
490
Landen Chaoc2885752020-09-11 21:48:54 +0800491static bool mt7531_dual_sgmii_supported(struct mt7530_priv *priv)
492{
493 u32 val;
494
495 val = mt7530_read(priv, MT7531_TOP_SIG_SR);
496
497 return (val & PAD_DUAL_SGMII_EN) != 0;
498}
499
500static int
501mt7531_pad_setup(struct dsa_switch *ds, phy_interface_t interface)
502{
503 struct mt7530_priv *priv = ds->priv;
504 u32 top_sig;
505 u32 hwstrap;
506 u32 xtal;
507 u32 val;
508
509 if (mt7531_dual_sgmii_supported(priv))
510 return 0;
511
512 val = mt7530_read(priv, MT7531_CREV);
513 top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR);
514 hwstrap = mt7530_read(priv, MT7531_HWTRAP);
515 if ((val & CHIP_REV_M) > 0)
516 xtal = (top_sig & PAD_MCM_SMI_EN) ? HWTRAP_XTAL_FSEL_40MHZ :
517 HWTRAP_XTAL_FSEL_25MHZ;
518 else
519 xtal = hwstrap & HWTRAP_XTAL_FSEL_MASK;
520
521 /* Step 1 : Disable MT7531 COREPLL */
522 val = mt7530_read(priv, MT7531_PLLGP_EN);
523 val &= ~EN_COREPLL;
524 mt7530_write(priv, MT7531_PLLGP_EN, val);
525
526 /* Step 2: switch to XTAL output */
527 val = mt7530_read(priv, MT7531_PLLGP_EN);
528 val |= SW_CLKSW;
529 mt7530_write(priv, MT7531_PLLGP_EN, val);
530
531 val = mt7530_read(priv, MT7531_PLLGP_CR0);
532 val &= ~RG_COREPLL_EN;
533 mt7530_write(priv, MT7531_PLLGP_CR0, val);
534
535 /* Step 3: disable PLLGP and enable program PLLGP */
536 val = mt7530_read(priv, MT7531_PLLGP_EN);
537 val |= SW_PLLGP;
538 mt7530_write(priv, MT7531_PLLGP_EN, val);
539
540 /* Step 4: program COREPLL output frequency to 500MHz */
541 val = mt7530_read(priv, MT7531_PLLGP_CR0);
542 val &= ~RG_COREPLL_POSDIV_M;
543 val |= 2 << RG_COREPLL_POSDIV_S;
544 mt7530_write(priv, MT7531_PLLGP_CR0, val);
545 usleep_range(25, 35);
546
547 switch (xtal) {
548 case HWTRAP_XTAL_FSEL_25MHZ:
549 val = mt7530_read(priv, MT7531_PLLGP_CR0);
550 val &= ~RG_COREPLL_SDM_PCW_M;
551 val |= 0x140000 << RG_COREPLL_SDM_PCW_S;
552 mt7530_write(priv, MT7531_PLLGP_CR0, val);
553 break;
554 case HWTRAP_XTAL_FSEL_40MHZ:
555 val = mt7530_read(priv, MT7531_PLLGP_CR0);
556 val &= ~RG_COREPLL_SDM_PCW_M;
557 val |= 0x190000 << RG_COREPLL_SDM_PCW_S;
558 mt7530_write(priv, MT7531_PLLGP_CR0, val);
559 break;
Tom Rix0e8c2662020-10-31 08:30:47 -0700560 }
Landen Chaoc2885752020-09-11 21:48:54 +0800561
562 /* Set feedback divide ratio update signal to high */
563 val = mt7530_read(priv, MT7531_PLLGP_CR0);
564 val |= RG_COREPLL_SDM_PCW_CHG;
565 mt7530_write(priv, MT7531_PLLGP_CR0, val);
566 /* Wait for at least 16 XTAL clocks */
567 usleep_range(10, 20);
568
569 /* Step 5: set feedback divide ratio update signal to low */
570 val = mt7530_read(priv, MT7531_PLLGP_CR0);
571 val &= ~RG_COREPLL_SDM_PCW_CHG;
572 mt7530_write(priv, MT7531_PLLGP_CR0, val);
573
574 /* Enable 325M clock for SGMII */
575 mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
576
577 /* Enable 250SSC clock for RGMII */
578 mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
579
580 /* Step 6: Enable MT7531 PLL */
581 val = mt7530_read(priv, MT7531_PLLGP_CR0);
582 val |= RG_COREPLL_EN;
583 mt7530_write(priv, MT7531_PLLGP_CR0, val);
584
585 val = mt7530_read(priv, MT7531_PLLGP_EN);
586 val |= EN_COREPLL;
587 mt7530_write(priv, MT7531_PLLGP_EN, val);
588 usleep_range(25, 35);
589
590 return 0;
591}
592
Sean Wangb8f126a2017-04-07 16:45:09 +0800593static void
594mt7530_mib_reset(struct dsa_switch *ds)
595{
596 struct mt7530_priv *priv = ds->priv;
597
598 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
599 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
600}
601
DENG Qingfangba751e22021-05-19 11:32:00 +0800602static int mt7530_phy_read(struct mt7530_priv *priv, int port, int regnum)
Sean Wangb8f126a2017-04-07 16:45:09 +0800603{
Sean Wangb8f126a2017-04-07 16:45:09 +0800604 return mdiobus_read_nested(priv->bus, port, regnum);
605}
606
DENG Qingfangba751e22021-05-19 11:32:00 +0800607static int mt7530_phy_write(struct mt7530_priv *priv, int port, int regnum,
Colin Ian King360cc342017-10-03 11:46:33 +0100608 u16 val)
Sean Wangb8f126a2017-04-07 16:45:09 +0800609{
Sean Wangb8f126a2017-04-07 16:45:09 +0800610 return mdiobus_write_nested(priv->bus, port, regnum, val);
611}
612
Landen Chaoc2885752020-09-11 21:48:54 +0800613static int
614mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad,
615 int regnum)
616{
617 struct mii_bus *bus = priv->bus;
618 struct mt7530_dummy_poll p;
619 u32 reg, val;
620 int ret;
621
622 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
623
624 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
625
626 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
627 !(val & MT7531_PHY_ACS_ST), 20, 100000);
628 if (ret < 0) {
629 dev_err(priv->dev, "poll timeout\n");
630 goto out;
631 }
632
633 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
634 MT7531_MDIO_DEV_ADDR(devad) | regnum;
635 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
636
637 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
638 !(val & MT7531_PHY_ACS_ST), 20, 100000);
639 if (ret < 0) {
640 dev_err(priv->dev, "poll timeout\n");
641 goto out;
642 }
643
644 reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) |
645 MT7531_MDIO_DEV_ADDR(devad);
646 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
647
648 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
649 !(val & MT7531_PHY_ACS_ST), 20, 100000);
650 if (ret < 0) {
651 dev_err(priv->dev, "poll timeout\n");
652 goto out;
653 }
654
655 ret = val & MT7531_MDIO_RW_DATA_MASK;
656out:
657 mutex_unlock(&bus->mdio_lock);
658
659 return ret;
660}
661
662static int
663mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad,
664 int regnum, u32 data)
665{
666 struct mii_bus *bus = priv->bus;
667 struct mt7530_dummy_poll p;
668 u32 val, reg;
669 int ret;
670
671 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
672
673 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
674
675 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
676 !(val & MT7531_PHY_ACS_ST), 20, 100000);
677 if (ret < 0) {
678 dev_err(priv->dev, "poll timeout\n");
679 goto out;
680 }
681
682 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
683 MT7531_MDIO_DEV_ADDR(devad) | regnum;
684 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
685
686 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
687 !(val & MT7531_PHY_ACS_ST), 20, 100000);
688 if (ret < 0) {
689 dev_err(priv->dev, "poll timeout\n");
690 goto out;
691 }
692
693 reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) |
694 MT7531_MDIO_DEV_ADDR(devad) | data;
695 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
696
697 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
698 !(val & MT7531_PHY_ACS_ST), 20, 100000);
699 if (ret < 0) {
700 dev_err(priv->dev, "poll timeout\n");
701 goto out;
702 }
703
704out:
705 mutex_unlock(&bus->mdio_lock);
706
707 return ret;
708}
709
710static int
711mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum)
712{
713 struct mii_bus *bus = priv->bus;
714 struct mt7530_dummy_poll p;
715 int ret;
716 u32 val;
717
718 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
719
720 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
721
722 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
723 !(val & MT7531_PHY_ACS_ST), 20, 100000);
724 if (ret < 0) {
725 dev_err(priv->dev, "poll timeout\n");
726 goto out;
727 }
728
729 val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) |
730 MT7531_MDIO_REG_ADDR(regnum);
731
732 mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST);
733
734 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
735 !(val & MT7531_PHY_ACS_ST), 20, 100000);
736 if (ret < 0) {
737 dev_err(priv->dev, "poll timeout\n");
738 goto out;
739 }
740
741 ret = val & MT7531_MDIO_RW_DATA_MASK;
742out:
743 mutex_unlock(&bus->mdio_lock);
744
745 return ret;
746}
747
748static int
749mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum,
750 u16 data)
751{
752 struct mii_bus *bus = priv->bus;
753 struct mt7530_dummy_poll p;
754 int ret;
755 u32 reg;
756
757 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
758
759 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
760
761 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
762 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
763 if (ret < 0) {
764 dev_err(priv->dev, "poll timeout\n");
765 goto out;
766 }
767
768 reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) |
769 MT7531_MDIO_REG_ADDR(regnum) | data;
770
771 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
772
773 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
774 !(reg & MT7531_PHY_ACS_ST), 20, 100000);
775 if (ret < 0) {
776 dev_err(priv->dev, "poll timeout\n");
777 goto out;
778 }
779
780out:
781 mutex_unlock(&bus->mdio_lock);
782
783 return ret;
784}
785
786static int
DENG Qingfangba751e22021-05-19 11:32:00 +0800787mt7531_ind_phy_read(struct mt7530_priv *priv, int port, int regnum)
Landen Chaoc2885752020-09-11 21:48:54 +0800788{
Landen Chaoc2885752020-09-11 21:48:54 +0800789 int devad;
790 int ret;
791
792 if (regnum & MII_ADDR_C45) {
793 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
794 ret = mt7531_ind_c45_phy_read(priv, port, devad,
795 regnum & MII_REGADDR_C45_MASK);
796 } else {
797 ret = mt7531_ind_c22_phy_read(priv, port, regnum);
798 }
799
800 return ret;
801}
802
803static int
DENG Qingfangba751e22021-05-19 11:32:00 +0800804mt7531_ind_phy_write(struct mt7530_priv *priv, int port, int regnum,
Landen Chaoc2885752020-09-11 21:48:54 +0800805 u16 data)
806{
Landen Chaoc2885752020-09-11 21:48:54 +0800807 int devad;
808 int ret;
809
810 if (regnum & MII_ADDR_C45) {
811 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f;
812 ret = mt7531_ind_c45_phy_write(priv, port, devad,
813 regnum & MII_REGADDR_C45_MASK,
814 data);
815 } else {
816 ret = mt7531_ind_c22_phy_write(priv, port, regnum, data);
817 }
818
819 return ret;
820}
821
DENG Qingfangba751e22021-05-19 11:32:00 +0800822static int
823mt753x_phy_read(struct mii_bus *bus, int port, int regnum)
824{
825 struct mt7530_priv *priv = bus->priv;
826
827 return priv->info->phy_read(priv, port, regnum);
828}
829
830static int
831mt753x_phy_write(struct mii_bus *bus, int port, int regnum, u16 val)
832{
833 struct mt7530_priv *priv = bus->priv;
834
835 return priv->info->phy_write(priv, port, regnum, val);
836}
837
Sean Wangb8f126a2017-04-07 16:45:09 +0800838static void
Florian Fainelli89f09042018-04-25 12:12:50 -0700839mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
840 uint8_t *data)
Sean Wangb8f126a2017-04-07 16:45:09 +0800841{
842 int i;
843
Florian Fainelli89f09042018-04-25 12:12:50 -0700844 if (stringset != ETH_SS_STATS)
845 return;
846
Sean Wangb8f126a2017-04-07 16:45:09 +0800847 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
848 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
849 ETH_GSTRING_LEN);
850}
851
852static void
853mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
854 uint64_t *data)
855{
856 struct mt7530_priv *priv = ds->priv;
857 const struct mt7530_mib_desc *mib;
858 u32 reg, i;
859 u64 hi;
860
861 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
862 mib = &mt7530_mib[i];
863 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
864
865 data[i] = mt7530_read(priv, reg);
866 if (mib->size == 2) {
867 hi = mt7530_read(priv, reg + 4);
868 data[i] |= hi << 32;
869 }
870 }
871}
872
873static int
Florian Fainelli89f09042018-04-25 12:12:50 -0700874mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
Sean Wangb8f126a2017-04-07 16:45:09 +0800875{
Florian Fainelli89f09042018-04-25 12:12:50 -0700876 if (sset != ETH_SS_STATS)
877 return 0;
878
Sean Wangb8f126a2017-04-07 16:45:09 +0800879 return ARRAY_SIZE(mt7530_mib);
880}
881
DENG Qingfangea6d5c92020-12-08 15:00:28 +0800882static int
883mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
884{
885 struct mt7530_priv *priv = ds->priv;
886 unsigned int secs = msecs / 1000;
887 unsigned int tmp_age_count;
888 unsigned int error = -1;
889 unsigned int age_count;
890 unsigned int age_unit;
891
892 /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
893 if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
894 return -ERANGE;
895
896 /* iterate through all possible age_count to find the closest pair */
897 for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
898 unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
899
900 if (tmp_age_unit <= AGE_UNIT_MAX) {
901 unsigned int tmp_error = secs -
902 (tmp_age_count + 1) * (tmp_age_unit + 1);
903
904 /* found a closer pair */
905 if (error > tmp_error) {
906 error = tmp_error;
907 age_count = tmp_age_count;
908 age_unit = tmp_age_unit;
909 }
910
911 /* found the exact match, so break the loop */
912 if (!error)
913 break;
914 }
915 }
916
917 mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
918
919 return 0;
920}
921
René van Dorst38f790a2019-09-02 15:02:26 +0200922static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
923{
924 struct mt7530_priv *priv = ds->priv;
925 u8 tx_delay = 0;
926 int val;
927
928 mutex_lock(&priv->reg_mutex);
929
930 val = mt7530_read(priv, MT7530_MHWTRAP);
931
932 val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
933 val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
934
935 switch (priv->p5_intf_sel) {
936 case P5_INTF_SEL_PHY_P0:
937 /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
938 val |= MHWTRAP_PHY0_SEL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500939 fallthrough;
René van Dorst38f790a2019-09-02 15:02:26 +0200940 case P5_INTF_SEL_PHY_P4:
941 /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
942 val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
943
944 /* Setup the MAC by default for the cpu port */
945 mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
946 break;
947 case P5_INTF_SEL_GMAC5:
948 /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
949 val &= ~MHWTRAP_P5_DIS;
950 break;
951 case P5_DISABLED:
952 interface = PHY_INTERFACE_MODE_NA;
953 break;
954 default:
955 dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
956 priv->p5_intf_sel);
957 goto unlock_exit;
958 }
959
960 /* Setup RGMII settings */
961 if (phy_interface_mode_is_rgmii(interface)) {
962 val |= MHWTRAP_P5_RGMII_MODE;
963
964 /* P5 RGMII RX Clock Control: delay setting for 1000M */
965 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
966
967 /* Don't set delay in DSA mode */
968 if (!dsa_is_dsa_port(priv->ds, 5) &&
969 (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
970 interface == PHY_INTERFACE_MODE_RGMII_ID))
971 tx_delay = 4; /* n * 0.5 ns */
972
973 /* P5 RGMII TX Clock Control: delay x */
974 mt7530_write(priv, MT7530_P5RGMIITXCR,
975 CSR_RGMII_TXC_CFG(0x10 + tx_delay));
976
977 /* reduce P5 RGMII Tx driving, 8mA */
978 mt7530_write(priv, MT7530_IO_DRV_CR,
979 P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
980 }
981
982 mt7530_write(priv, MT7530_MHWTRAP, val);
983
984 dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
985 val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
986
987 priv->p5_interface = interface;
988
989unlock_exit:
990 mutex_unlock(&priv->reg_mutex);
991}
992
Sean Wangb8f126a2017-04-07 16:45:09 +0800993static int
Landen Chaoc2885752020-09-11 21:48:54 +0800994mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
Sean Wangb8f126a2017-04-07 16:45:09 +0800995{
Landen Chaoc2885752020-09-11 21:48:54 +0800996 struct mt7530_priv *priv = ds->priv;
Alex Dewar0ce0c3c2020-09-19 20:28:10 +0100997 int ret;
Landen Chaoc2885752020-09-11 21:48:54 +0800998
999 /* Setup max capability of CPU port at first */
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01001000 if (priv->info->cpu_port_config) {
1001 ret = priv->info->cpu_port_config(ds, port);
1002 if (ret)
1003 return ret;
1004 }
Landen Chaoc2885752020-09-11 21:48:54 +08001005
Sean Wangb8f126a2017-04-07 16:45:09 +08001006 /* Enable Mediatek header mode on the cpu port */
1007 mt7530_write(priv, MT7530_PVC_P(port),
1008 PORT_SPEC_TAG);
1009
DENG Qingfang5a308332021-03-16 01:09:40 +08001010 /* Disable flooding by default */
1011 mt7530_rmw(priv, MT7530_MFC, BC_FFP_MASK | UNM_FFP_MASK | UNU_FFP_MASK,
1012 BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) | UNU_FFP(BIT(port)));
Sean Wangb8f126a2017-04-07 16:45:09 +08001013
Greg Ungererddda1ac2019-01-30 11:24:05 +10001014 /* Set CPU port number */
1015 if (priv->id == ID_MT7621)
1016 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
1017
Sean Wangb8f126a2017-04-07 16:45:09 +08001018 /* CPU port gets connected to all user ports of
Landen Chaoc2885752020-09-11 21:48:54 +08001019 * the switch.
Sean Wangb8f126a2017-04-07 16:45:09 +08001020 */
1021 mt7530_write(priv, MT7530_PCR_P(port),
Vivien Didelot02bc6e52017-10-26 11:22:56 -04001022 PCR_MATRIX(dsa_user_ports(priv->ds)));
Sean Wangb8f126a2017-04-07 16:45:09 +08001023
DENG Qingfang60871752021-08-04 00:04:02 +08001024 /* Set to fallback mode for independent VLAN learning */
1025 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1026 MT7530_PORT_FALLBACK_MODE);
1027
Sean Wangb8f126a2017-04-07 16:45:09 +08001028 return 0;
1029}
1030
1031static int
1032mt7530_port_enable(struct dsa_switch *ds, int port,
1033 struct phy_device *phy)
1034{
1035 struct mt7530_priv *priv = ds->priv;
1036
Vivien Didelot74be4ba2019-08-19 16:00:49 -04001037 if (!dsa_is_user_port(ds, port))
1038 return 0;
1039
Sean Wangb8f126a2017-04-07 16:45:09 +08001040 mutex_lock(&priv->reg_mutex);
1041
Sean Wangb8f126a2017-04-07 16:45:09 +08001042 /* Allow the user port gets connected to the cpu port and also
1043 * restore the port matrix if the port is the member of a certain
1044 * bridge.
1045 */
1046 priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
1047 priv->ports[port].enable = true;
1048 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1049 priv->ports[port].pm);
René van Dorst1d011452020-03-27 15:44:12 +01001050 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
Sean Wangb8f126a2017-04-07 16:45:09 +08001051
1052 mutex_unlock(&priv->reg_mutex);
1053
1054 return 0;
1055}
1056
1057static void
Andrew Lunn75104db2019-02-24 20:44:43 +01001058mt7530_port_disable(struct dsa_switch *ds, int port)
Sean Wangb8f126a2017-04-07 16:45:09 +08001059{
1060 struct mt7530_priv *priv = ds->priv;
1061
Vivien Didelot74be4ba2019-08-19 16:00:49 -04001062 if (!dsa_is_user_port(ds, port))
1063 return;
1064
Sean Wangb8f126a2017-04-07 16:45:09 +08001065 mutex_lock(&priv->reg_mutex);
1066
1067 /* Clear up all port matrix which could be restored in the next
1068 * enablement for the port.
1069 */
1070 priv->ports[port].enable = false;
1071 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1072 PCR_MATRIX_CLR);
René van Dorst1d011452020-03-27 15:44:12 +01001073 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
Sean Wangb8f126a2017-04-07 16:45:09 +08001074
1075 mutex_unlock(&priv->reg_mutex);
1076}
1077
DENG Qingfang94701742020-11-03 13:06:18 +08001078static int
1079mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1080{
1081 struct mt7530_priv *priv = ds->priv;
1082 struct mii_bus *bus = priv->bus;
1083 int length;
1084 u32 val;
1085
1086 /* When a new MTU is set, DSA always set the CPU port's MTU to the
1087 * largest MTU of the slave ports. Because the switch only has a global
1088 * RX length register, only allowing CPU port here is enough.
1089 */
1090 if (!dsa_is_cpu_port(ds, port))
1091 return 0;
1092
1093 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1094
1095 val = mt7530_mii_read(priv, MT7530_GMACCR);
1096 val &= ~MAX_RX_PKT_LEN_MASK;
1097
1098 /* RX length also includes Ethernet header, MTK tag, and FCS length */
1099 length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
1100 if (length <= 1522) {
1101 val |= MAX_RX_PKT_LEN_1522;
1102 } else if (length <= 1536) {
1103 val |= MAX_RX_PKT_LEN_1536;
1104 } else if (length <= 1552) {
1105 val |= MAX_RX_PKT_LEN_1552;
1106 } else {
1107 val &= ~MAX_RX_JUMBO_MASK;
1108 val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
1109 val |= MAX_RX_PKT_LEN_JUMBO;
1110 }
1111
1112 mt7530_mii_write(priv, MT7530_GMACCR, val);
1113
1114 mutex_unlock(&bus->mdio_lock);
1115
1116 return 0;
1117}
1118
1119static int
1120mt7530_port_max_mtu(struct dsa_switch *ds, int port)
1121{
1122 return MT7530_MAX_MTU;
1123}
1124
Sean Wangb8f126a2017-04-07 16:45:09 +08001125static void
1126mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1127{
1128 struct mt7530_priv *priv = ds->priv;
1129 u32 stp_state;
1130
1131 switch (state) {
1132 case BR_STATE_DISABLED:
1133 stp_state = MT7530_STP_DISABLED;
1134 break;
1135 case BR_STATE_BLOCKING:
1136 stp_state = MT7530_STP_BLOCKING;
1137 break;
1138 case BR_STATE_LISTENING:
1139 stp_state = MT7530_STP_LISTENING;
1140 break;
1141 case BR_STATE_LEARNING:
1142 stp_state = MT7530_STP_LEARNING;
1143 break;
1144 case BR_STATE_FORWARDING:
1145 default:
1146 stp_state = MT7530_STP_FORWARDING;
1147 break;
1148 }
1149
DENG Qingfanga9e3f622021-08-04 00:04:03 +08001150 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK(FID_BRIDGED),
1151 FID_PST(FID_BRIDGED, stp_state));
Sean Wangb8f126a2017-04-07 16:45:09 +08001152}
1153
1154static int
DENG Qingfang5a308332021-03-16 01:09:40 +08001155mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1156 struct switchdev_brport_flags flags,
1157 struct netlink_ext_ack *extack)
1158{
1159 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1160 BR_BCAST_FLOOD))
1161 return -EINVAL;
1162
1163 return 0;
1164}
1165
1166static int
1167mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
1168 struct switchdev_brport_flags flags,
1169 struct netlink_ext_ack *extack)
1170{
1171 struct mt7530_priv *priv = ds->priv;
1172
1173 if (flags.mask & BR_LEARNING)
1174 mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
1175 flags.val & BR_LEARNING ? 0 : SA_DIS);
1176
1177 if (flags.mask & BR_FLOOD)
1178 mt7530_rmw(priv, MT7530_MFC, UNU_FFP(BIT(port)),
1179 flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
1180
1181 if (flags.mask & BR_MCAST_FLOOD)
1182 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1183 flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
1184
1185 if (flags.mask & BR_BCAST_FLOOD)
1186 mt7530_rmw(priv, MT7530_MFC, BC_FFP(BIT(port)),
1187 flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
1188
1189 return 0;
1190}
1191
1192static int
1193mt7530_port_set_mrouter(struct dsa_switch *ds, int port, bool mrouter,
1194 struct netlink_ext_ack *extack)
1195{
1196 struct mt7530_priv *priv = ds->priv;
1197
1198 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1199 mrouter ? UNM_FFP(BIT(port)) : 0);
1200
1201 return 0;
1202}
1203
1204static int
Sean Wangb8f126a2017-04-07 16:45:09 +08001205mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1206 struct net_device *bridge)
1207{
1208 struct mt7530_priv *priv = ds->priv;
1209 u32 port_bitmap = BIT(MT7530_CPU_PORT);
1210 int i;
1211
1212 mutex_lock(&priv->reg_mutex);
1213
1214 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1215 /* Add this port to the port matrix of the other ports in the
1216 * same bridge. If the port is disabled, port matrix is kept
1217 * and not being setup until the port becomes enabled.
1218 */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -04001219 if (dsa_is_user_port(ds, i) && i != port) {
Vivien Didelotc8652c82017-10-16 11:12:19 -04001220 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +08001221 continue;
1222 if (priv->ports[i].enable)
1223 mt7530_set(priv, MT7530_PCR_P(i),
1224 PCR_MATRIX(BIT(port)));
1225 priv->ports[i].pm |= PCR_MATRIX(BIT(port));
1226
1227 port_bitmap |= BIT(i);
1228 }
1229 }
1230
1231 /* Add the all other ports to this port matrix. */
1232 if (priv->ports[port].enable)
1233 mt7530_rmw(priv, MT7530_PCR_P(port),
1234 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1235 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1236
DENG Qingfang60871752021-08-04 00:04:02 +08001237 /* Set to fallback mode for independent VLAN learning */
1238 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1239 MT7530_PORT_FALLBACK_MODE);
1240
Sean Wangb8f126a2017-04-07 16:45:09 +08001241 mutex_unlock(&priv->reg_mutex);
1242
1243 return 0;
1244}
1245
1246static void
Sean Wang83163f72017-12-15 12:47:00 +08001247mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1248{
1249 struct mt7530_priv *priv = ds->priv;
1250 bool all_user_ports_removed = true;
1251 int i;
1252
DENG Qingfang60871752021-08-04 00:04:02 +08001253 /* This is called after .port_bridge_leave when leaving a VLAN-aware
1254 * bridge. Don't set standalone ports to fallback mode.
Sean Wang83163f72017-12-15 12:47:00 +08001255 */
DENG Qingfang60871752021-08-04 00:04:02 +08001256 if (dsa_to_port(ds, port)->bridge_dev)
1257 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1258 MT7530_PORT_FALLBACK_MODE);
1259
DENG Qingfange0451242020-04-14 14:34:08 +08001260 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1261 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1262 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +08001263
DENG Qingfang60871752021-08-04 00:04:02 +08001264 /* Set PVID to 0 */
1265 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1266 G0_PORT_VID_DEF);
1267
Sean Wang83163f72017-12-15 12:47:00 +08001268 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1269 if (dsa_is_user_port(ds, i) &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001270 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Sean Wang83163f72017-12-15 12:47:00 +08001271 all_user_ports_removed = false;
1272 break;
1273 }
1274 }
1275
1276 /* CPU port also does the same thing until all user ports belonging to
1277 * the CPU port get out of VLAN filtering mode.
1278 */
1279 if (all_user_ports_removed) {
1280 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
1281 PCR_MATRIX(dsa_user_ports(priv->ds)));
DENG Qingfange0451242020-04-14 14:34:08 +08001282 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
1283 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +08001284 }
1285}
1286
1287static void
1288mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1289{
1290 struct mt7530_priv *priv = ds->priv;
1291
Sean Wang83163f72017-12-15 12:47:00 +08001292 /* Trapped into security mode allows packet forwarding through VLAN
DENG Qingfang60871752021-08-04 00:04:02 +08001293 * table lookup.
Sean Wang83163f72017-12-15 12:47:00 +08001294 */
DENG Qingfang60871752021-08-04 00:04:02 +08001295 if (dsa_is_user_port(ds, port)) {
DENG Qingfang38152ea2020-05-13 23:37:17 +08001296 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1297 MT7530_PORT_SECURITY_MODE);
DENG Qingfang60871752021-08-04 00:04:02 +08001298 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1299 G0_PORT_VID(priv->ports[port].pvid));
1300 }
Sean Wang83163f72017-12-15 12:47:00 +08001301
1302 /* Set the port as a user port which is to be able to recognize VID
1303 * from incoming packets before fetching entry within the VLAN table.
1304 */
DENG Qingfange0451242020-04-14 14:34:08 +08001305 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1306 VLAN_ATTR(MT7530_VLAN_USER) |
1307 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
Sean Wang83163f72017-12-15 12:47:00 +08001308}
1309
1310static void
Sean Wangb8f126a2017-04-07 16:45:09 +08001311mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1312 struct net_device *bridge)
1313{
1314 struct mt7530_priv *priv = ds->priv;
1315 int i;
1316
1317 mutex_lock(&priv->reg_mutex);
1318
1319 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1320 /* Remove this port from the port matrix of the other ports
1321 * in the same bridge. If the port is disabled, port matrix
1322 * is kept and not being setup until the port becomes enabled.
Sean Wang83163f72017-12-15 12:47:00 +08001323 * And the other port's port matrix cannot be broken when the
1324 * other port is still a VLAN-aware port.
Sean Wangb8f126a2017-04-07 16:45:09 +08001325 */
Vladimir Oltean2a130552019-04-28 21:45:50 +03001326 if (dsa_is_user_port(ds, i) && i != port &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001327 !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Vivien Didelotc8652c82017-10-16 11:12:19 -04001328 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +08001329 continue;
1330 if (priv->ports[i].enable)
1331 mt7530_clear(priv, MT7530_PCR_P(i),
1332 PCR_MATRIX(BIT(port)));
1333 priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
1334 }
1335 }
1336
1337 /* Set the cpu port to be the only one in the port matrix of
1338 * this port.
1339 */
1340 if (priv->ports[port].enable)
1341 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1342 PCR_MATRIX(BIT(MT7530_CPU_PORT)));
1343 priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
1344
DENG Qingfang60871752021-08-04 00:04:02 +08001345 /* When a port is removed from the bridge, the port would be set up
1346 * back to the default as is at initial boot which is a VLAN-unaware
1347 * port.
1348 */
1349 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1350 MT7530_PORT_MATRIX_MODE);
1351
Sean Wangb8f126a2017-04-07 16:45:09 +08001352 mutex_unlock(&priv->reg_mutex);
1353}
1354
1355static int
Sean Wangb8f126a2017-04-07 16:45:09 +08001356mt7530_port_fdb_add(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001357 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +08001358{
1359 struct mt7530_priv *priv = ds->priv;
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +03001360 int ret;
Sean Wangb8f126a2017-04-07 16:45:09 +08001361 u8 port_mask = BIT(port);
1362
1363 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001364 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
Florian Fainelli18bd5942018-04-02 16:24:14 -07001365 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001366 mutex_unlock(&priv->reg_mutex);
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +03001367
1368 return ret;
Sean Wangb8f126a2017-04-07 16:45:09 +08001369}
1370
1371static int
1372mt7530_port_fdb_del(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001373 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +08001374{
1375 struct mt7530_priv *priv = ds->priv;
1376 int ret;
1377 u8 port_mask = BIT(port);
1378
1379 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001380 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
Florian Fainelli18bd5942018-04-02 16:24:14 -07001381 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001382 mutex_unlock(&priv->reg_mutex);
1383
1384 return ret;
1385}
1386
1387static int
1388mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001389 dsa_fdb_dump_cb_t *cb, void *data)
Sean Wangb8f126a2017-04-07 16:45:09 +08001390{
1391 struct mt7530_priv *priv = ds->priv;
1392 struct mt7530_fdb _fdb = { 0 };
1393 int cnt = MT7530_NUM_FDB_RECORDS;
1394 int ret = 0;
1395 u32 rsp = 0;
1396
1397 mutex_lock(&priv->reg_mutex);
1398
1399 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1400 if (ret < 0)
1401 goto err;
1402
1403 do {
1404 if (rsp & ATC_SRCH_HIT) {
1405 mt7530_fdb_read(priv, &_fdb);
1406 if (_fdb.port_mask & BIT(port)) {
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001407 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1408 data);
Sean Wangb8f126a2017-04-07 16:45:09 +08001409 if (ret < 0)
1410 break;
1411 }
1412 }
1413 } while (--cnt &&
1414 !(rsp & ATC_SRCH_END) &&
1415 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1416err:
1417 mutex_unlock(&priv->reg_mutex);
1418
1419 return 0;
1420}
1421
Sean Wang83163f72017-12-15 12:47:00 +08001422static int
DENG Qingfang5a308332021-03-16 01:09:40 +08001423mt7530_port_mdb_add(struct dsa_switch *ds, int port,
1424 const struct switchdev_obj_port_mdb *mdb)
1425{
1426 struct mt7530_priv *priv = ds->priv;
1427 const u8 *addr = mdb->addr;
1428 u16 vid = mdb->vid;
1429 u8 port_mask = 0;
1430 int ret;
1431
1432 mutex_lock(&priv->reg_mutex);
1433
1434 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1435 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1436 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1437 & PORT_MAP_MASK;
1438
1439 port_mask |= BIT(port);
1440 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1441 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1442
1443 mutex_unlock(&priv->reg_mutex);
1444
1445 return ret;
1446}
1447
1448static int
1449mt7530_port_mdb_del(struct dsa_switch *ds, int port,
1450 const struct switchdev_obj_port_mdb *mdb)
1451{
1452 struct mt7530_priv *priv = ds->priv;
1453 const u8 *addr = mdb->addr;
1454 u16 vid = mdb->vid;
1455 u8 port_mask = 0;
1456 int ret;
1457
1458 mutex_lock(&priv->reg_mutex);
1459
1460 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1461 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1462 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1463 & PORT_MAP_MASK;
1464
1465 port_mask &= ~BIT(port);
1466 mt7530_fdb_write(priv, vid, port_mask, addr, -1,
1467 port_mask ? STATIC_ENT : STATIC_EMP);
1468 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1469
1470 mutex_unlock(&priv->reg_mutex);
1471
1472 return ret;
1473}
1474
1475static int
Sean Wang83163f72017-12-15 12:47:00 +08001476mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1477{
1478 struct mt7530_dummy_poll p;
1479 u32 val;
1480 int ret;
1481
1482 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1483 mt7530_write(priv, MT7530_VTCR, val);
1484
1485 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1486 ret = readx_poll_timeout(_mt7530_read, &p, val,
1487 !(val & VTCR_BUSY), 20, 20000);
1488 if (ret < 0) {
1489 dev_err(priv->dev, "poll timeout\n");
1490 return ret;
1491 }
1492
1493 val = mt7530_read(priv, MT7530_VTCR);
1494 if (val & VTCR_INVALID) {
1495 dev_err(priv->dev, "read VTCR invalid\n");
1496 return -EINVAL;
1497 }
1498
1499 return 0;
1500}
1501
1502static int
Vladimir Oltean89153ed2021-02-13 22:43:19 +02001503mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1504 struct netlink_ext_ack *extack)
Sean Wang83163f72017-12-15 12:47:00 +08001505{
Sean Wang83163f72017-12-15 12:47:00 +08001506 if (vlan_filtering) {
1507 /* The port is being kept as VLAN-unaware port when bridge is
1508 * set up with vlan_filtering not being set, Otherwise, the
1509 * port and the corresponding CPU port is required the setup
1510 * for becoming a VLAN-aware port.
1511 */
1512 mt7530_port_set_vlan_aware(ds, port);
1513 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
Vladimir Olteane3ee07d2019-04-28 21:45:47 +03001514 } else {
1515 mt7530_port_set_vlan_unaware(ds, port);
Sean Wang83163f72017-12-15 12:47:00 +08001516 }
1517
1518 return 0;
1519}
1520
Sean Wang83163f72017-12-15 12:47:00 +08001521static void
1522mt7530_hw_vlan_add(struct mt7530_priv *priv,
1523 struct mt7530_hw_vlan_entry *entry)
1524{
1525 u8 new_members;
1526 u32 val;
1527
1528 new_members = entry->old_members | BIT(entry->port) |
1529 BIT(MT7530_CPU_PORT);
1530
1531 /* Validate the entry with independent learning, create egress tag per
1532 * VLAN and joining the port as one of the port members.
1533 */
DENG Qingfang60871752021-08-04 00:04:02 +08001534 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | FID(FID_BRIDGED) |
1535 VLAN_VALID;
Sean Wang83163f72017-12-15 12:47:00 +08001536 mt7530_write(priv, MT7530_VAWD1, val);
1537
1538 /* Decide whether adding tag or not for those outgoing packets from the
1539 * port inside the VLAN.
1540 */
1541 val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1542 MT7530_VLAN_EGRESS_TAG;
1543 mt7530_rmw(priv, MT7530_VAWD2,
1544 ETAG_CTRL_P_MASK(entry->port),
1545 ETAG_CTRL_P(entry->port, val));
1546
1547 /* CPU port is always taken as a tagged port for serving more than one
1548 * VLANs across and also being applied with egress type stack mode for
1549 * that VLAN tags would be appended after hardware special tag used as
1550 * DSA tag.
1551 */
1552 mt7530_rmw(priv, MT7530_VAWD2,
1553 ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1554 ETAG_CTRL_P(MT7530_CPU_PORT,
1555 MT7530_VLAN_EGRESS_STACK));
1556}
1557
1558static void
1559mt7530_hw_vlan_del(struct mt7530_priv *priv,
1560 struct mt7530_hw_vlan_entry *entry)
1561{
1562 u8 new_members;
1563 u32 val;
1564
1565 new_members = entry->old_members & ~BIT(entry->port);
1566
1567 val = mt7530_read(priv, MT7530_VAWD1);
1568 if (!(val & VLAN_VALID)) {
1569 dev_err(priv->dev,
1570 "Cannot be deleted due to invalid entry\n");
1571 return;
1572 }
1573
1574 /* If certain member apart from CPU port is still alive in the VLAN,
1575 * the entry would be kept valid. Otherwise, the entry is got to be
1576 * disabled.
1577 */
1578 if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1579 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1580 VLAN_VALID;
1581 mt7530_write(priv, MT7530_VAWD1, val);
1582 } else {
1583 mt7530_write(priv, MT7530_VAWD1, 0);
1584 mt7530_write(priv, MT7530_VAWD2, 0);
1585 }
1586}
1587
1588static void
1589mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1590 struct mt7530_hw_vlan_entry *entry,
1591 mt7530_vlan_op vlan_op)
1592{
1593 u32 val;
1594
1595 /* Fetch entry */
1596 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1597
1598 val = mt7530_read(priv, MT7530_VAWD1);
1599
1600 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1601
1602 /* Manipulate entry */
1603 vlan_op(priv, entry);
1604
1605 /* Flush result to hardware */
1606 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1607}
1608
Vladimir Oltean1958d582021-01-09 02:01:53 +02001609static int
Sean Wang83163f72017-12-15 12:47:00 +08001610mt7530_port_vlan_add(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001611 const struct switchdev_obj_port_vlan *vlan,
1612 struct netlink_ext_ack *extack)
Sean Wang83163f72017-12-15 12:47:00 +08001613{
1614 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1615 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1616 struct mt7530_hw_vlan_entry new_entry;
1617 struct mt7530_priv *priv = ds->priv;
Sean Wang83163f72017-12-15 12:47:00 +08001618
Sean Wang83163f72017-12-15 12:47:00 +08001619 mutex_lock(&priv->reg_mutex);
1620
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001621 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1622 mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
Sean Wang83163f72017-12-15 12:47:00 +08001623
1624 if (pvid) {
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001625 priv->ports[port].pvid = vlan->vid;
DENG Qingfang60871752021-08-04 00:04:02 +08001626
1627 /* Only configure PVID if VLAN filtering is enabled */
1628 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1629 mt7530_rmw(priv, MT7530_PPBV1_P(port),
1630 G0_PORT_VID_MASK,
1631 G0_PORT_VID(vlan->vid));
Sean Wang83163f72017-12-15 12:47:00 +08001632 }
1633
1634 mutex_unlock(&priv->reg_mutex);
Vladimir Oltean1958d582021-01-09 02:01:53 +02001635
1636 return 0;
Sean Wang83163f72017-12-15 12:47:00 +08001637}
1638
1639static int
1640mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1641 const struct switchdev_obj_port_vlan *vlan)
1642{
1643 struct mt7530_hw_vlan_entry target_entry;
1644 struct mt7530_priv *priv = ds->priv;
Sean Wang83163f72017-12-15 12:47:00 +08001645
Sean Wang83163f72017-12-15 12:47:00 +08001646 mutex_lock(&priv->reg_mutex);
1647
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001648 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1649 mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
1650 mt7530_hw_vlan_del);
Sean Wang83163f72017-12-15 12:47:00 +08001651
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001652 /* PVID is being restored to the default whenever the PVID port
1653 * is being removed from the VLAN.
1654 */
DENG Qingfang60871752021-08-04 00:04:02 +08001655 if (priv->ports[port].pvid == vlan->vid) {
1656 priv->ports[port].pvid = G0_PORT_VID_DEF;
1657 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1658 G0_PORT_VID_DEF);
1659 }
Sean Wang83163f72017-12-15 12:47:00 +08001660
Sean Wang83163f72017-12-15 12:47:00 +08001661
1662 mutex_unlock(&priv->reg_mutex);
1663
1664 return 0;
1665}
1666
Landen Chaoc2885752020-09-11 21:48:54 +08001667static int mt753x_mirror_port_get(unsigned int id, u32 val)
1668{
1669 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) :
1670 MIRROR_PORT(val);
1671}
1672
1673static int mt753x_mirror_port_set(unsigned int id, u32 val)
1674{
1675 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) :
1676 MIRROR_PORT(val);
1677}
1678
1679static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
DENG Qingfang37feab62020-03-06 20:35:35 +08001680 struct dsa_mall_mirror_tc_entry *mirror,
1681 bool ingress)
1682{
1683 struct mt7530_priv *priv = ds->priv;
Landen Chaoc2885752020-09-11 21:48:54 +08001684 int monitor_port;
DENG Qingfang37feab62020-03-06 20:35:35 +08001685 u32 val;
1686
1687 /* Check for existent entry */
1688 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1689 return -EEXIST;
1690
Landen Chaoc2885752020-09-11 21:48:54 +08001691 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
DENG Qingfang37feab62020-03-06 20:35:35 +08001692
1693 /* MT7530 only supports one monitor port */
Landen Chaoc2885752020-09-11 21:48:54 +08001694 monitor_port = mt753x_mirror_port_get(priv->id, val);
1695 if (val & MT753X_MIRROR_EN(priv->id) &&
1696 monitor_port != mirror->to_local_port)
DENG Qingfang37feab62020-03-06 20:35:35 +08001697 return -EEXIST;
1698
Landen Chaoc2885752020-09-11 21:48:54 +08001699 val |= MT753X_MIRROR_EN(priv->id);
1700 val &= ~MT753X_MIRROR_MASK(priv->id);
1701 val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port);
1702 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
DENG Qingfang37feab62020-03-06 20:35:35 +08001703
1704 val = mt7530_read(priv, MT7530_PCR_P(port));
1705 if (ingress) {
1706 val |= PORT_RX_MIR;
1707 priv->mirror_rx |= BIT(port);
1708 } else {
1709 val |= PORT_TX_MIR;
1710 priv->mirror_tx |= BIT(port);
1711 }
1712 mt7530_write(priv, MT7530_PCR_P(port), val);
1713
1714 return 0;
1715}
1716
Landen Chaoc2885752020-09-11 21:48:54 +08001717static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
DENG Qingfang37feab62020-03-06 20:35:35 +08001718 struct dsa_mall_mirror_tc_entry *mirror)
1719{
1720 struct mt7530_priv *priv = ds->priv;
1721 u32 val;
1722
1723 val = mt7530_read(priv, MT7530_PCR_P(port));
1724 if (mirror->ingress) {
1725 val &= ~PORT_RX_MIR;
1726 priv->mirror_rx &= ~BIT(port);
1727 } else {
1728 val &= ~PORT_TX_MIR;
1729 priv->mirror_tx &= ~BIT(port);
1730 }
1731 mt7530_write(priv, MT7530_PCR_P(port), val);
1732
1733 if (!priv->mirror_rx && !priv->mirror_tx) {
Landen Chaoc2885752020-09-11 21:48:54 +08001734 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1735 val &= ~MT753X_MIRROR_EN(priv->id);
1736 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
DENG Qingfang37feab62020-03-06 20:35:35 +08001737 }
1738}
1739
Sean Wangb8f126a2017-04-07 16:45:09 +08001740static enum dsa_tag_protocol
Florian Fainelli4d776482020-01-07 21:06:05 -08001741mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1742 enum dsa_tag_protocol mp)
Sean Wangb8f126a2017-04-07 16:45:09 +08001743{
Vladimir Oltean244f8a82021-07-31 01:57:14 +03001744 return DSA_TAG_PROTO_MTK;
Sean Wangb8f126a2017-04-07 16:45:09 +08001745}
1746
DENG Qingfang63c75c02021-02-26 14:32:26 +08001747#ifdef CONFIG_GPIOLIB
DENG Qingfang429a0ed2021-01-25 12:43:22 +08001748static inline u32
1749mt7530_gpio_to_bit(unsigned int offset)
1750{
1751 /* Map GPIO offset to register bit
1752 * [ 2: 0] port 0 LED 0..2 as GPIO 0..2
1753 * [ 6: 4] port 1 LED 0..2 as GPIO 3..5
1754 * [10: 8] port 2 LED 0..2 as GPIO 6..8
1755 * [14:12] port 3 LED 0..2 as GPIO 9..11
1756 * [18:16] port 4 LED 0..2 as GPIO 12..14
1757 */
1758 return BIT(offset + offset / 3);
1759}
1760
1761static int
1762mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
1763{
1764 struct mt7530_priv *priv = gpiochip_get_data(gc);
1765 u32 bit = mt7530_gpio_to_bit(offset);
1766
1767 return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
1768}
1769
1770static void
1771mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
1772{
1773 struct mt7530_priv *priv = gpiochip_get_data(gc);
1774 u32 bit = mt7530_gpio_to_bit(offset);
1775
1776 if (value)
1777 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1778 else
1779 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1780}
1781
1782static int
1783mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
1784{
1785 struct mt7530_priv *priv = gpiochip_get_data(gc);
1786 u32 bit = mt7530_gpio_to_bit(offset);
1787
1788 return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
1789 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1790}
1791
1792static int
1793mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
1794{
1795 struct mt7530_priv *priv = gpiochip_get_data(gc);
1796 u32 bit = mt7530_gpio_to_bit(offset);
1797
1798 mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
1799 mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
1800
1801 return 0;
1802}
1803
1804static int
1805mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
1806{
1807 struct mt7530_priv *priv = gpiochip_get_data(gc);
1808 u32 bit = mt7530_gpio_to_bit(offset);
1809
1810 mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
1811
1812 if (value)
1813 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1814 else
1815 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1816
1817 mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
1818
1819 return 0;
1820}
1821
1822static int
1823mt7530_setup_gpio(struct mt7530_priv *priv)
1824{
1825 struct device *dev = priv->dev;
1826 struct gpio_chip *gc;
1827
1828 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
1829 if (!gc)
1830 return -ENOMEM;
1831
1832 mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
1833 mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
1834 mt7530_write(priv, MT7530_LED_IO_MODE, 0);
1835
1836 gc->label = "mt7530";
1837 gc->parent = dev;
1838 gc->owner = THIS_MODULE;
1839 gc->get_direction = mt7530_gpio_get_direction;
1840 gc->direction_input = mt7530_gpio_direction_input;
1841 gc->direction_output = mt7530_gpio_direction_output;
1842 gc->get = mt7530_gpio_get;
1843 gc->set = mt7530_gpio_set;
1844 gc->base = -1;
1845 gc->ngpio = 15;
1846 gc->can_sleep = true;
1847
1848 return devm_gpiochip_add_data(dev, gc, priv);
1849}
DENG Qingfang63c75c02021-02-26 14:32:26 +08001850#endif /* CONFIG_GPIOLIB */
DENG Qingfang429a0ed2021-01-25 12:43:22 +08001851
DENG Qingfangba751e22021-05-19 11:32:00 +08001852static irqreturn_t
1853mt7530_irq_thread_fn(int irq, void *dev_id)
1854{
1855 struct mt7530_priv *priv = dev_id;
1856 bool handled = false;
1857 u32 val;
1858 int p;
1859
1860 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1861 val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
1862 mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
1863 mutex_unlock(&priv->bus->mdio_lock);
1864
1865 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1866 if (BIT(p) & val) {
1867 unsigned int irq;
1868
1869 irq = irq_find_mapping(priv->irq_domain, p);
1870 handle_nested_irq(irq);
1871 handled = true;
1872 }
1873 }
1874
1875 return IRQ_RETVAL(handled);
1876}
1877
1878static void
1879mt7530_irq_mask(struct irq_data *d)
1880{
1881 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1882
1883 priv->irq_enable &= ~BIT(d->hwirq);
1884}
1885
1886static void
1887mt7530_irq_unmask(struct irq_data *d)
1888{
1889 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1890
1891 priv->irq_enable |= BIT(d->hwirq);
1892}
1893
1894static void
1895mt7530_irq_bus_lock(struct irq_data *d)
1896{
1897 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1898
1899 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1900}
1901
1902static void
1903mt7530_irq_bus_sync_unlock(struct irq_data *d)
1904{
1905 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1906
1907 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
1908 mutex_unlock(&priv->bus->mdio_lock);
1909}
1910
1911static struct irq_chip mt7530_irq_chip = {
1912 .name = KBUILD_MODNAME,
1913 .irq_mask = mt7530_irq_mask,
1914 .irq_unmask = mt7530_irq_unmask,
1915 .irq_bus_lock = mt7530_irq_bus_lock,
1916 .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
1917};
1918
1919static int
1920mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
1921 irq_hw_number_t hwirq)
1922{
1923 irq_set_chip_data(irq, domain->host_data);
1924 irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
1925 irq_set_nested_thread(irq, true);
1926 irq_set_noprobe(irq);
1927
1928 return 0;
1929}
1930
1931static const struct irq_domain_ops mt7530_irq_domain_ops = {
1932 .map = mt7530_irq_map,
1933 .xlate = irq_domain_xlate_onecell,
1934};
1935
1936static void
1937mt7530_setup_mdio_irq(struct mt7530_priv *priv)
1938{
1939 struct dsa_switch *ds = priv->ds;
1940 int p;
1941
1942 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1943 if (BIT(p) & ds->phys_mii_mask) {
1944 unsigned int irq;
1945
1946 irq = irq_create_mapping(priv->irq_domain, p);
1947 ds->slave_mii_bus->irq[p] = irq;
1948 }
1949 }
1950}
1951
1952static int
1953mt7530_setup_irq(struct mt7530_priv *priv)
1954{
1955 struct device *dev = priv->dev;
1956 struct device_node *np = dev->of_node;
1957 int ret;
1958
1959 if (!of_property_read_bool(np, "interrupt-controller")) {
1960 dev_info(dev, "no interrupt support\n");
1961 return 0;
1962 }
1963
1964 priv->irq = of_irq_get(np, 0);
1965 if (priv->irq <= 0) {
1966 dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
1967 return priv->irq ? : -EINVAL;
1968 }
1969
1970 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
1971 &mt7530_irq_domain_ops, priv);
1972 if (!priv->irq_domain) {
1973 dev_err(dev, "failed to create IRQ domain\n");
1974 return -ENOMEM;
1975 }
1976
1977 /* This register must be set for MT7530 to properly fire interrupts */
1978 if (priv->id != ID_MT7531)
1979 mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
1980
1981 ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
1982 IRQF_ONESHOT, KBUILD_MODNAME, priv);
1983 if (ret) {
1984 irq_domain_remove(priv->irq_domain);
1985 dev_err(dev, "failed to request IRQ: %d\n", ret);
1986 return ret;
1987 }
1988
1989 return 0;
1990}
1991
1992static void
1993mt7530_free_mdio_irq(struct mt7530_priv *priv)
1994{
1995 int p;
1996
1997 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1998 if (BIT(p) & priv->ds->phys_mii_mask) {
1999 unsigned int irq;
2000
2001 irq = irq_find_mapping(priv->irq_domain, p);
2002 irq_dispose_mapping(irq);
2003 }
2004 }
2005}
2006
2007static void
2008mt7530_free_irq_common(struct mt7530_priv *priv)
2009{
2010 free_irq(priv->irq, priv);
2011 irq_domain_remove(priv->irq_domain);
2012}
2013
2014static void
2015mt7530_free_irq(struct mt7530_priv *priv)
2016{
2017 mt7530_free_mdio_irq(priv);
2018 mt7530_free_irq_common(priv);
2019}
2020
2021static int
2022mt7530_setup_mdio(struct mt7530_priv *priv)
2023{
2024 struct dsa_switch *ds = priv->ds;
2025 struct device *dev = priv->dev;
2026 struct mii_bus *bus;
2027 static int idx;
2028 int ret;
2029
2030 bus = devm_mdiobus_alloc(dev);
2031 if (!bus)
2032 return -ENOMEM;
2033
2034 ds->slave_mii_bus = bus;
2035 bus->priv = priv;
2036 bus->name = KBUILD_MODNAME "-mii";
2037 snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
2038 bus->read = mt753x_phy_read;
2039 bus->write = mt753x_phy_write;
2040 bus->parent = dev;
2041 bus->phy_mask = ~ds->phys_mii_mask;
2042
2043 if (priv->irq)
2044 mt7530_setup_mdio_irq(priv);
2045
2046 ret = mdiobus_register(bus);
2047 if (ret) {
2048 dev_err(dev, "failed to register MDIO bus: %d\n", ret);
2049 if (priv->irq)
2050 mt7530_free_mdio_irq(priv);
2051 }
2052
2053 return ret;
2054}
2055
Sean Wangb8f126a2017-04-07 16:45:09 +08002056static int
2057mt7530_setup(struct dsa_switch *ds)
2058{
2059 struct mt7530_priv *priv = ds->priv;
René van Dorst38f790a2019-09-02 15:02:26 +02002060 struct device_node *phy_node;
2061 struct device_node *mac_np;
Sean Wangb8f126a2017-04-07 16:45:09 +08002062 struct mt7530_dummy_poll p;
René van Dorst38f790a2019-09-02 15:02:26 +02002063 phy_interface_t interface;
René van Dorstca366d62019-09-02 15:02:24 +02002064 struct device_node *dn;
2065 u32 id, val;
2066 int ret, i;
Sean Wangb8f126a2017-04-07 16:45:09 +08002067
Vivien Didelot0abfd492017-09-20 12:28:05 -04002068 /* The parent node of master netdev which holds the common system
Sean Wangb8f126a2017-04-07 16:45:09 +08002069 * controller also is the container for two GMACs nodes representing
2070 * as two netdev instances.
2071 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04002072 dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
DENG Qingfang0b69c542021-08-04 00:04:01 +08002073 ds->assisted_learning_on_cpu_port = true;
DENG Qingfang771c8902020-12-11 01:03:22 +08002074 ds->mtu_enforcement_ingress = true;
Sean Wangb8f126a2017-04-07 16:45:09 +08002075
Greg Ungererddda1ac2019-01-30 11:24:05 +10002076 if (priv->id == ID_MT7530) {
Greg Ungererddda1ac2019-01-30 11:24:05 +10002077 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
2078 ret = regulator_enable(priv->core_pwr);
2079 if (ret < 0) {
2080 dev_err(priv->dev,
2081 "Failed to enable core power: %d\n", ret);
2082 return ret;
2083 }
2084
2085 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
2086 ret = regulator_enable(priv->io_pwr);
2087 if (ret < 0) {
2088 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
2089 ret);
2090 return ret;
2091 }
Sean Wangb8f126a2017-04-07 16:45:09 +08002092 }
2093
2094 /* Reset whole chip through gpio pin or memory-mapped registers for
2095 * different type of hardware
2096 */
2097 if (priv->mcm) {
2098 reset_control_assert(priv->rstc);
2099 usleep_range(1000, 1100);
2100 reset_control_deassert(priv->rstc);
2101 } else {
2102 gpiod_set_value_cansleep(priv->reset, 0);
2103 usleep_range(1000, 1100);
2104 gpiod_set_value_cansleep(priv->reset, 1);
2105 }
2106
2107 /* Waiting for MT7530 got to stable */
2108 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2109 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2110 20, 1000000);
2111 if (ret < 0) {
2112 dev_err(priv->dev, "reset timeout\n");
2113 return ret;
2114 }
2115
2116 id = mt7530_read(priv, MT7530_CREV);
2117 id >>= CHIP_NAME_SHIFT;
2118 if (id != MT7530_ID) {
2119 dev_err(priv->dev, "chip %x can't be supported\n", id);
2120 return -ENODEV;
2121 }
2122
2123 /* Reset the switch through internal reset */
2124 mt7530_write(priv, MT7530_SYS_CTRL,
2125 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2126 SYS_CTRL_REG_RST);
2127
2128 /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
2129 val = mt7530_read(priv, MT7530_MHWTRAP);
2130 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
2131 val |= MHWTRAP_MANUAL;
2132 mt7530_write(priv, MT7530_MHWTRAP, val);
2133
René van Dorstca366d62019-09-02 15:02:24 +02002134 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2135
Sean Wangb8f126a2017-04-07 16:45:09 +08002136 /* Enable and reset MIB counters */
2137 mt7530_mib_reset(ds);
2138
Sean Wangb8f126a2017-04-07 16:45:09 +08002139 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2140 /* Disable forwarding by default on all ports */
2141 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2142 PCR_MATRIX_CLR);
2143
DENG Qingfang0b69c542021-08-04 00:04:01 +08002144 /* Disable learning by default on all ports */
2145 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2146
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002147 if (dsa_is_cpu_port(ds, i)) {
2148 ret = mt753x_cpu_port_enable(ds, i);
2149 if (ret)
2150 return ret;
DENG Qingfang5a308332021-03-16 01:09:40 +08002151 } else {
Andrew Lunn75104db2019-02-24 20:44:43 +01002152 mt7530_port_disable(ds, i);
DENG Qingfang60871752021-08-04 00:04:02 +08002153
2154 /* Set default PVID to 0 on all user ports */
2155 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2156 G0_PORT_VID_DEF);
DENG Qingfang5a308332021-03-16 01:09:40 +08002157 }
DENG Qingfange0451242020-04-14 14:34:08 +08002158 /* Enable consistent egress tag */
2159 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2160 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wangb8f126a2017-04-07 16:45:09 +08002161 }
2162
René van Dorst38f790a2019-09-02 15:02:26 +02002163 /* Setup port 5 */
2164 priv->p5_intf_sel = P5_DISABLED;
2165 interface = PHY_INTERFACE_MODE_NA;
2166
2167 if (!dsa_is_unused_port(ds, 5)) {
2168 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002169 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
2170 if (ret && ret != -ENODEV)
2171 return ret;
René van Dorst38f790a2019-09-02 15:02:26 +02002172 } else {
2173 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
2174 for_each_child_of_node(dn, mac_np) {
2175 if (!of_device_is_compatible(mac_np,
2176 "mediatek,eth-mac"))
2177 continue;
2178
2179 ret = of_property_read_u32(mac_np, "reg", &id);
2180 if (ret < 0 || id != 1)
2181 continue;
2182
2183 phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
Chuanhong Guo04528002020-04-03 19:28:24 +08002184 if (!phy_node)
2185 continue;
2186
René van Dorst38f790a2019-09-02 15:02:26 +02002187 if (phy_node->parent == priv->dev->of_node->parent) {
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002188 ret = of_get_phy_mode(mac_np, &interface);
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302189 if (ret && ret != -ENODEV) {
2190 of_node_put(mac_np);
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002191 return ret;
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302192 }
René van Dorst38f790a2019-09-02 15:02:26 +02002193 id = of_mdio_parse_addr(ds->dev, phy_node);
2194 if (id == 0)
2195 priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
2196 if (id == 4)
2197 priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
2198 }
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302199 of_node_put(mac_np);
René van Dorst38f790a2019-09-02 15:02:26 +02002200 of_node_put(phy_node);
2201 break;
2202 }
2203 }
2204
DENG Qingfang63c75c02021-02-26 14:32:26 +08002205#ifdef CONFIG_GPIOLIB
DENG Qingfang429a0ed2021-01-25 12:43:22 +08002206 if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
2207 ret = mt7530_setup_gpio(priv);
2208 if (ret)
2209 return ret;
2210 }
DENG Qingfang63c75c02021-02-26 14:32:26 +08002211#endif /* CONFIG_GPIOLIB */
DENG Qingfang429a0ed2021-01-25 12:43:22 +08002212
René van Dorst38f790a2019-09-02 15:02:26 +02002213 mt7530_setup_port5(ds, interface);
2214
Sean Wangb8f126a2017-04-07 16:45:09 +08002215 /* Flush the FDB table */
Florian Fainelli18bd5942018-04-02 16:24:14 -07002216 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08002217 if (ret < 0)
2218 return ret;
2219
2220 return 0;
2221}
2222
Landen Chaoc2885752020-09-11 21:48:54 +08002223static int
2224mt7531_setup(struct dsa_switch *ds)
2225{
2226 struct mt7530_priv *priv = ds->priv;
2227 struct mt7530_dummy_poll p;
2228 u32 val, id;
2229 int ret, i;
2230
2231 /* Reset whole chip through gpio pin or memory-mapped registers for
2232 * different type of hardware
2233 */
2234 if (priv->mcm) {
2235 reset_control_assert(priv->rstc);
2236 usleep_range(1000, 1100);
2237 reset_control_deassert(priv->rstc);
2238 } else {
2239 gpiod_set_value_cansleep(priv->reset, 0);
2240 usleep_range(1000, 1100);
2241 gpiod_set_value_cansleep(priv->reset, 1);
2242 }
2243
2244 /* Waiting for MT7530 got to stable */
2245 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2246 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2247 20, 1000000);
2248 if (ret < 0) {
2249 dev_err(priv->dev, "reset timeout\n");
2250 return ret;
2251 }
2252
2253 id = mt7530_read(priv, MT7531_CREV);
2254 id >>= CHIP_NAME_SHIFT;
2255
2256 if (id != MT7531_ID) {
2257 dev_err(priv->dev, "chip %x can't be supported\n", id);
2258 return -ENODEV;
2259 }
2260
2261 /* Reset the switch through internal reset */
2262 mt7530_write(priv, MT7530_SYS_CTRL,
2263 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2264 SYS_CTRL_REG_RST);
2265
2266 if (mt7531_dual_sgmii_supported(priv)) {
2267 priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII;
2268
2269 /* Let ds->slave_mii_bus be able to access external phy. */
2270 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
2271 MT7531_EXT_P_MDC_11);
2272 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
2273 MT7531_EXT_P_MDIO_12);
2274 } else {
2275 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
2276 }
2277 dev_dbg(ds->dev, "P5 support %s interface\n",
2278 p5_intf_modes(priv->p5_intf_sel));
2279
2280 mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
2281 MT7531_GPIO0_INTERRUPT);
2282
2283 /* Let phylink decide the interface later. */
2284 priv->p5_interface = PHY_INTERFACE_MODE_NA;
2285 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2286
2287 /* Enable PHY core PLL, since phy_device has not yet been created
2288 * provided for phy_[read,write]_mmd_indirect is called, we provide
2289 * our own mt7531_ind_mmd_phy_[read,write] to complete this
2290 * function.
2291 */
2292 val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR,
2293 MDIO_MMD_VEND2, CORE_PLL_GROUP4);
2294 val |= MT7531_PHY_PLL_BYPASS_MODE;
2295 val &= ~MT7531_PHY_PLL_OFF;
2296 mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2,
2297 CORE_PLL_GROUP4, val);
2298
2299 /* BPDU to CPU port */
2300 mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK,
2301 BIT(MT7530_CPU_PORT));
2302 mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK,
2303 MT753X_BPDU_CPU_ONLY);
2304
2305 /* Enable and reset MIB counters */
2306 mt7530_mib_reset(ds);
2307
2308 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2309 /* Disable forwarding by default on all ports */
2310 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2311 PCR_MATRIX_CLR);
2312
DENG Qingfang0b69c542021-08-04 00:04:01 +08002313 /* Disable learning by default on all ports */
2314 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2315
Landen Chaoc2885752020-09-11 21:48:54 +08002316 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
2317
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002318 if (dsa_is_cpu_port(ds, i)) {
2319 ret = mt753x_cpu_port_enable(ds, i);
2320 if (ret)
2321 return ret;
DENG Qingfang5a308332021-03-16 01:09:40 +08002322 } else {
Landen Chaoc2885752020-09-11 21:48:54 +08002323 mt7530_port_disable(ds, i);
DENG Qingfang60871752021-08-04 00:04:02 +08002324
2325 /* Set default PVID to 0 on all user ports */
2326 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2327 G0_PORT_VID_DEF);
DENG Qingfang5a308332021-03-16 01:09:40 +08002328 }
2329
Landen Chaoc2885752020-09-11 21:48:54 +08002330 /* Enable consistent egress tag */
2331 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2332 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2333 }
2334
DENG Qingfang0b69c542021-08-04 00:04:01 +08002335 ds->assisted_learning_on_cpu_port = true;
DENG Qingfang771c8902020-12-11 01:03:22 +08002336 ds->mtu_enforcement_ingress = true;
Landen Chaoc2885752020-09-11 21:48:54 +08002337
2338 /* Flush the FDB table */
2339 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2340 if (ret < 0)
2341 return ret;
2342
2343 return 0;
2344}
2345
Landen Chao88bdef82020-09-11 21:48:52 +08002346static bool
2347mt7530_phy_mode_supported(struct dsa_switch *ds, int port,
2348 const struct phylink_link_state *state)
2349{
2350 struct mt7530_priv *priv = ds->priv;
2351
2352 switch (port) {
2353 case 0 ... 4: /* Internal phy */
2354 if (state->interface != PHY_INTERFACE_MODE_GMII)
2355 return false;
2356 break;
2357 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2358 if (!phy_interface_mode_is_rgmii(state->interface) &&
2359 state->interface != PHY_INTERFACE_MODE_MII &&
2360 state->interface != PHY_INTERFACE_MODE_GMII)
2361 return false;
2362 break;
2363 case 6: /* 1st cpu port */
2364 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
2365 state->interface != PHY_INTERFACE_MODE_TRGMII)
2366 return false;
2367 break;
2368 default:
2369 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2370 port);
2371 return false;
2372 }
2373
2374 return true;
2375}
2376
Landen Chaoc2885752020-09-11 21:48:54 +08002377static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port)
2378{
2379 return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII);
2380}
2381
2382static bool
2383mt7531_phy_mode_supported(struct dsa_switch *ds, int port,
2384 const struct phylink_link_state *state)
2385{
2386 struct mt7530_priv *priv = ds->priv;
2387
2388 switch (port) {
2389 case 0 ... 4: /* Internal phy */
2390 if (state->interface != PHY_INTERFACE_MODE_GMII)
2391 return false;
2392 break;
2393 case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */
2394 if (mt7531_is_rgmii_port(priv, port))
2395 return phy_interface_mode_is_rgmii(state->interface);
2396 fallthrough;
2397 case 6: /* 1st cpu port supports sgmii/8023z only */
2398 if (state->interface != PHY_INTERFACE_MODE_SGMII &&
2399 !phy_interface_mode_is_8023z(state->interface))
2400 return false;
2401 break;
2402 default:
2403 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2404 port);
2405 return false;
2406 }
2407
2408 return true;
2409}
2410
Landen Chao88bdef82020-09-11 21:48:52 +08002411static bool
2412mt753x_phy_mode_supported(struct dsa_switch *ds, int port,
2413 const struct phylink_link_state *state)
2414{
2415 struct mt7530_priv *priv = ds->priv;
2416
2417 return priv->info->phy_mode_supported(ds, port, state);
2418}
2419
2420static int
2421mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
2422{
2423 struct mt7530_priv *priv = ds->priv;
2424
2425 return priv->info->pad_setup(ds, state->interface);
2426}
2427
2428static int
2429mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2430 phy_interface_t interface)
2431{
2432 struct mt7530_priv *priv = ds->priv;
2433
2434 /* Only need to setup port5. */
2435 if (port != 5)
2436 return 0;
2437
2438 mt7530_setup_port5(priv->ds, interface);
2439
2440 return 0;
2441}
2442
Landen Chaoc2885752020-09-11 21:48:54 +08002443static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port,
2444 phy_interface_t interface,
2445 struct phy_device *phydev)
2446{
2447 u32 val;
2448
2449 if (!mt7531_is_rgmii_port(priv, port)) {
2450 dev_err(priv->dev, "RGMII mode is not available for port %d\n",
2451 port);
2452 return -EINVAL;
2453 }
2454
2455 val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2456 val |= GP_CLK_EN;
2457 val &= ~GP_MODE_MASK;
2458 val |= GP_MODE(MT7531_GP_MODE_RGMII);
2459 val &= ~CLK_SKEW_IN_MASK;
2460 val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2461 val &= ~CLK_SKEW_OUT_MASK;
2462 val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2463 val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2464
2465 /* Do not adjust rgmii delay when vendor phy driver presents. */
2466 if (!phydev || phy_driver_is_genphy(phydev)) {
2467 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2468 switch (interface) {
2469 case PHY_INTERFACE_MODE_RGMII:
2470 val |= TXCLK_NO_REVERSE;
2471 val |= RXCLK_NO_DELAY;
2472 break;
2473 case PHY_INTERFACE_MODE_RGMII_RXID:
2474 val |= TXCLK_NO_REVERSE;
2475 break;
2476 case PHY_INTERFACE_MODE_RGMII_TXID:
2477 val |= RXCLK_NO_DELAY;
2478 break;
2479 case PHY_INTERFACE_MODE_RGMII_ID:
2480 break;
2481 default:
2482 return -EINVAL;
2483 }
2484 }
2485 mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2486
2487 return 0;
2488}
2489
2490static void mt7531_sgmii_validate(struct mt7530_priv *priv, int port,
2491 unsigned long *supported)
2492{
2493 /* Port5 supports ethier RGMII or SGMII.
2494 * Port6 supports SGMII only.
2495 */
2496 switch (port) {
2497 case 5:
2498 if (mt7531_is_rgmii_port(priv, port))
2499 break;
2500 fallthrough;
2501 case 6:
2502 phylink_set(supported, 1000baseX_Full);
2503 phylink_set(supported, 2500baseX_Full);
2504 phylink_set(supported, 2500baseT_Full);
2505 }
2506}
2507
2508static void
2509mt7531_sgmii_link_up_force(struct dsa_switch *ds, int port,
2510 unsigned int mode, phy_interface_t interface,
2511 int speed, int duplex)
2512{
2513 struct mt7530_priv *priv = ds->priv;
2514 unsigned int val;
2515
2516 /* For adjusting speed and duplex of SGMII force mode. */
2517 if (interface != PHY_INTERFACE_MODE_SGMII ||
2518 phylink_autoneg_inband(mode))
2519 return;
2520
2521 /* SGMII force mode setting */
2522 val = mt7530_read(priv, MT7531_SGMII_MODE(port));
2523 val &= ~MT7531_SGMII_IF_MODE_MASK;
2524
2525 switch (speed) {
2526 case SPEED_10:
2527 val |= MT7531_SGMII_FORCE_SPEED_10;
2528 break;
2529 case SPEED_100:
2530 val |= MT7531_SGMII_FORCE_SPEED_100;
2531 break;
2532 case SPEED_1000:
2533 val |= MT7531_SGMII_FORCE_SPEED_1000;
2534 break;
2535 }
2536
2537 /* MT7531 SGMII 1G force mode can only work in full duplex mode,
2538 * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2539 */
2540 if ((speed == SPEED_10 || speed == SPEED_100) &&
2541 duplex != DUPLEX_FULL)
2542 val |= MT7531_SGMII_FORCE_HALF_DUPLEX;
2543
2544 mt7530_write(priv, MT7531_SGMII_MODE(port), val);
2545}
2546
2547static bool mt753x_is_mac_port(u32 port)
2548{
2549 return (port == 5 || port == 6);
2550}
2551
2552static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port,
2553 phy_interface_t interface)
2554{
2555 u32 val;
2556
2557 if (!mt753x_is_mac_port(port))
2558 return -EINVAL;
2559
2560 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2561 MT7531_SGMII_PHYA_PWD);
2562
2563 val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port));
2564 val &= ~MT7531_RG_TPHY_SPEED_MASK;
2565 /* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B
2566 * encoding.
2567 */
2568 val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ?
2569 MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G;
2570 mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val);
2571
2572 mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2573
2574 /* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex
2575 * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2576 */
2577 mt7530_rmw(priv, MT7531_SGMII_MODE(port),
2578 MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS,
2579 MT7531_SGMII_FORCE_SPEED_1000);
2580
2581 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2582
2583 return 0;
2584}
2585
2586static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port,
2587 phy_interface_t interface)
2588{
2589 if (!mt753x_is_mac_port(port))
2590 return -EINVAL;
2591
2592 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2593 MT7531_SGMII_PHYA_PWD);
2594
2595 mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
2596 MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G);
2597
2598 mt7530_set(priv, MT7531_SGMII_MODE(port),
2599 MT7531_SGMII_REMOTE_FAULT_DIS |
2600 MT7531_SGMII_SPEED_DUPLEX_AN);
2601
2602 mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port),
2603 MT7531_SGMII_TX_CONFIG_MASK, 1);
2604
2605 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2606
2607 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART);
2608
2609 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2610
2611 return 0;
2612}
2613
2614static void mt7531_sgmii_restart_an(struct dsa_switch *ds, int port)
2615{
2616 struct mt7530_priv *priv = ds->priv;
2617 u32 val;
2618
2619 /* Only restart AN when AN is enabled */
2620 val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2621 if (val & MT7531_SGMII_AN_ENABLE) {
2622 val |= MT7531_SGMII_AN_RESTART;
2623 mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val);
2624 }
2625}
2626
2627static int
2628mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2629 phy_interface_t interface)
2630{
2631 struct mt7530_priv *priv = ds->priv;
2632 struct phy_device *phydev;
2633 struct dsa_port *dp;
2634
2635 if (!mt753x_is_mac_port(port)) {
2636 dev_err(priv->dev, "port %d is not a MAC port\n", port);
2637 return -EINVAL;
2638 }
2639
2640 switch (interface) {
2641 case PHY_INTERFACE_MODE_RGMII:
2642 case PHY_INTERFACE_MODE_RGMII_ID:
2643 case PHY_INTERFACE_MODE_RGMII_RXID:
2644 case PHY_INTERFACE_MODE_RGMII_TXID:
2645 dp = dsa_to_port(ds, port);
2646 phydev = dp->slave->phydev;
2647 return mt7531_rgmii_setup(priv, port, interface, phydev);
2648 case PHY_INTERFACE_MODE_SGMII:
2649 return mt7531_sgmii_setup_mode_an(priv, port, interface);
2650 case PHY_INTERFACE_MODE_NA:
2651 case PHY_INTERFACE_MODE_1000BASEX:
2652 case PHY_INTERFACE_MODE_2500BASEX:
2653 if (phylink_autoneg_inband(mode))
2654 return -EINVAL;
2655
2656 return mt7531_sgmii_setup_mode_force(priv, port, interface);
2657 default:
2658 return -EINVAL;
2659 }
2660
2661 return -EINVAL;
2662}
2663
Landen Chao88bdef82020-09-11 21:48:52 +08002664static int
2665mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2666 const struct phylink_link_state *state)
2667{
2668 struct mt7530_priv *priv = ds->priv;
2669
2670 return priv->info->mac_port_config(ds, port, mode, state->interface);
2671}
2672
2673static void
2674mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2675 const struct phylink_link_state *state)
René van Dorstca366d62019-09-02 15:02:24 +02002676{
2677 struct mt7530_priv *priv = ds->priv;
2678 u32 mcr_cur, mcr_new;
2679
Landen Chao88bdef82020-09-11 21:48:52 +08002680 if (!mt753x_phy_mode_supported(ds, port, state))
2681 goto unsupported;
2682
René van Dorstca366d62019-09-02 15:02:24 +02002683 switch (port) {
Landen Chao88bdef82020-09-11 21:48:52 +08002684 case 0 ... 4: /* Internal phy */
René van Dorstca366d62019-09-02 15:02:24 +02002685 if (state->interface != PHY_INTERFACE_MODE_GMII)
Landen Chao88bdef82020-09-11 21:48:52 +08002686 goto unsupported;
René van Dorstca366d62019-09-02 15:02:24 +02002687 break;
René van Dorst38f790a2019-09-02 15:02:26 +02002688 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2689 if (priv->p5_interface == state->interface)
2690 break;
René van Dorst38f790a2019-09-02 15:02:26 +02002691
Landen Chao88bdef82020-09-11 21:48:52 +08002692 if (mt753x_mac_config(ds, port, mode, state) < 0)
2693 goto unsupported;
2694
Landen Chaoc2885752020-09-11 21:48:54 +08002695 if (priv->p5_intf_sel != P5_DISABLED)
2696 priv->p5_interface = state->interface;
René van Dorst38f790a2019-09-02 15:02:26 +02002697 break;
René van Dorstca366d62019-09-02 15:02:24 +02002698 case 6: /* 1st cpu port */
2699 if (priv->p6_interface == state->interface)
2700 break;
2701
Landen Chao88bdef82020-09-11 21:48:52 +08002702 mt753x_pad_setup(ds, state);
René van Dorstca366d62019-09-02 15:02:24 +02002703
Landen Chao88bdef82020-09-11 21:48:52 +08002704 if (mt753x_mac_config(ds, port, mode, state) < 0)
2705 goto unsupported;
René van Dorstca366d62019-09-02 15:02:24 +02002706
René van Dorstca366d62019-09-02 15:02:24 +02002707 priv->p6_interface = state->interface;
2708 break;
2709 default:
Landen Chao88bdef82020-09-11 21:48:52 +08002710unsupported:
2711 dev_err(ds->dev, "%s: unsupported %s port: %i\n",
2712 __func__, phy_modes(state->interface), port);
René van Dorstca366d62019-09-02 15:02:24 +02002713 return;
2714 }
2715
Landen Chaoc2885752020-09-11 21:48:54 +08002716 if (phylink_autoneg_inband(mode) &&
2717 state->interface != PHY_INTERFACE_MODE_SGMII) {
René van Dorstca366d62019-09-02 15:02:24 +02002718 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
2719 __func__);
2720 return;
2721 }
2722
2723 mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
2724 mcr_new = mcr_cur;
René van Dorst1d011452020-03-27 15:44:12 +01002725 mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
René van Dorstca366d62019-09-02 15:02:24 +02002726 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
Landen Chaoc2885752020-09-11 21:48:54 +08002727 PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id);
René van Dorstca366d62019-09-02 15:02:24 +02002728
René van Dorst38f790a2019-09-02 15:02:26 +02002729 /* Are we connected to external phy */
2730 if (port == 5 && dsa_is_user_port(ds, 5))
2731 mcr_new |= PMCR_EXT_PHY;
2732
René van Dorstca366d62019-09-02 15:02:24 +02002733 if (mcr_new != mcr_cur)
2734 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
2735}
2736
Landen Chaoc2885752020-09-11 21:48:54 +08002737static void
2738mt753x_phylink_mac_an_restart(struct dsa_switch *ds, int port)
2739{
2740 struct mt7530_priv *priv = ds->priv;
2741
2742 if (!priv->info->mac_pcs_an_restart)
2743 return;
2744
2745 priv->info->mac_pcs_an_restart(ds, port);
2746}
2747
2748static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port,
René van Dorstca366d62019-09-02 15:02:24 +02002749 unsigned int mode,
2750 phy_interface_t interface)
2751{
2752 struct mt7530_priv *priv = ds->priv;
2753
René van Dorst1d011452020-03-27 15:44:12 +01002754 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
René van Dorstca366d62019-09-02 15:02:24 +02002755}
2756
Landen Chaoc2885752020-09-11 21:48:54 +08002757static void mt753x_mac_pcs_link_up(struct dsa_switch *ds, int port,
2758 unsigned int mode, phy_interface_t interface,
2759 int speed, int duplex)
2760{
2761 struct mt7530_priv *priv = ds->priv;
2762
2763 if (!priv->info->mac_pcs_link_up)
2764 return;
2765
2766 priv->info->mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2767}
2768
2769static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port,
René van Dorstca366d62019-09-02 15:02:24 +02002770 unsigned int mode,
2771 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00002772 struct phy_device *phydev,
2773 int speed, int duplex,
2774 bool tx_pause, bool rx_pause)
René van Dorstca366d62019-09-02 15:02:24 +02002775{
2776 struct mt7530_priv *priv = ds->priv;
René van Dorst1d011452020-03-27 15:44:12 +01002777 u32 mcr;
René van Dorstca366d62019-09-02 15:02:24 +02002778
Landen Chaoc2885752020-09-11 21:48:54 +08002779 mt753x_mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2780
René van Dorst1d011452020-03-27 15:44:12 +01002781 mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
2782
Landen Chaoc2885752020-09-11 21:48:54 +08002783 /* MT753x MAC works in 1G full duplex mode for all up-clocked
2784 * variants.
2785 */
2786 if (interface == PHY_INTERFACE_MODE_TRGMII ||
2787 (phy_interface_mode_is_8023z(interface))) {
2788 speed = SPEED_1000;
2789 duplex = DUPLEX_FULL;
2790 }
2791
René van Dorst1d011452020-03-27 15:44:12 +01002792 switch (speed) {
2793 case SPEED_1000:
2794 mcr |= PMCR_FORCE_SPEED_1000;
2795 break;
2796 case SPEED_100:
2797 mcr |= PMCR_FORCE_SPEED_100;
2798 break;
2799 }
2800 if (duplex == DUPLEX_FULL) {
2801 mcr |= PMCR_FORCE_FDX;
2802 if (tx_pause)
2803 mcr |= PMCR_TX_FC_EN;
2804 if (rx_pause)
2805 mcr |= PMCR_RX_FC_EN;
2806 }
2807
René van Dorst40b5d2f2021-04-12 08:50:31 +02002808 if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, 0) >= 0) {
2809 switch (speed) {
2810 case SPEED_1000:
2811 mcr |= PMCR_FORCE_EEE1G;
2812 break;
2813 case SPEED_100:
2814 mcr |= PMCR_FORCE_EEE100;
2815 break;
2816 }
2817 }
2818
René van Dorst1d011452020-03-27 15:44:12 +01002819 mt7530_set(priv, MT7530_PMCR_P(port), mcr);
René van Dorstca366d62019-09-02 15:02:24 +02002820}
2821
Landen Chaoc2885752020-09-11 21:48:54 +08002822static int
2823mt7531_cpu_port_config(struct dsa_switch *ds, int port)
2824{
2825 struct mt7530_priv *priv = ds->priv;
2826 phy_interface_t interface;
2827 int speed;
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002828 int ret;
Landen Chaoc2885752020-09-11 21:48:54 +08002829
2830 switch (port) {
2831 case 5:
2832 if (mt7531_is_rgmii_port(priv, port))
2833 interface = PHY_INTERFACE_MODE_RGMII;
2834 else
2835 interface = PHY_INTERFACE_MODE_2500BASEX;
2836
2837 priv->p5_interface = interface;
2838 break;
2839 case 6:
2840 interface = PHY_INTERFACE_MODE_2500BASEX;
2841
2842 mt7531_pad_setup(ds, interface);
2843
2844 priv->p6_interface = interface;
2845 break;
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002846 default:
2847 return -EINVAL;
Landen Chaoc2885752020-09-11 21:48:54 +08002848 }
2849
2850 if (interface == PHY_INTERFACE_MODE_2500BASEX)
2851 speed = SPEED_2500;
2852 else
2853 speed = SPEED_1000;
2854
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002855 ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
2856 if (ret)
2857 return ret;
Landen Chaoc2885752020-09-11 21:48:54 +08002858 mt7530_write(priv, MT7530_PMCR_P(port),
2859 PMCR_CPU_PORT_SETTING(priv->id));
2860 mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2861 speed, DUPLEX_FULL, true, true);
2862
2863 return 0;
2864}
2865
Landen Chao88bdef82020-09-11 21:48:52 +08002866static void
2867mt7530_mac_port_validate(struct dsa_switch *ds, int port,
2868 unsigned long *supported)
2869{
2870 if (port == 5)
2871 phylink_set(supported, 1000baseX_Full);
2872}
2873
Landen Chaoc2885752020-09-11 21:48:54 +08002874static void mt7531_mac_port_validate(struct dsa_switch *ds, int port,
2875 unsigned long *supported)
2876{
2877 struct mt7530_priv *priv = ds->priv;
2878
2879 mt7531_sgmii_validate(priv, port, supported);
2880}
2881
Landen Chao88bdef82020-09-11 21:48:52 +08002882static void
2883mt753x_phylink_validate(struct dsa_switch *ds, int port,
2884 unsigned long *supported,
2885 struct phylink_link_state *state)
René van Dorstca366d62019-09-02 15:02:24 +02002886{
2887 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Landen Chao88bdef82020-09-11 21:48:52 +08002888 struct mt7530_priv *priv = ds->priv;
René van Dorstca366d62019-09-02 15:02:24 +02002889
Landen Chao88bdef82020-09-11 21:48:52 +08002890 if (state->interface != PHY_INTERFACE_MODE_NA &&
2891 !mt753x_phy_mode_supported(ds, port, state)) {
René van Dorstca366d62019-09-02 15:02:24 +02002892 linkmode_zero(supported);
2893 return;
2894 }
2895
2896 phylink_set_port_modes(mask);
René van Dorstca366d62019-09-02 15:02:24 +02002897
Landen Chaoc2885752020-09-11 21:48:54 +08002898 if (state->interface != PHY_INTERFACE_MODE_TRGMII ||
2899 !phy_interface_mode_is_8023z(state->interface)) {
René van Dorstca366d62019-09-02 15:02:24 +02002900 phylink_set(mask, 10baseT_Half);
2901 phylink_set(mask, 10baseT_Full);
2902 phylink_set(mask, 100baseT_Half);
2903 phylink_set(mask, 100baseT_Full);
Landen Chao88bdef82020-09-11 21:48:52 +08002904 phylink_set(mask, Autoneg);
René van Dorst38f790a2019-09-02 15:02:26 +02002905 }
René van Dorstca366d62019-09-02 15:02:24 +02002906
Landen Chao88bdef82020-09-11 21:48:52 +08002907 /* This switch only supports 1G full-duplex. */
2908 if (state->interface != PHY_INTERFACE_MODE_MII)
2909 phylink_set(mask, 1000baseT_Full);
2910
2911 priv->info->mac_port_validate(ds, port, mask);
2912
René van Dorstca366d62019-09-02 15:02:24 +02002913 phylink_set(mask, Pause);
2914 phylink_set(mask, Asym_Pause);
2915
2916 linkmode_and(supported, supported, mask);
2917 linkmode_and(state->advertising, state->advertising, mask);
Landen Chaoc2885752020-09-11 21:48:54 +08002918
2919 /* We can only operate at 2500BaseX or 1000BaseX. If requested
2920 * to advertise both, only report advertising at 2500BaseX.
2921 */
2922 phylink_helper_basex_speed(state);
René van Dorstca366d62019-09-02 15:02:24 +02002923}
2924
2925static int
2926mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
2927 struct phylink_link_state *state)
2928{
2929 struct mt7530_priv *priv = ds->priv;
2930 u32 pmsr;
2931
2932 if (port < 0 || port >= MT7530_NUM_PORTS)
2933 return -EINVAL;
2934
2935 pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2936
2937 state->link = (pmsr & PMSR_LINK);
2938 state->an_complete = state->link;
2939 state->duplex = !!(pmsr & PMSR_DPX);
2940
2941 switch (pmsr & PMSR_SPEED_MASK) {
2942 case PMSR_SPEED_10:
2943 state->speed = SPEED_10;
2944 break;
2945 case PMSR_SPEED_100:
2946 state->speed = SPEED_100;
2947 break;
2948 case PMSR_SPEED_1000:
2949 state->speed = SPEED_1000;
2950 break;
2951 default:
2952 state->speed = SPEED_UNKNOWN;
2953 break;
2954 }
2955
2956 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2957 if (pmsr & PMSR_RX_FC)
2958 state->pause |= MLO_PAUSE_RX;
2959 if (pmsr & PMSR_TX_FC)
2960 state->pause |= MLO_PAUSE_TX;
2961
2962 return 1;
2963}
2964
Landen Chao88bdef82020-09-11 21:48:52 +08002965static int
Landen Chaoc2885752020-09-11 21:48:54 +08002966mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port,
2967 struct phylink_link_state *state)
2968{
2969 u32 status, val;
2970 u16 config_reg;
2971
2972 status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2973 state->link = !!(status & MT7531_SGMII_LINK_STATUS);
2974 if (state->interface == PHY_INTERFACE_MODE_SGMII &&
2975 (status & MT7531_SGMII_AN_ENABLE)) {
2976 val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port));
2977 config_reg = val >> 16;
2978
2979 switch (config_reg & LPA_SGMII_SPD_MASK) {
2980 case LPA_SGMII_1000:
2981 state->speed = SPEED_1000;
2982 break;
2983 case LPA_SGMII_100:
2984 state->speed = SPEED_100;
2985 break;
2986 case LPA_SGMII_10:
2987 state->speed = SPEED_10;
2988 break;
2989 default:
2990 dev_err(priv->dev, "invalid sgmii PHY speed\n");
2991 state->link = false;
2992 return -EINVAL;
2993 }
2994
2995 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2996 state->duplex = DUPLEX_FULL;
2997 else
2998 state->duplex = DUPLEX_HALF;
2999 }
3000
3001 return 0;
3002}
3003
3004static int
3005mt7531_phylink_mac_link_state(struct dsa_switch *ds, int port,
3006 struct phylink_link_state *state)
3007{
3008 struct mt7530_priv *priv = ds->priv;
3009
3010 if (state->interface == PHY_INTERFACE_MODE_SGMII)
3011 return mt7531_sgmii_pcs_get_state_an(priv, port, state);
3012
3013 return -EOPNOTSUPP;
3014}
3015
3016static int
Landen Chao88bdef82020-09-11 21:48:52 +08003017mt753x_phylink_mac_link_state(struct dsa_switch *ds, int port,
3018 struct phylink_link_state *state)
3019{
3020 struct mt7530_priv *priv = ds->priv;
3021
3022 return priv->info->mac_port_get_state(ds, port, state);
3023}
3024
3025static int
3026mt753x_setup(struct dsa_switch *ds)
3027{
3028 struct mt7530_priv *priv = ds->priv;
DENG Qingfangba751e22021-05-19 11:32:00 +08003029 int ret = priv->info->sw_setup(ds);
Landen Chao88bdef82020-09-11 21:48:52 +08003030
DENG Qingfangba751e22021-05-19 11:32:00 +08003031 if (ret)
3032 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003033
DENG Qingfangba751e22021-05-19 11:32:00 +08003034 ret = mt7530_setup_irq(priv);
3035 if (ret)
3036 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003037
DENG Qingfangba751e22021-05-19 11:32:00 +08003038 ret = mt7530_setup_mdio(priv);
3039 if (ret && priv->irq)
3040 mt7530_free_irq_common(priv);
Landen Chao88bdef82020-09-11 21:48:52 +08003041
DENG Qingfangba751e22021-05-19 11:32:00 +08003042 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003043}
3044
René van Dorst40b5d2f2021-04-12 08:50:31 +02003045static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
3046 struct ethtool_eee *e)
3047{
3048 struct mt7530_priv *priv = ds->priv;
3049 u32 eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port));
3050
3051 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
3052 e->tx_lpi_timer = GET_LPI_THRESH(eeecr);
3053
3054 return 0;
3055}
3056
3057static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
3058 struct ethtool_eee *e)
3059{
3060 struct mt7530_priv *priv = ds->priv;
3061 u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
3062
3063 if (e->tx_lpi_timer > 0xFFF)
3064 return -EINVAL;
3065
3066 set = SET_LPI_THRESH(e->tx_lpi_timer);
3067 if (!e->tx_lpi_enabled)
3068 /* Force LPI Mode without a delay */
3069 set |= LPI_MODE_EN;
3070 mt7530_rmw(priv, MT7530_PMEEECR_P(port), mask, set);
3071
3072 return 0;
3073}
3074
Bhumika Goyald78d6772017-08-09 10:34:15 +05303075static const struct dsa_switch_ops mt7530_switch_ops = {
Sean Wangb8f126a2017-04-07 16:45:09 +08003076 .get_tag_protocol = mtk_get_tag_protocol,
Landen Chao88bdef82020-09-11 21:48:52 +08003077 .setup = mt753x_setup,
Sean Wangb8f126a2017-04-07 16:45:09 +08003078 .get_strings = mt7530_get_strings,
Sean Wangb8f126a2017-04-07 16:45:09 +08003079 .get_ethtool_stats = mt7530_get_ethtool_stats,
3080 .get_sset_count = mt7530_get_sset_count,
DENG Qingfangea6d5c92020-12-08 15:00:28 +08003081 .set_ageing_time = mt7530_set_ageing_time,
Sean Wangb8f126a2017-04-07 16:45:09 +08003082 .port_enable = mt7530_port_enable,
3083 .port_disable = mt7530_port_disable,
DENG Qingfang94701742020-11-03 13:06:18 +08003084 .port_change_mtu = mt7530_port_change_mtu,
3085 .port_max_mtu = mt7530_port_max_mtu,
Sean Wangb8f126a2017-04-07 16:45:09 +08003086 .port_stp_state_set = mt7530_stp_state_set,
DENG Qingfang5a308332021-03-16 01:09:40 +08003087 .port_pre_bridge_flags = mt7530_port_pre_bridge_flags,
3088 .port_bridge_flags = mt7530_port_bridge_flags,
3089 .port_set_mrouter = mt7530_port_set_mrouter,
Sean Wangb8f126a2017-04-07 16:45:09 +08003090 .port_bridge_join = mt7530_port_bridge_join,
3091 .port_bridge_leave = mt7530_port_bridge_leave,
Sean Wangb8f126a2017-04-07 16:45:09 +08003092 .port_fdb_add = mt7530_port_fdb_add,
3093 .port_fdb_del = mt7530_port_fdb_del,
3094 .port_fdb_dump = mt7530_port_fdb_dump,
DENG Qingfang5a308332021-03-16 01:09:40 +08003095 .port_mdb_add = mt7530_port_mdb_add,
3096 .port_mdb_del = mt7530_port_mdb_del,
Sean Wang83163f72017-12-15 12:47:00 +08003097 .port_vlan_filtering = mt7530_port_vlan_filtering,
Sean Wang83163f72017-12-15 12:47:00 +08003098 .port_vlan_add = mt7530_port_vlan_add,
3099 .port_vlan_del = mt7530_port_vlan_del,
Landen Chaoc2885752020-09-11 21:48:54 +08003100 .port_mirror_add = mt753x_port_mirror_add,
3101 .port_mirror_del = mt753x_port_mirror_del,
Landen Chao88bdef82020-09-11 21:48:52 +08003102 .phylink_validate = mt753x_phylink_validate,
3103 .phylink_mac_link_state = mt753x_phylink_mac_link_state,
3104 .phylink_mac_config = mt753x_phylink_mac_config,
Landen Chaoc2885752020-09-11 21:48:54 +08003105 .phylink_mac_an_restart = mt753x_phylink_mac_an_restart,
3106 .phylink_mac_link_down = mt753x_phylink_mac_link_down,
3107 .phylink_mac_link_up = mt753x_phylink_mac_link_up,
René van Dorst40b5d2f2021-04-12 08:50:31 +02003108 .get_mac_eee = mt753x_get_mac_eee,
3109 .set_mac_eee = mt753x_set_mac_eee,
Sean Wangb8f126a2017-04-07 16:45:09 +08003110};
3111
Landen Chao88bdef82020-09-11 21:48:52 +08003112static const struct mt753x_info mt753x_table[] = {
3113 [ID_MT7621] = {
3114 .id = ID_MT7621,
3115 .sw_setup = mt7530_setup,
3116 .phy_read = mt7530_phy_read,
3117 .phy_write = mt7530_phy_write,
3118 .pad_setup = mt7530_pad_clk_setup,
3119 .phy_mode_supported = mt7530_phy_mode_supported,
3120 .mac_port_validate = mt7530_mac_port_validate,
3121 .mac_port_get_state = mt7530_phylink_mac_link_state,
3122 .mac_port_config = mt7530_mac_config,
3123 },
3124 [ID_MT7530] = {
3125 .id = ID_MT7530,
3126 .sw_setup = mt7530_setup,
3127 .phy_read = mt7530_phy_read,
3128 .phy_write = mt7530_phy_write,
3129 .pad_setup = mt7530_pad_clk_setup,
3130 .phy_mode_supported = mt7530_phy_mode_supported,
3131 .mac_port_validate = mt7530_mac_port_validate,
3132 .mac_port_get_state = mt7530_phylink_mac_link_state,
3133 .mac_port_config = mt7530_mac_config,
3134 },
Landen Chaoc2885752020-09-11 21:48:54 +08003135 [ID_MT7531] = {
3136 .id = ID_MT7531,
3137 .sw_setup = mt7531_setup,
3138 .phy_read = mt7531_ind_phy_read,
3139 .phy_write = mt7531_ind_phy_write,
3140 .pad_setup = mt7531_pad_setup,
3141 .cpu_port_config = mt7531_cpu_port_config,
3142 .phy_mode_supported = mt7531_phy_mode_supported,
3143 .mac_port_validate = mt7531_mac_port_validate,
3144 .mac_port_get_state = mt7531_phylink_mac_link_state,
3145 .mac_port_config = mt7531_mac_config,
3146 .mac_pcs_an_restart = mt7531_sgmii_restart_an,
3147 .mac_pcs_link_up = mt7531_sgmii_link_up_force,
3148 },
Landen Chao88bdef82020-09-11 21:48:52 +08003149};
3150
Greg Ungererddda1ac2019-01-30 11:24:05 +10003151static const struct of_device_id mt7530_of_match[] = {
Landen Chao88bdef82020-09-11 21:48:52 +08003152 { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
3153 { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
Landen Chaoc2885752020-09-11 21:48:54 +08003154 { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
Greg Ungererddda1ac2019-01-30 11:24:05 +10003155 { /* sentinel */ },
3156};
3157MODULE_DEVICE_TABLE(of, mt7530_of_match);
3158
Sean Wangb8f126a2017-04-07 16:45:09 +08003159static int
3160mt7530_probe(struct mdio_device *mdiodev)
3161{
3162 struct mt7530_priv *priv;
3163 struct device_node *dn;
3164
3165 dn = mdiodev->dev.of_node;
3166
3167 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
3168 if (!priv)
3169 return -ENOMEM;
3170
Vivien Didelot7e99e342019-10-21 16:51:30 -04003171 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
Sean Wangb8f126a2017-04-07 16:45:09 +08003172 if (!priv->ds)
3173 return -ENOMEM;
3174
Vivien Didelot7e99e342019-10-21 16:51:30 -04003175 priv->ds->dev = &mdiodev->dev;
3176 priv->ds->num_ports = DSA_MAX_PORTS;
3177
Sean Wangb8f126a2017-04-07 16:45:09 +08003178 /* Use medatek,mcm property to distinguish hardware type that would
3179 * casues a little bit differences on power-on sequence.
3180 */
3181 priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
3182 if (priv->mcm) {
3183 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
3184
3185 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
3186 if (IS_ERR(priv->rstc)) {
3187 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3188 return PTR_ERR(priv->rstc);
3189 }
3190 }
3191
Greg Ungererddda1ac2019-01-30 11:24:05 +10003192 /* Get the hardware identifier from the devicetree node.
3193 * We will need it for some of the clock and regulator setup.
3194 */
Landen Chao88bdef82020-09-11 21:48:52 +08003195 priv->info = of_device_get_match_data(&mdiodev->dev);
3196 if (!priv->info)
3197 return -EINVAL;
3198
3199 /* Sanity check if these required device operations are filled
3200 * properly.
3201 */
3202 if (!priv->info->sw_setup || !priv->info->pad_setup ||
3203 !priv->info->phy_read || !priv->info->phy_write ||
3204 !priv->info->phy_mode_supported ||
3205 !priv->info->mac_port_validate ||
3206 !priv->info->mac_port_get_state || !priv->info->mac_port_config)
3207 return -EINVAL;
3208
3209 priv->id = priv->info->id;
Sean Wangb8f126a2017-04-07 16:45:09 +08003210
Greg Ungererddda1ac2019-01-30 11:24:05 +10003211 if (priv->id == ID_MT7530) {
3212 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
3213 if (IS_ERR(priv->core_pwr))
3214 return PTR_ERR(priv->core_pwr);
3215
3216 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
3217 if (IS_ERR(priv->io_pwr))
3218 return PTR_ERR(priv->io_pwr);
3219 }
Sean Wangb8f126a2017-04-07 16:45:09 +08003220
3221 /* Not MCM that indicates switch works as the remote standalone
3222 * integrated circuit so the GPIO pin would be used to complete
3223 * the reset, otherwise memory-mapped register accessing used
3224 * through syscon provides in the case of MCM.
3225 */
3226 if (!priv->mcm) {
3227 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
3228 GPIOD_OUT_LOW);
3229 if (IS_ERR(priv->reset)) {
3230 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3231 return PTR_ERR(priv->reset);
3232 }
3233 }
3234
3235 priv->bus = mdiodev->bus;
3236 priv->dev = &mdiodev->dev;
3237 priv->ds->priv = priv;
3238 priv->ds->ops = &mt7530_switch_ops;
3239 mutex_init(&priv->reg_mutex);
3240 dev_set_drvdata(&mdiodev->dev, priv);
3241
Vivien Didelot23c9ee42017-05-26 18:12:51 -04003242 return dsa_register_switch(priv->ds);
Sean Wangb8f126a2017-04-07 16:45:09 +08003243}
3244
3245static void
3246mt7530_remove(struct mdio_device *mdiodev)
3247{
3248 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
3249 int ret = 0;
3250
3251 ret = regulator_disable(priv->core_pwr);
3252 if (ret < 0)
3253 dev_err(priv->dev,
3254 "Failed to disable core power: %d\n", ret);
3255
3256 ret = regulator_disable(priv->io_pwr);
3257 if (ret < 0)
3258 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
3259 ret);
3260
DENG Qingfangba751e22021-05-19 11:32:00 +08003261 if (priv->irq)
3262 mt7530_free_irq(priv);
3263
Sean Wangb8f126a2017-04-07 16:45:09 +08003264 dsa_unregister_switch(priv->ds);
3265 mutex_destroy(&priv->reg_mutex);
3266}
3267
Sean Wangb8f126a2017-04-07 16:45:09 +08003268static struct mdio_driver mt7530_mdio_driver = {
3269 .probe = mt7530_probe,
3270 .remove = mt7530_remove,
3271 .mdiodrv.driver = {
3272 .name = "mt7530",
3273 .of_match_table = mt7530_of_match,
3274 },
3275};
3276
3277mdio_module_driver(mt7530_mdio_driver);
3278
3279MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
3280MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
3281MODULE_LICENSE("GPL");