blob: 606a9f4db5793409edb0877e756da1974d26caa8 [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
1150 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
1151}
1152
1153static int
DENG Qingfang5a308332021-03-16 01:09:40 +08001154mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1155 struct switchdev_brport_flags flags,
1156 struct netlink_ext_ack *extack)
1157{
1158 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1159 BR_BCAST_FLOOD))
1160 return -EINVAL;
1161
1162 return 0;
1163}
1164
1165static int
1166mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
1167 struct switchdev_brport_flags flags,
1168 struct netlink_ext_ack *extack)
1169{
1170 struct mt7530_priv *priv = ds->priv;
1171
1172 if (flags.mask & BR_LEARNING)
1173 mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
1174 flags.val & BR_LEARNING ? 0 : SA_DIS);
1175
1176 if (flags.mask & BR_FLOOD)
1177 mt7530_rmw(priv, MT7530_MFC, UNU_FFP(BIT(port)),
1178 flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
1179
1180 if (flags.mask & BR_MCAST_FLOOD)
1181 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1182 flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
1183
1184 if (flags.mask & BR_BCAST_FLOOD)
1185 mt7530_rmw(priv, MT7530_MFC, BC_FFP(BIT(port)),
1186 flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
1187
1188 return 0;
1189}
1190
1191static int
1192mt7530_port_set_mrouter(struct dsa_switch *ds, int port, bool mrouter,
1193 struct netlink_ext_ack *extack)
1194{
1195 struct mt7530_priv *priv = ds->priv;
1196
1197 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)),
1198 mrouter ? UNM_FFP(BIT(port)) : 0);
1199
1200 return 0;
1201}
1202
1203static int
Sean Wangb8f126a2017-04-07 16:45:09 +08001204mt7530_port_bridge_join(struct dsa_switch *ds, int port,
1205 struct net_device *bridge)
1206{
1207 struct mt7530_priv *priv = ds->priv;
1208 u32 port_bitmap = BIT(MT7530_CPU_PORT);
1209 int i;
1210
1211 mutex_lock(&priv->reg_mutex);
1212
1213 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1214 /* Add this port to the port matrix of the other ports in the
1215 * same bridge. If the port is disabled, port matrix is kept
1216 * and not being setup until the port becomes enabled.
1217 */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -04001218 if (dsa_is_user_port(ds, i) && i != port) {
Vivien Didelotc8652c82017-10-16 11:12:19 -04001219 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +08001220 continue;
1221 if (priv->ports[i].enable)
1222 mt7530_set(priv, MT7530_PCR_P(i),
1223 PCR_MATRIX(BIT(port)));
1224 priv->ports[i].pm |= PCR_MATRIX(BIT(port));
1225
1226 port_bitmap |= BIT(i);
1227 }
1228 }
1229
1230 /* Add the all other ports to this port matrix. */
1231 if (priv->ports[port].enable)
1232 mt7530_rmw(priv, MT7530_PCR_P(port),
1233 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
1234 priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
1235
DENG Qingfang60871752021-08-04 00:04:02 +08001236 /* Set to fallback mode for independent VLAN learning */
1237 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1238 MT7530_PORT_FALLBACK_MODE);
1239
Sean Wangb8f126a2017-04-07 16:45:09 +08001240 mutex_unlock(&priv->reg_mutex);
1241
1242 return 0;
1243}
1244
1245static void
Sean Wang83163f72017-12-15 12:47:00 +08001246mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
1247{
1248 struct mt7530_priv *priv = ds->priv;
1249 bool all_user_ports_removed = true;
1250 int i;
1251
DENG Qingfang60871752021-08-04 00:04:02 +08001252 /* This is called after .port_bridge_leave when leaving a VLAN-aware
1253 * bridge. Don't set standalone ports to fallback mode.
Sean Wang83163f72017-12-15 12:47:00 +08001254 */
DENG Qingfang60871752021-08-04 00:04:02 +08001255 if (dsa_to_port(ds, port)->bridge_dev)
1256 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1257 MT7530_PORT_FALLBACK_MODE);
1258
DENG Qingfange0451242020-04-14 14:34:08 +08001259 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1260 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
1261 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +08001262
DENG Qingfang60871752021-08-04 00:04:02 +08001263 /* Set PVID to 0 */
1264 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1265 G0_PORT_VID_DEF);
1266
Sean Wang83163f72017-12-15 12:47:00 +08001267 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1268 if (dsa_is_user_port(ds, i) &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001269 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Sean Wang83163f72017-12-15 12:47:00 +08001270 all_user_ports_removed = false;
1271 break;
1272 }
1273 }
1274
1275 /* CPU port also does the same thing until all user ports belonging to
1276 * the CPU port get out of VLAN filtering mode.
1277 */
1278 if (all_user_ports_removed) {
1279 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
1280 PCR_MATRIX(dsa_user_ports(priv->ds)));
DENG Qingfange0451242020-04-14 14:34:08 +08001281 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
1282 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wang83163f72017-12-15 12:47:00 +08001283 }
1284}
1285
1286static void
1287mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
1288{
1289 struct mt7530_priv *priv = ds->priv;
1290
Sean Wang83163f72017-12-15 12:47:00 +08001291 /* Trapped into security mode allows packet forwarding through VLAN
DENG Qingfang60871752021-08-04 00:04:02 +08001292 * table lookup.
Sean Wang83163f72017-12-15 12:47:00 +08001293 */
DENG Qingfang60871752021-08-04 00:04:02 +08001294 if (dsa_is_user_port(ds, port)) {
DENG Qingfang38152ea2020-05-13 23:37:17 +08001295 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1296 MT7530_PORT_SECURITY_MODE);
DENG Qingfang60871752021-08-04 00:04:02 +08001297 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1298 G0_PORT_VID(priv->ports[port].pvid));
1299 }
Sean Wang83163f72017-12-15 12:47:00 +08001300
1301 /* Set the port as a user port which is to be able to recognize VID
1302 * from incoming packets before fetching entry within the VLAN table.
1303 */
DENG Qingfange0451242020-04-14 14:34:08 +08001304 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
1305 VLAN_ATTR(MT7530_VLAN_USER) |
1306 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
Sean Wang83163f72017-12-15 12:47:00 +08001307}
1308
1309static void
Sean Wangb8f126a2017-04-07 16:45:09 +08001310mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
1311 struct net_device *bridge)
1312{
1313 struct mt7530_priv *priv = ds->priv;
1314 int i;
1315
1316 mutex_lock(&priv->reg_mutex);
1317
1318 for (i = 0; i < MT7530_NUM_PORTS; i++) {
1319 /* Remove this port from the port matrix of the other ports
1320 * in the same bridge. If the port is disabled, port matrix
1321 * is kept and not being setup until the port becomes enabled.
Sean Wang83163f72017-12-15 12:47:00 +08001322 * And the other port's port matrix cannot be broken when the
1323 * other port is still a VLAN-aware port.
Sean Wangb8f126a2017-04-07 16:45:09 +08001324 */
Vladimir Oltean2a130552019-04-28 21:45:50 +03001325 if (dsa_is_user_port(ds, i) && i != port &&
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001326 !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
Vivien Didelotc8652c82017-10-16 11:12:19 -04001327 if (dsa_to_port(ds, i)->bridge_dev != bridge)
Sean Wangb8f126a2017-04-07 16:45:09 +08001328 continue;
1329 if (priv->ports[i].enable)
1330 mt7530_clear(priv, MT7530_PCR_P(i),
1331 PCR_MATRIX(BIT(port)));
1332 priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
1333 }
1334 }
1335
1336 /* Set the cpu port to be the only one in the port matrix of
1337 * this port.
1338 */
1339 if (priv->ports[port].enable)
1340 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
1341 PCR_MATRIX(BIT(MT7530_CPU_PORT)));
1342 priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
1343
DENG Qingfang60871752021-08-04 00:04:02 +08001344 /* When a port is removed from the bridge, the port would be set up
1345 * back to the default as is at initial boot which is a VLAN-unaware
1346 * port.
1347 */
1348 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
1349 MT7530_PORT_MATRIX_MODE);
1350
Sean Wangb8f126a2017-04-07 16:45:09 +08001351 mutex_unlock(&priv->reg_mutex);
1352}
1353
1354static int
Sean Wangb8f126a2017-04-07 16:45:09 +08001355mt7530_port_fdb_add(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001356 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +08001357{
1358 struct mt7530_priv *priv = ds->priv;
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +03001359 int ret;
Sean Wangb8f126a2017-04-07 16:45:09 +08001360 u8 port_mask = BIT(port);
1361
1362 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001363 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
Florian Fainelli18bd5942018-04-02 16:24:14 -07001364 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001365 mutex_unlock(&priv->reg_mutex);
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +03001366
1367 return ret;
Sean Wangb8f126a2017-04-07 16:45:09 +08001368}
1369
1370static int
1371mt7530_port_fdb_del(struct dsa_switch *ds, int port,
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001372 const unsigned char *addr, u16 vid)
Sean Wangb8f126a2017-04-07 16:45:09 +08001373{
1374 struct mt7530_priv *priv = ds->priv;
1375 int ret;
1376 u8 port_mask = BIT(port);
1377
1378 mutex_lock(&priv->reg_mutex);
Arkadi Sharshevsky6c2c1dc2017-08-06 16:15:39 +03001379 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
Florian Fainelli18bd5942018-04-02 16:24:14 -07001380 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08001381 mutex_unlock(&priv->reg_mutex);
1382
1383 return ret;
1384}
1385
1386static int
1387mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001388 dsa_fdb_dump_cb_t *cb, void *data)
Sean Wangb8f126a2017-04-07 16:45:09 +08001389{
1390 struct mt7530_priv *priv = ds->priv;
1391 struct mt7530_fdb _fdb = { 0 };
1392 int cnt = MT7530_NUM_FDB_RECORDS;
1393 int ret = 0;
1394 u32 rsp = 0;
1395
1396 mutex_lock(&priv->reg_mutex);
1397
1398 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1399 if (ret < 0)
1400 goto err;
1401
1402 do {
1403 if (rsp & ATC_SRCH_HIT) {
1404 mt7530_fdb_read(priv, &_fdb);
1405 if (_fdb.port_mask & BIT(port)) {
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001406 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
1407 data);
Sean Wangb8f126a2017-04-07 16:45:09 +08001408 if (ret < 0)
1409 break;
1410 }
1411 }
1412 } while (--cnt &&
1413 !(rsp & ATC_SRCH_END) &&
1414 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1415err:
1416 mutex_unlock(&priv->reg_mutex);
1417
1418 return 0;
1419}
1420
Sean Wang83163f72017-12-15 12:47:00 +08001421static int
DENG Qingfang5a308332021-03-16 01:09:40 +08001422mt7530_port_mdb_add(struct dsa_switch *ds, int port,
1423 const struct switchdev_obj_port_mdb *mdb)
1424{
1425 struct mt7530_priv *priv = ds->priv;
1426 const u8 *addr = mdb->addr;
1427 u16 vid = mdb->vid;
1428 u8 port_mask = 0;
1429 int ret;
1430
1431 mutex_lock(&priv->reg_mutex);
1432
1433 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1434 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1435 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1436 & PORT_MAP_MASK;
1437
1438 port_mask |= BIT(port);
1439 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
1440 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1441
1442 mutex_unlock(&priv->reg_mutex);
1443
1444 return ret;
1445}
1446
1447static int
1448mt7530_port_mdb_del(struct dsa_switch *ds, int port,
1449 const struct switchdev_obj_port_mdb *mdb)
1450{
1451 struct mt7530_priv *priv = ds->priv;
1452 const u8 *addr = mdb->addr;
1453 u16 vid = mdb->vid;
1454 u8 port_mask = 0;
1455 int ret;
1456
1457 mutex_lock(&priv->reg_mutex);
1458
1459 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
1460 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
1461 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
1462 & PORT_MAP_MASK;
1463
1464 port_mask &= ~BIT(port);
1465 mt7530_fdb_write(priv, vid, port_mask, addr, -1,
1466 port_mask ? STATIC_ENT : STATIC_EMP);
1467 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
1468
1469 mutex_unlock(&priv->reg_mutex);
1470
1471 return ret;
1472}
1473
1474static int
Sean Wang83163f72017-12-15 12:47:00 +08001475mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
1476{
1477 struct mt7530_dummy_poll p;
1478 u32 val;
1479 int ret;
1480
1481 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
1482 mt7530_write(priv, MT7530_VTCR, val);
1483
1484 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
1485 ret = readx_poll_timeout(_mt7530_read, &p, val,
1486 !(val & VTCR_BUSY), 20, 20000);
1487 if (ret < 0) {
1488 dev_err(priv->dev, "poll timeout\n");
1489 return ret;
1490 }
1491
1492 val = mt7530_read(priv, MT7530_VTCR);
1493 if (val & VTCR_INVALID) {
1494 dev_err(priv->dev, "read VTCR invalid\n");
1495 return -EINVAL;
1496 }
1497
1498 return 0;
1499}
1500
1501static int
Vladimir Oltean89153ed2021-02-13 22:43:19 +02001502mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1503 struct netlink_ext_ack *extack)
Sean Wang83163f72017-12-15 12:47:00 +08001504{
Sean Wang83163f72017-12-15 12:47:00 +08001505 if (vlan_filtering) {
1506 /* The port is being kept as VLAN-unaware port when bridge is
1507 * set up with vlan_filtering not being set, Otherwise, the
1508 * port and the corresponding CPU port is required the setup
1509 * for becoming a VLAN-aware port.
1510 */
1511 mt7530_port_set_vlan_aware(ds, port);
1512 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
Vladimir Olteane3ee07d2019-04-28 21:45:47 +03001513 } else {
1514 mt7530_port_set_vlan_unaware(ds, port);
Sean Wang83163f72017-12-15 12:47:00 +08001515 }
1516
1517 return 0;
1518}
1519
Sean Wang83163f72017-12-15 12:47:00 +08001520static void
1521mt7530_hw_vlan_add(struct mt7530_priv *priv,
1522 struct mt7530_hw_vlan_entry *entry)
1523{
1524 u8 new_members;
1525 u32 val;
1526
1527 new_members = entry->old_members | BIT(entry->port) |
1528 BIT(MT7530_CPU_PORT);
1529
1530 /* Validate the entry with independent learning, create egress tag per
1531 * VLAN and joining the port as one of the port members.
1532 */
DENG Qingfang60871752021-08-04 00:04:02 +08001533 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | FID(FID_BRIDGED) |
1534 VLAN_VALID;
Sean Wang83163f72017-12-15 12:47:00 +08001535 mt7530_write(priv, MT7530_VAWD1, val);
1536
1537 /* Decide whether adding tag or not for those outgoing packets from the
1538 * port inside the VLAN.
1539 */
1540 val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
1541 MT7530_VLAN_EGRESS_TAG;
1542 mt7530_rmw(priv, MT7530_VAWD2,
1543 ETAG_CTRL_P_MASK(entry->port),
1544 ETAG_CTRL_P(entry->port, val));
1545
1546 /* CPU port is always taken as a tagged port for serving more than one
1547 * VLANs across and also being applied with egress type stack mode for
1548 * that VLAN tags would be appended after hardware special tag used as
1549 * DSA tag.
1550 */
1551 mt7530_rmw(priv, MT7530_VAWD2,
1552 ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
1553 ETAG_CTRL_P(MT7530_CPU_PORT,
1554 MT7530_VLAN_EGRESS_STACK));
1555}
1556
1557static void
1558mt7530_hw_vlan_del(struct mt7530_priv *priv,
1559 struct mt7530_hw_vlan_entry *entry)
1560{
1561 u8 new_members;
1562 u32 val;
1563
1564 new_members = entry->old_members & ~BIT(entry->port);
1565
1566 val = mt7530_read(priv, MT7530_VAWD1);
1567 if (!(val & VLAN_VALID)) {
1568 dev_err(priv->dev,
1569 "Cannot be deleted due to invalid entry\n");
1570 return;
1571 }
1572
1573 /* If certain member apart from CPU port is still alive in the VLAN,
1574 * the entry would be kept valid. Otherwise, the entry is got to be
1575 * disabled.
1576 */
1577 if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
1578 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
1579 VLAN_VALID;
1580 mt7530_write(priv, MT7530_VAWD1, val);
1581 } else {
1582 mt7530_write(priv, MT7530_VAWD1, 0);
1583 mt7530_write(priv, MT7530_VAWD2, 0);
1584 }
1585}
1586
1587static void
1588mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
1589 struct mt7530_hw_vlan_entry *entry,
1590 mt7530_vlan_op vlan_op)
1591{
1592 u32 val;
1593
1594 /* Fetch entry */
1595 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
1596
1597 val = mt7530_read(priv, MT7530_VAWD1);
1598
1599 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
1600
1601 /* Manipulate entry */
1602 vlan_op(priv, entry);
1603
1604 /* Flush result to hardware */
1605 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
1606}
1607
Vladimir Oltean1958d582021-01-09 02:01:53 +02001608static int
Sean Wang83163f72017-12-15 12:47:00 +08001609mt7530_port_vlan_add(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001610 const struct switchdev_obj_port_vlan *vlan,
1611 struct netlink_ext_ack *extack)
Sean Wang83163f72017-12-15 12:47:00 +08001612{
1613 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1614 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1615 struct mt7530_hw_vlan_entry new_entry;
1616 struct mt7530_priv *priv = ds->priv;
Sean Wang83163f72017-12-15 12:47:00 +08001617
Sean Wang83163f72017-12-15 12:47:00 +08001618 mutex_lock(&priv->reg_mutex);
1619
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001620 mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
1621 mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
Sean Wang83163f72017-12-15 12:47:00 +08001622
1623 if (pvid) {
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001624 priv->ports[port].pvid = vlan->vid;
DENG Qingfang60871752021-08-04 00:04:02 +08001625
1626 /* Only configure PVID if VLAN filtering is enabled */
1627 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1628 mt7530_rmw(priv, MT7530_PPBV1_P(port),
1629 G0_PORT_VID_MASK,
1630 G0_PORT_VID(vlan->vid));
Sean Wang83163f72017-12-15 12:47:00 +08001631 }
1632
1633 mutex_unlock(&priv->reg_mutex);
Vladimir Oltean1958d582021-01-09 02:01:53 +02001634
1635 return 0;
Sean Wang83163f72017-12-15 12:47:00 +08001636}
1637
1638static int
1639mt7530_port_vlan_del(struct dsa_switch *ds, int port,
1640 const struct switchdev_obj_port_vlan *vlan)
1641{
1642 struct mt7530_hw_vlan_entry target_entry;
1643 struct mt7530_priv *priv = ds->priv;
Sean Wang83163f72017-12-15 12:47:00 +08001644
Sean Wang83163f72017-12-15 12:47:00 +08001645 mutex_lock(&priv->reg_mutex);
1646
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001647 mt7530_hw_vlan_entry_init(&target_entry, port, 0);
1648 mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
1649 mt7530_hw_vlan_del);
Sean Wang83163f72017-12-15 12:47:00 +08001650
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001651 /* PVID is being restored to the default whenever the PVID port
1652 * is being removed from the VLAN.
1653 */
DENG Qingfang60871752021-08-04 00:04:02 +08001654 if (priv->ports[port].pvid == vlan->vid) {
1655 priv->ports[port].pvid = G0_PORT_VID_DEF;
1656 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
1657 G0_PORT_VID_DEF);
1658 }
Sean Wang83163f72017-12-15 12:47:00 +08001659
Sean Wang83163f72017-12-15 12:47:00 +08001660
1661 mutex_unlock(&priv->reg_mutex);
1662
1663 return 0;
1664}
1665
Landen Chaoc2885752020-09-11 21:48:54 +08001666static int mt753x_mirror_port_get(unsigned int id, u32 val)
1667{
1668 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) :
1669 MIRROR_PORT(val);
1670}
1671
1672static int mt753x_mirror_port_set(unsigned int id, u32 val)
1673{
1674 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) :
1675 MIRROR_PORT(val);
1676}
1677
1678static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
DENG Qingfang37feab62020-03-06 20:35:35 +08001679 struct dsa_mall_mirror_tc_entry *mirror,
1680 bool ingress)
1681{
1682 struct mt7530_priv *priv = ds->priv;
Landen Chaoc2885752020-09-11 21:48:54 +08001683 int monitor_port;
DENG Qingfang37feab62020-03-06 20:35:35 +08001684 u32 val;
1685
1686 /* Check for existent entry */
1687 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
1688 return -EEXIST;
1689
Landen Chaoc2885752020-09-11 21:48:54 +08001690 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
DENG Qingfang37feab62020-03-06 20:35:35 +08001691
1692 /* MT7530 only supports one monitor port */
Landen Chaoc2885752020-09-11 21:48:54 +08001693 monitor_port = mt753x_mirror_port_get(priv->id, val);
1694 if (val & MT753X_MIRROR_EN(priv->id) &&
1695 monitor_port != mirror->to_local_port)
DENG Qingfang37feab62020-03-06 20:35:35 +08001696 return -EEXIST;
1697
Landen Chaoc2885752020-09-11 21:48:54 +08001698 val |= MT753X_MIRROR_EN(priv->id);
1699 val &= ~MT753X_MIRROR_MASK(priv->id);
1700 val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port);
1701 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
DENG Qingfang37feab62020-03-06 20:35:35 +08001702
1703 val = mt7530_read(priv, MT7530_PCR_P(port));
1704 if (ingress) {
1705 val |= PORT_RX_MIR;
1706 priv->mirror_rx |= BIT(port);
1707 } else {
1708 val |= PORT_TX_MIR;
1709 priv->mirror_tx |= BIT(port);
1710 }
1711 mt7530_write(priv, MT7530_PCR_P(port), val);
1712
1713 return 0;
1714}
1715
Landen Chaoc2885752020-09-11 21:48:54 +08001716static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
DENG Qingfang37feab62020-03-06 20:35:35 +08001717 struct dsa_mall_mirror_tc_entry *mirror)
1718{
1719 struct mt7530_priv *priv = ds->priv;
1720 u32 val;
1721
1722 val = mt7530_read(priv, MT7530_PCR_P(port));
1723 if (mirror->ingress) {
1724 val &= ~PORT_RX_MIR;
1725 priv->mirror_rx &= ~BIT(port);
1726 } else {
1727 val &= ~PORT_TX_MIR;
1728 priv->mirror_tx &= ~BIT(port);
1729 }
1730 mt7530_write(priv, MT7530_PCR_P(port), val);
1731
1732 if (!priv->mirror_rx && !priv->mirror_tx) {
Landen Chaoc2885752020-09-11 21:48:54 +08001733 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
1734 val &= ~MT753X_MIRROR_EN(priv->id);
1735 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
DENG Qingfang37feab62020-03-06 20:35:35 +08001736 }
1737}
1738
Sean Wangb8f126a2017-04-07 16:45:09 +08001739static enum dsa_tag_protocol
Florian Fainelli4d776482020-01-07 21:06:05 -08001740mtk_get_tag_protocol(struct dsa_switch *ds, int port,
1741 enum dsa_tag_protocol mp)
Sean Wangb8f126a2017-04-07 16:45:09 +08001742{
Vladimir Oltean244f8a82021-07-31 01:57:14 +03001743 return DSA_TAG_PROTO_MTK;
Sean Wangb8f126a2017-04-07 16:45:09 +08001744}
1745
DENG Qingfang63c75c02021-02-26 14:32:26 +08001746#ifdef CONFIG_GPIOLIB
DENG Qingfang429a0ed2021-01-25 12:43:22 +08001747static inline u32
1748mt7530_gpio_to_bit(unsigned int offset)
1749{
1750 /* Map GPIO offset to register bit
1751 * [ 2: 0] port 0 LED 0..2 as GPIO 0..2
1752 * [ 6: 4] port 1 LED 0..2 as GPIO 3..5
1753 * [10: 8] port 2 LED 0..2 as GPIO 6..8
1754 * [14:12] port 3 LED 0..2 as GPIO 9..11
1755 * [18:16] port 4 LED 0..2 as GPIO 12..14
1756 */
1757 return BIT(offset + offset / 3);
1758}
1759
1760static int
1761mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
1762{
1763 struct mt7530_priv *priv = gpiochip_get_data(gc);
1764 u32 bit = mt7530_gpio_to_bit(offset);
1765
1766 return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
1767}
1768
1769static void
1770mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
1771{
1772 struct mt7530_priv *priv = gpiochip_get_data(gc);
1773 u32 bit = mt7530_gpio_to_bit(offset);
1774
1775 if (value)
1776 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1777 else
1778 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1779}
1780
1781static int
1782mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
1783{
1784 struct mt7530_priv *priv = gpiochip_get_data(gc);
1785 u32 bit = mt7530_gpio_to_bit(offset);
1786
1787 return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
1788 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1789}
1790
1791static int
1792mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
1793{
1794 struct mt7530_priv *priv = gpiochip_get_data(gc);
1795 u32 bit = mt7530_gpio_to_bit(offset);
1796
1797 mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
1798 mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
1799
1800 return 0;
1801}
1802
1803static int
1804mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
1805{
1806 struct mt7530_priv *priv = gpiochip_get_data(gc);
1807 u32 bit = mt7530_gpio_to_bit(offset);
1808
1809 mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
1810
1811 if (value)
1812 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
1813 else
1814 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
1815
1816 mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
1817
1818 return 0;
1819}
1820
1821static int
1822mt7530_setup_gpio(struct mt7530_priv *priv)
1823{
1824 struct device *dev = priv->dev;
1825 struct gpio_chip *gc;
1826
1827 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
1828 if (!gc)
1829 return -ENOMEM;
1830
1831 mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
1832 mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
1833 mt7530_write(priv, MT7530_LED_IO_MODE, 0);
1834
1835 gc->label = "mt7530";
1836 gc->parent = dev;
1837 gc->owner = THIS_MODULE;
1838 gc->get_direction = mt7530_gpio_get_direction;
1839 gc->direction_input = mt7530_gpio_direction_input;
1840 gc->direction_output = mt7530_gpio_direction_output;
1841 gc->get = mt7530_gpio_get;
1842 gc->set = mt7530_gpio_set;
1843 gc->base = -1;
1844 gc->ngpio = 15;
1845 gc->can_sleep = true;
1846
1847 return devm_gpiochip_add_data(dev, gc, priv);
1848}
DENG Qingfang63c75c02021-02-26 14:32:26 +08001849#endif /* CONFIG_GPIOLIB */
DENG Qingfang429a0ed2021-01-25 12:43:22 +08001850
DENG Qingfangba751e22021-05-19 11:32:00 +08001851static irqreturn_t
1852mt7530_irq_thread_fn(int irq, void *dev_id)
1853{
1854 struct mt7530_priv *priv = dev_id;
1855 bool handled = false;
1856 u32 val;
1857 int p;
1858
1859 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1860 val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
1861 mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
1862 mutex_unlock(&priv->bus->mdio_lock);
1863
1864 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1865 if (BIT(p) & val) {
1866 unsigned int irq;
1867
1868 irq = irq_find_mapping(priv->irq_domain, p);
1869 handle_nested_irq(irq);
1870 handled = true;
1871 }
1872 }
1873
1874 return IRQ_RETVAL(handled);
1875}
1876
1877static void
1878mt7530_irq_mask(struct irq_data *d)
1879{
1880 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1881
1882 priv->irq_enable &= ~BIT(d->hwirq);
1883}
1884
1885static void
1886mt7530_irq_unmask(struct irq_data *d)
1887{
1888 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1889
1890 priv->irq_enable |= BIT(d->hwirq);
1891}
1892
1893static void
1894mt7530_irq_bus_lock(struct irq_data *d)
1895{
1896 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1897
1898 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
1899}
1900
1901static void
1902mt7530_irq_bus_sync_unlock(struct irq_data *d)
1903{
1904 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
1905
1906 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
1907 mutex_unlock(&priv->bus->mdio_lock);
1908}
1909
1910static struct irq_chip mt7530_irq_chip = {
1911 .name = KBUILD_MODNAME,
1912 .irq_mask = mt7530_irq_mask,
1913 .irq_unmask = mt7530_irq_unmask,
1914 .irq_bus_lock = mt7530_irq_bus_lock,
1915 .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
1916};
1917
1918static int
1919mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
1920 irq_hw_number_t hwirq)
1921{
1922 irq_set_chip_data(irq, domain->host_data);
1923 irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
1924 irq_set_nested_thread(irq, true);
1925 irq_set_noprobe(irq);
1926
1927 return 0;
1928}
1929
1930static const struct irq_domain_ops mt7530_irq_domain_ops = {
1931 .map = mt7530_irq_map,
1932 .xlate = irq_domain_xlate_onecell,
1933};
1934
1935static void
1936mt7530_setup_mdio_irq(struct mt7530_priv *priv)
1937{
1938 struct dsa_switch *ds = priv->ds;
1939 int p;
1940
1941 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1942 if (BIT(p) & ds->phys_mii_mask) {
1943 unsigned int irq;
1944
1945 irq = irq_create_mapping(priv->irq_domain, p);
1946 ds->slave_mii_bus->irq[p] = irq;
1947 }
1948 }
1949}
1950
1951static int
1952mt7530_setup_irq(struct mt7530_priv *priv)
1953{
1954 struct device *dev = priv->dev;
1955 struct device_node *np = dev->of_node;
1956 int ret;
1957
1958 if (!of_property_read_bool(np, "interrupt-controller")) {
1959 dev_info(dev, "no interrupt support\n");
1960 return 0;
1961 }
1962
1963 priv->irq = of_irq_get(np, 0);
1964 if (priv->irq <= 0) {
1965 dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
1966 return priv->irq ? : -EINVAL;
1967 }
1968
1969 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
1970 &mt7530_irq_domain_ops, priv);
1971 if (!priv->irq_domain) {
1972 dev_err(dev, "failed to create IRQ domain\n");
1973 return -ENOMEM;
1974 }
1975
1976 /* This register must be set for MT7530 to properly fire interrupts */
1977 if (priv->id != ID_MT7531)
1978 mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
1979
1980 ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
1981 IRQF_ONESHOT, KBUILD_MODNAME, priv);
1982 if (ret) {
1983 irq_domain_remove(priv->irq_domain);
1984 dev_err(dev, "failed to request IRQ: %d\n", ret);
1985 return ret;
1986 }
1987
1988 return 0;
1989}
1990
1991static void
1992mt7530_free_mdio_irq(struct mt7530_priv *priv)
1993{
1994 int p;
1995
1996 for (p = 0; p < MT7530_NUM_PHYS; p++) {
1997 if (BIT(p) & priv->ds->phys_mii_mask) {
1998 unsigned int irq;
1999
2000 irq = irq_find_mapping(priv->irq_domain, p);
2001 irq_dispose_mapping(irq);
2002 }
2003 }
2004}
2005
2006static void
2007mt7530_free_irq_common(struct mt7530_priv *priv)
2008{
2009 free_irq(priv->irq, priv);
2010 irq_domain_remove(priv->irq_domain);
2011}
2012
2013static void
2014mt7530_free_irq(struct mt7530_priv *priv)
2015{
2016 mt7530_free_mdio_irq(priv);
2017 mt7530_free_irq_common(priv);
2018}
2019
2020static int
2021mt7530_setup_mdio(struct mt7530_priv *priv)
2022{
2023 struct dsa_switch *ds = priv->ds;
2024 struct device *dev = priv->dev;
2025 struct mii_bus *bus;
2026 static int idx;
2027 int ret;
2028
2029 bus = devm_mdiobus_alloc(dev);
2030 if (!bus)
2031 return -ENOMEM;
2032
2033 ds->slave_mii_bus = bus;
2034 bus->priv = priv;
2035 bus->name = KBUILD_MODNAME "-mii";
2036 snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
2037 bus->read = mt753x_phy_read;
2038 bus->write = mt753x_phy_write;
2039 bus->parent = dev;
2040 bus->phy_mask = ~ds->phys_mii_mask;
2041
2042 if (priv->irq)
2043 mt7530_setup_mdio_irq(priv);
2044
2045 ret = mdiobus_register(bus);
2046 if (ret) {
2047 dev_err(dev, "failed to register MDIO bus: %d\n", ret);
2048 if (priv->irq)
2049 mt7530_free_mdio_irq(priv);
2050 }
2051
2052 return ret;
2053}
2054
Sean Wangb8f126a2017-04-07 16:45:09 +08002055static int
2056mt7530_setup(struct dsa_switch *ds)
2057{
2058 struct mt7530_priv *priv = ds->priv;
René van Dorst38f790a2019-09-02 15:02:26 +02002059 struct device_node *phy_node;
2060 struct device_node *mac_np;
Sean Wangb8f126a2017-04-07 16:45:09 +08002061 struct mt7530_dummy_poll p;
René van Dorst38f790a2019-09-02 15:02:26 +02002062 phy_interface_t interface;
René van Dorstca366d62019-09-02 15:02:24 +02002063 struct device_node *dn;
2064 u32 id, val;
2065 int ret, i;
Sean Wangb8f126a2017-04-07 16:45:09 +08002066
Vivien Didelot0abfd492017-09-20 12:28:05 -04002067 /* The parent node of master netdev which holds the common system
Sean Wangb8f126a2017-04-07 16:45:09 +08002068 * controller also is the container for two GMACs nodes representing
2069 * as two netdev instances.
2070 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04002071 dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent;
DENG Qingfang0b69c542021-08-04 00:04:01 +08002072 ds->assisted_learning_on_cpu_port = true;
DENG Qingfang771c8902020-12-11 01:03:22 +08002073 ds->mtu_enforcement_ingress = true;
Sean Wangb8f126a2017-04-07 16:45:09 +08002074
Greg Ungererddda1ac2019-01-30 11:24:05 +10002075 if (priv->id == ID_MT7530) {
Greg Ungererddda1ac2019-01-30 11:24:05 +10002076 regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
2077 ret = regulator_enable(priv->core_pwr);
2078 if (ret < 0) {
2079 dev_err(priv->dev,
2080 "Failed to enable core power: %d\n", ret);
2081 return ret;
2082 }
2083
2084 regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
2085 ret = regulator_enable(priv->io_pwr);
2086 if (ret < 0) {
2087 dev_err(priv->dev, "Failed to enable io pwr: %d\n",
2088 ret);
2089 return ret;
2090 }
Sean Wangb8f126a2017-04-07 16:45:09 +08002091 }
2092
2093 /* Reset whole chip through gpio pin or memory-mapped registers for
2094 * different type of hardware
2095 */
2096 if (priv->mcm) {
2097 reset_control_assert(priv->rstc);
2098 usleep_range(1000, 1100);
2099 reset_control_deassert(priv->rstc);
2100 } else {
2101 gpiod_set_value_cansleep(priv->reset, 0);
2102 usleep_range(1000, 1100);
2103 gpiod_set_value_cansleep(priv->reset, 1);
2104 }
2105
2106 /* Waiting for MT7530 got to stable */
2107 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2108 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2109 20, 1000000);
2110 if (ret < 0) {
2111 dev_err(priv->dev, "reset timeout\n");
2112 return ret;
2113 }
2114
2115 id = mt7530_read(priv, MT7530_CREV);
2116 id >>= CHIP_NAME_SHIFT;
2117 if (id != MT7530_ID) {
2118 dev_err(priv->dev, "chip %x can't be supported\n", id);
2119 return -ENODEV;
2120 }
2121
2122 /* Reset the switch through internal reset */
2123 mt7530_write(priv, MT7530_SYS_CTRL,
2124 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2125 SYS_CTRL_REG_RST);
2126
2127 /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
2128 val = mt7530_read(priv, MT7530_MHWTRAP);
2129 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
2130 val |= MHWTRAP_MANUAL;
2131 mt7530_write(priv, MT7530_MHWTRAP, val);
2132
René van Dorstca366d62019-09-02 15:02:24 +02002133 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2134
Sean Wangb8f126a2017-04-07 16:45:09 +08002135 /* Enable and reset MIB counters */
2136 mt7530_mib_reset(ds);
2137
Sean Wangb8f126a2017-04-07 16:45:09 +08002138 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2139 /* Disable forwarding by default on all ports */
2140 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2141 PCR_MATRIX_CLR);
2142
DENG Qingfang0b69c542021-08-04 00:04:01 +08002143 /* Disable learning by default on all ports */
2144 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2145
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002146 if (dsa_is_cpu_port(ds, i)) {
2147 ret = mt753x_cpu_port_enable(ds, i);
2148 if (ret)
2149 return ret;
DENG Qingfang5a308332021-03-16 01:09:40 +08002150 } else {
Andrew Lunn75104db2019-02-24 20:44:43 +01002151 mt7530_port_disable(ds, i);
DENG Qingfang60871752021-08-04 00:04:02 +08002152
2153 /* Set default PVID to 0 on all user ports */
2154 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2155 G0_PORT_VID_DEF);
DENG Qingfang5a308332021-03-16 01:09:40 +08002156 }
DENG Qingfange0451242020-04-14 14:34:08 +08002157 /* Enable consistent egress tag */
2158 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2159 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
Sean Wangb8f126a2017-04-07 16:45:09 +08002160 }
2161
René van Dorst38f790a2019-09-02 15:02:26 +02002162 /* Setup port 5 */
2163 priv->p5_intf_sel = P5_DISABLED;
2164 interface = PHY_INTERFACE_MODE_NA;
2165
2166 if (!dsa_is_unused_port(ds, 5)) {
2167 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002168 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface);
2169 if (ret && ret != -ENODEV)
2170 return ret;
René van Dorst38f790a2019-09-02 15:02:26 +02002171 } else {
2172 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
2173 for_each_child_of_node(dn, mac_np) {
2174 if (!of_device_is_compatible(mac_np,
2175 "mediatek,eth-mac"))
2176 continue;
2177
2178 ret = of_property_read_u32(mac_np, "reg", &id);
2179 if (ret < 0 || id != 1)
2180 continue;
2181
2182 phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
Chuanhong Guo04528002020-04-03 19:28:24 +08002183 if (!phy_node)
2184 continue;
2185
René van Dorst38f790a2019-09-02 15:02:26 +02002186 if (phy_node->parent == priv->dev->of_node->parent) {
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002187 ret = of_get_phy_mode(mac_np, &interface);
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302188 if (ret && ret != -ENODEV) {
2189 of_node_put(mac_np);
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01002190 return ret;
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302191 }
René van Dorst38f790a2019-09-02 15:02:26 +02002192 id = of_mdio_parse_addr(ds->dev, phy_node);
2193 if (id == 0)
2194 priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
2195 if (id == 4)
2196 priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
2197 }
Sumera Priyadarsini8e4efd42020-08-25 01:33:11 +05302198 of_node_put(mac_np);
René van Dorst38f790a2019-09-02 15:02:26 +02002199 of_node_put(phy_node);
2200 break;
2201 }
2202 }
2203
DENG Qingfang63c75c02021-02-26 14:32:26 +08002204#ifdef CONFIG_GPIOLIB
DENG Qingfang429a0ed2021-01-25 12:43:22 +08002205 if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
2206 ret = mt7530_setup_gpio(priv);
2207 if (ret)
2208 return ret;
2209 }
DENG Qingfang63c75c02021-02-26 14:32:26 +08002210#endif /* CONFIG_GPIOLIB */
DENG Qingfang429a0ed2021-01-25 12:43:22 +08002211
René van Dorst38f790a2019-09-02 15:02:26 +02002212 mt7530_setup_port5(ds, interface);
2213
Sean Wangb8f126a2017-04-07 16:45:09 +08002214 /* Flush the FDB table */
Florian Fainelli18bd5942018-04-02 16:24:14 -07002215 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
Sean Wangb8f126a2017-04-07 16:45:09 +08002216 if (ret < 0)
2217 return ret;
2218
2219 return 0;
2220}
2221
Landen Chaoc2885752020-09-11 21:48:54 +08002222static int
2223mt7531_setup(struct dsa_switch *ds)
2224{
2225 struct mt7530_priv *priv = ds->priv;
2226 struct mt7530_dummy_poll p;
2227 u32 val, id;
2228 int ret, i;
2229
2230 /* Reset whole chip through gpio pin or memory-mapped registers for
2231 * different type of hardware
2232 */
2233 if (priv->mcm) {
2234 reset_control_assert(priv->rstc);
2235 usleep_range(1000, 1100);
2236 reset_control_deassert(priv->rstc);
2237 } else {
2238 gpiod_set_value_cansleep(priv->reset, 0);
2239 usleep_range(1000, 1100);
2240 gpiod_set_value_cansleep(priv->reset, 1);
2241 }
2242
2243 /* Waiting for MT7530 got to stable */
2244 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
2245 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
2246 20, 1000000);
2247 if (ret < 0) {
2248 dev_err(priv->dev, "reset timeout\n");
2249 return ret;
2250 }
2251
2252 id = mt7530_read(priv, MT7531_CREV);
2253 id >>= CHIP_NAME_SHIFT;
2254
2255 if (id != MT7531_ID) {
2256 dev_err(priv->dev, "chip %x can't be supported\n", id);
2257 return -ENODEV;
2258 }
2259
2260 /* Reset the switch through internal reset */
2261 mt7530_write(priv, MT7530_SYS_CTRL,
2262 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
2263 SYS_CTRL_REG_RST);
2264
2265 if (mt7531_dual_sgmii_supported(priv)) {
2266 priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII;
2267
2268 /* Let ds->slave_mii_bus be able to access external phy. */
2269 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
2270 MT7531_EXT_P_MDC_11);
2271 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
2272 MT7531_EXT_P_MDIO_12);
2273 } else {
2274 priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
2275 }
2276 dev_dbg(ds->dev, "P5 support %s interface\n",
2277 p5_intf_modes(priv->p5_intf_sel));
2278
2279 mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
2280 MT7531_GPIO0_INTERRUPT);
2281
2282 /* Let phylink decide the interface later. */
2283 priv->p5_interface = PHY_INTERFACE_MODE_NA;
2284 priv->p6_interface = PHY_INTERFACE_MODE_NA;
2285
2286 /* Enable PHY core PLL, since phy_device has not yet been created
2287 * provided for phy_[read,write]_mmd_indirect is called, we provide
2288 * our own mt7531_ind_mmd_phy_[read,write] to complete this
2289 * function.
2290 */
2291 val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR,
2292 MDIO_MMD_VEND2, CORE_PLL_GROUP4);
2293 val |= MT7531_PHY_PLL_BYPASS_MODE;
2294 val &= ~MT7531_PHY_PLL_OFF;
2295 mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2,
2296 CORE_PLL_GROUP4, val);
2297
2298 /* BPDU to CPU port */
2299 mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK,
2300 BIT(MT7530_CPU_PORT));
2301 mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK,
2302 MT753X_BPDU_CPU_ONLY);
2303
2304 /* Enable and reset MIB counters */
2305 mt7530_mib_reset(ds);
2306
2307 for (i = 0; i < MT7530_NUM_PORTS; i++) {
2308 /* Disable forwarding by default on all ports */
2309 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
2310 PCR_MATRIX_CLR);
2311
DENG Qingfang0b69c542021-08-04 00:04:01 +08002312 /* Disable learning by default on all ports */
2313 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
2314
Landen Chaoc2885752020-09-11 21:48:54 +08002315 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
2316
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002317 if (dsa_is_cpu_port(ds, i)) {
2318 ret = mt753x_cpu_port_enable(ds, i);
2319 if (ret)
2320 return ret;
DENG Qingfang5a308332021-03-16 01:09:40 +08002321 } else {
Landen Chaoc2885752020-09-11 21:48:54 +08002322 mt7530_port_disable(ds, i);
DENG Qingfang60871752021-08-04 00:04:02 +08002323
2324 /* Set default PVID to 0 on all user ports */
2325 mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
2326 G0_PORT_VID_DEF);
DENG Qingfang5a308332021-03-16 01:09:40 +08002327 }
2328
Landen Chaoc2885752020-09-11 21:48:54 +08002329 /* Enable consistent egress tag */
2330 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
2331 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
2332 }
2333
DENG Qingfang0b69c542021-08-04 00:04:01 +08002334 ds->assisted_learning_on_cpu_port = true;
DENG Qingfang771c8902020-12-11 01:03:22 +08002335 ds->mtu_enforcement_ingress = true;
Landen Chaoc2885752020-09-11 21:48:54 +08002336
2337 /* Flush the FDB table */
2338 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
2339 if (ret < 0)
2340 return ret;
2341
2342 return 0;
2343}
2344
Landen Chao88bdef82020-09-11 21:48:52 +08002345static bool
2346mt7530_phy_mode_supported(struct dsa_switch *ds, int port,
2347 const struct phylink_link_state *state)
2348{
2349 struct mt7530_priv *priv = ds->priv;
2350
2351 switch (port) {
2352 case 0 ... 4: /* Internal phy */
2353 if (state->interface != PHY_INTERFACE_MODE_GMII)
2354 return false;
2355 break;
2356 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2357 if (!phy_interface_mode_is_rgmii(state->interface) &&
2358 state->interface != PHY_INTERFACE_MODE_MII &&
2359 state->interface != PHY_INTERFACE_MODE_GMII)
2360 return false;
2361 break;
2362 case 6: /* 1st cpu port */
2363 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
2364 state->interface != PHY_INTERFACE_MODE_TRGMII)
2365 return false;
2366 break;
2367 default:
2368 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2369 port);
2370 return false;
2371 }
2372
2373 return true;
2374}
2375
Landen Chaoc2885752020-09-11 21:48:54 +08002376static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port)
2377{
2378 return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII);
2379}
2380
2381static bool
2382mt7531_phy_mode_supported(struct dsa_switch *ds, int port,
2383 const struct phylink_link_state *state)
2384{
2385 struct mt7530_priv *priv = ds->priv;
2386
2387 switch (port) {
2388 case 0 ... 4: /* Internal phy */
2389 if (state->interface != PHY_INTERFACE_MODE_GMII)
2390 return false;
2391 break;
2392 case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */
2393 if (mt7531_is_rgmii_port(priv, port))
2394 return phy_interface_mode_is_rgmii(state->interface);
2395 fallthrough;
2396 case 6: /* 1st cpu port supports sgmii/8023z only */
2397 if (state->interface != PHY_INTERFACE_MODE_SGMII &&
2398 !phy_interface_mode_is_8023z(state->interface))
2399 return false;
2400 break;
2401 default:
2402 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
2403 port);
2404 return false;
2405 }
2406
2407 return true;
2408}
2409
Landen Chao88bdef82020-09-11 21:48:52 +08002410static bool
2411mt753x_phy_mode_supported(struct dsa_switch *ds, int port,
2412 const struct phylink_link_state *state)
2413{
2414 struct mt7530_priv *priv = ds->priv;
2415
2416 return priv->info->phy_mode_supported(ds, port, state);
2417}
2418
2419static int
2420mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state)
2421{
2422 struct mt7530_priv *priv = ds->priv;
2423
2424 return priv->info->pad_setup(ds, state->interface);
2425}
2426
2427static int
2428mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2429 phy_interface_t interface)
2430{
2431 struct mt7530_priv *priv = ds->priv;
2432
2433 /* Only need to setup port5. */
2434 if (port != 5)
2435 return 0;
2436
2437 mt7530_setup_port5(priv->ds, interface);
2438
2439 return 0;
2440}
2441
Landen Chaoc2885752020-09-11 21:48:54 +08002442static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port,
2443 phy_interface_t interface,
2444 struct phy_device *phydev)
2445{
2446 u32 val;
2447
2448 if (!mt7531_is_rgmii_port(priv, port)) {
2449 dev_err(priv->dev, "RGMII mode is not available for port %d\n",
2450 port);
2451 return -EINVAL;
2452 }
2453
2454 val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
2455 val |= GP_CLK_EN;
2456 val &= ~GP_MODE_MASK;
2457 val |= GP_MODE(MT7531_GP_MODE_RGMII);
2458 val &= ~CLK_SKEW_IN_MASK;
2459 val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
2460 val &= ~CLK_SKEW_OUT_MASK;
2461 val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
2462 val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
2463
2464 /* Do not adjust rgmii delay when vendor phy driver presents. */
2465 if (!phydev || phy_driver_is_genphy(phydev)) {
2466 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
2467 switch (interface) {
2468 case PHY_INTERFACE_MODE_RGMII:
2469 val |= TXCLK_NO_REVERSE;
2470 val |= RXCLK_NO_DELAY;
2471 break;
2472 case PHY_INTERFACE_MODE_RGMII_RXID:
2473 val |= TXCLK_NO_REVERSE;
2474 break;
2475 case PHY_INTERFACE_MODE_RGMII_TXID:
2476 val |= RXCLK_NO_DELAY;
2477 break;
2478 case PHY_INTERFACE_MODE_RGMII_ID:
2479 break;
2480 default:
2481 return -EINVAL;
2482 }
2483 }
2484 mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
2485
2486 return 0;
2487}
2488
2489static void mt7531_sgmii_validate(struct mt7530_priv *priv, int port,
2490 unsigned long *supported)
2491{
2492 /* Port5 supports ethier RGMII or SGMII.
2493 * Port6 supports SGMII only.
2494 */
2495 switch (port) {
2496 case 5:
2497 if (mt7531_is_rgmii_port(priv, port))
2498 break;
2499 fallthrough;
2500 case 6:
2501 phylink_set(supported, 1000baseX_Full);
2502 phylink_set(supported, 2500baseX_Full);
2503 phylink_set(supported, 2500baseT_Full);
2504 }
2505}
2506
2507static void
2508mt7531_sgmii_link_up_force(struct dsa_switch *ds, int port,
2509 unsigned int mode, phy_interface_t interface,
2510 int speed, int duplex)
2511{
2512 struct mt7530_priv *priv = ds->priv;
2513 unsigned int val;
2514
2515 /* For adjusting speed and duplex of SGMII force mode. */
2516 if (interface != PHY_INTERFACE_MODE_SGMII ||
2517 phylink_autoneg_inband(mode))
2518 return;
2519
2520 /* SGMII force mode setting */
2521 val = mt7530_read(priv, MT7531_SGMII_MODE(port));
2522 val &= ~MT7531_SGMII_IF_MODE_MASK;
2523
2524 switch (speed) {
2525 case SPEED_10:
2526 val |= MT7531_SGMII_FORCE_SPEED_10;
2527 break;
2528 case SPEED_100:
2529 val |= MT7531_SGMII_FORCE_SPEED_100;
2530 break;
2531 case SPEED_1000:
2532 val |= MT7531_SGMII_FORCE_SPEED_1000;
2533 break;
2534 }
2535
2536 /* MT7531 SGMII 1G force mode can only work in full duplex mode,
2537 * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2538 */
2539 if ((speed == SPEED_10 || speed == SPEED_100) &&
2540 duplex != DUPLEX_FULL)
2541 val |= MT7531_SGMII_FORCE_HALF_DUPLEX;
2542
2543 mt7530_write(priv, MT7531_SGMII_MODE(port), val);
2544}
2545
2546static bool mt753x_is_mac_port(u32 port)
2547{
2548 return (port == 5 || port == 6);
2549}
2550
2551static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port,
2552 phy_interface_t interface)
2553{
2554 u32 val;
2555
2556 if (!mt753x_is_mac_port(port))
2557 return -EINVAL;
2558
2559 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2560 MT7531_SGMII_PHYA_PWD);
2561
2562 val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port));
2563 val &= ~MT7531_RG_TPHY_SPEED_MASK;
2564 /* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B
2565 * encoding.
2566 */
2567 val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ?
2568 MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G;
2569 mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val);
2570
2571 mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2572
2573 /* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex
2574 * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not.
2575 */
2576 mt7530_rmw(priv, MT7531_SGMII_MODE(port),
2577 MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS,
2578 MT7531_SGMII_FORCE_SPEED_1000);
2579
2580 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2581
2582 return 0;
2583}
2584
2585static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port,
2586 phy_interface_t interface)
2587{
2588 if (!mt753x_is_mac_port(port))
2589 return -EINVAL;
2590
2591 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
2592 MT7531_SGMII_PHYA_PWD);
2593
2594 mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
2595 MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G);
2596
2597 mt7530_set(priv, MT7531_SGMII_MODE(port),
2598 MT7531_SGMII_REMOTE_FAULT_DIS |
2599 MT7531_SGMII_SPEED_DUPLEX_AN);
2600
2601 mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port),
2602 MT7531_SGMII_TX_CONFIG_MASK, 1);
2603
2604 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE);
2605
2606 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART);
2607
2608 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0);
2609
2610 return 0;
2611}
2612
2613static void mt7531_sgmii_restart_an(struct dsa_switch *ds, int port)
2614{
2615 struct mt7530_priv *priv = ds->priv;
2616 u32 val;
2617
2618 /* Only restart AN when AN is enabled */
2619 val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2620 if (val & MT7531_SGMII_AN_ENABLE) {
2621 val |= MT7531_SGMII_AN_RESTART;
2622 mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val);
2623 }
2624}
2625
2626static int
2627mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2628 phy_interface_t interface)
2629{
2630 struct mt7530_priv *priv = ds->priv;
2631 struct phy_device *phydev;
2632 struct dsa_port *dp;
2633
2634 if (!mt753x_is_mac_port(port)) {
2635 dev_err(priv->dev, "port %d is not a MAC port\n", port);
2636 return -EINVAL;
2637 }
2638
2639 switch (interface) {
2640 case PHY_INTERFACE_MODE_RGMII:
2641 case PHY_INTERFACE_MODE_RGMII_ID:
2642 case PHY_INTERFACE_MODE_RGMII_RXID:
2643 case PHY_INTERFACE_MODE_RGMII_TXID:
2644 dp = dsa_to_port(ds, port);
2645 phydev = dp->slave->phydev;
2646 return mt7531_rgmii_setup(priv, port, interface, phydev);
2647 case PHY_INTERFACE_MODE_SGMII:
2648 return mt7531_sgmii_setup_mode_an(priv, port, interface);
2649 case PHY_INTERFACE_MODE_NA:
2650 case PHY_INTERFACE_MODE_1000BASEX:
2651 case PHY_INTERFACE_MODE_2500BASEX:
2652 if (phylink_autoneg_inband(mode))
2653 return -EINVAL;
2654
2655 return mt7531_sgmii_setup_mode_force(priv, port, interface);
2656 default:
2657 return -EINVAL;
2658 }
2659
2660 return -EINVAL;
2661}
2662
Landen Chao88bdef82020-09-11 21:48:52 +08002663static int
2664mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2665 const struct phylink_link_state *state)
2666{
2667 struct mt7530_priv *priv = ds->priv;
2668
2669 return priv->info->mac_port_config(ds, port, mode, state->interface);
2670}
2671
2672static void
2673mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
2674 const struct phylink_link_state *state)
René van Dorstca366d62019-09-02 15:02:24 +02002675{
2676 struct mt7530_priv *priv = ds->priv;
2677 u32 mcr_cur, mcr_new;
2678
Landen Chao88bdef82020-09-11 21:48:52 +08002679 if (!mt753x_phy_mode_supported(ds, port, state))
2680 goto unsupported;
2681
René van Dorstca366d62019-09-02 15:02:24 +02002682 switch (port) {
Landen Chao88bdef82020-09-11 21:48:52 +08002683 case 0 ... 4: /* Internal phy */
René van Dorstca366d62019-09-02 15:02:24 +02002684 if (state->interface != PHY_INTERFACE_MODE_GMII)
Landen Chao88bdef82020-09-11 21:48:52 +08002685 goto unsupported;
René van Dorstca366d62019-09-02 15:02:24 +02002686 break;
René van Dorst38f790a2019-09-02 15:02:26 +02002687 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
2688 if (priv->p5_interface == state->interface)
2689 break;
René van Dorst38f790a2019-09-02 15:02:26 +02002690
Landen Chao88bdef82020-09-11 21:48:52 +08002691 if (mt753x_mac_config(ds, port, mode, state) < 0)
2692 goto unsupported;
2693
Landen Chaoc2885752020-09-11 21:48:54 +08002694 if (priv->p5_intf_sel != P5_DISABLED)
2695 priv->p5_interface = state->interface;
René van Dorst38f790a2019-09-02 15:02:26 +02002696 break;
René van Dorstca366d62019-09-02 15:02:24 +02002697 case 6: /* 1st cpu port */
2698 if (priv->p6_interface == state->interface)
2699 break;
2700
Landen Chao88bdef82020-09-11 21:48:52 +08002701 mt753x_pad_setup(ds, state);
René van Dorstca366d62019-09-02 15:02:24 +02002702
Landen Chao88bdef82020-09-11 21:48:52 +08002703 if (mt753x_mac_config(ds, port, mode, state) < 0)
2704 goto unsupported;
René van Dorstca366d62019-09-02 15:02:24 +02002705
René van Dorstca366d62019-09-02 15:02:24 +02002706 priv->p6_interface = state->interface;
2707 break;
2708 default:
Landen Chao88bdef82020-09-11 21:48:52 +08002709unsupported:
2710 dev_err(ds->dev, "%s: unsupported %s port: %i\n",
2711 __func__, phy_modes(state->interface), port);
René van Dorstca366d62019-09-02 15:02:24 +02002712 return;
2713 }
2714
Landen Chaoc2885752020-09-11 21:48:54 +08002715 if (phylink_autoneg_inband(mode) &&
2716 state->interface != PHY_INTERFACE_MODE_SGMII) {
René van Dorstca366d62019-09-02 15:02:24 +02002717 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
2718 __func__);
2719 return;
2720 }
2721
2722 mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
2723 mcr_new = mcr_cur;
René van Dorst1d011452020-03-27 15:44:12 +01002724 mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
René van Dorstca366d62019-09-02 15:02:24 +02002725 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
Landen Chaoc2885752020-09-11 21:48:54 +08002726 PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id);
René van Dorstca366d62019-09-02 15:02:24 +02002727
René van Dorst38f790a2019-09-02 15:02:26 +02002728 /* Are we connected to external phy */
2729 if (port == 5 && dsa_is_user_port(ds, 5))
2730 mcr_new |= PMCR_EXT_PHY;
2731
René van Dorstca366d62019-09-02 15:02:24 +02002732 if (mcr_new != mcr_cur)
2733 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
2734}
2735
Landen Chaoc2885752020-09-11 21:48:54 +08002736static void
2737mt753x_phylink_mac_an_restart(struct dsa_switch *ds, int port)
2738{
2739 struct mt7530_priv *priv = ds->priv;
2740
2741 if (!priv->info->mac_pcs_an_restart)
2742 return;
2743
2744 priv->info->mac_pcs_an_restart(ds, port);
2745}
2746
2747static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port,
René van Dorstca366d62019-09-02 15:02:24 +02002748 unsigned int mode,
2749 phy_interface_t interface)
2750{
2751 struct mt7530_priv *priv = ds->priv;
2752
René van Dorst1d011452020-03-27 15:44:12 +01002753 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
René van Dorstca366d62019-09-02 15:02:24 +02002754}
2755
Landen Chaoc2885752020-09-11 21:48:54 +08002756static void mt753x_mac_pcs_link_up(struct dsa_switch *ds, int port,
2757 unsigned int mode, phy_interface_t interface,
2758 int speed, int duplex)
2759{
2760 struct mt7530_priv *priv = ds->priv;
2761
2762 if (!priv->info->mac_pcs_link_up)
2763 return;
2764
2765 priv->info->mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2766}
2767
2768static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port,
René van Dorstca366d62019-09-02 15:02:24 +02002769 unsigned int mode,
2770 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00002771 struct phy_device *phydev,
2772 int speed, int duplex,
2773 bool tx_pause, bool rx_pause)
René van Dorstca366d62019-09-02 15:02:24 +02002774{
2775 struct mt7530_priv *priv = ds->priv;
René van Dorst1d011452020-03-27 15:44:12 +01002776 u32 mcr;
René van Dorstca366d62019-09-02 15:02:24 +02002777
Landen Chaoc2885752020-09-11 21:48:54 +08002778 mt753x_mac_pcs_link_up(ds, port, mode, interface, speed, duplex);
2779
René van Dorst1d011452020-03-27 15:44:12 +01002780 mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
2781
Landen Chaoc2885752020-09-11 21:48:54 +08002782 /* MT753x MAC works in 1G full duplex mode for all up-clocked
2783 * variants.
2784 */
2785 if (interface == PHY_INTERFACE_MODE_TRGMII ||
2786 (phy_interface_mode_is_8023z(interface))) {
2787 speed = SPEED_1000;
2788 duplex = DUPLEX_FULL;
2789 }
2790
René van Dorst1d011452020-03-27 15:44:12 +01002791 switch (speed) {
2792 case SPEED_1000:
2793 mcr |= PMCR_FORCE_SPEED_1000;
2794 break;
2795 case SPEED_100:
2796 mcr |= PMCR_FORCE_SPEED_100;
2797 break;
2798 }
2799 if (duplex == DUPLEX_FULL) {
2800 mcr |= PMCR_FORCE_FDX;
2801 if (tx_pause)
2802 mcr |= PMCR_TX_FC_EN;
2803 if (rx_pause)
2804 mcr |= PMCR_RX_FC_EN;
2805 }
2806
René van Dorst40b5d2f2021-04-12 08:50:31 +02002807 if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, 0) >= 0) {
2808 switch (speed) {
2809 case SPEED_1000:
2810 mcr |= PMCR_FORCE_EEE1G;
2811 break;
2812 case SPEED_100:
2813 mcr |= PMCR_FORCE_EEE100;
2814 break;
2815 }
2816 }
2817
René van Dorst1d011452020-03-27 15:44:12 +01002818 mt7530_set(priv, MT7530_PMCR_P(port), mcr);
René van Dorstca366d62019-09-02 15:02:24 +02002819}
2820
Landen Chaoc2885752020-09-11 21:48:54 +08002821static int
2822mt7531_cpu_port_config(struct dsa_switch *ds, int port)
2823{
2824 struct mt7530_priv *priv = ds->priv;
2825 phy_interface_t interface;
2826 int speed;
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002827 int ret;
Landen Chaoc2885752020-09-11 21:48:54 +08002828
2829 switch (port) {
2830 case 5:
2831 if (mt7531_is_rgmii_port(priv, port))
2832 interface = PHY_INTERFACE_MODE_RGMII;
2833 else
2834 interface = PHY_INTERFACE_MODE_2500BASEX;
2835
2836 priv->p5_interface = interface;
2837 break;
2838 case 6:
2839 interface = PHY_INTERFACE_MODE_2500BASEX;
2840
2841 mt7531_pad_setup(ds, interface);
2842
2843 priv->p6_interface = interface;
2844 break;
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002845 default:
2846 return -EINVAL;
Landen Chaoc2885752020-09-11 21:48:54 +08002847 }
2848
2849 if (interface == PHY_INTERFACE_MODE_2500BASEX)
2850 speed = SPEED_2500;
2851 else
2852 speed = SPEED_1000;
2853
Alex Dewar0ce0c3c2020-09-19 20:28:10 +01002854 ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
2855 if (ret)
2856 return ret;
Landen Chaoc2885752020-09-11 21:48:54 +08002857 mt7530_write(priv, MT7530_PMCR_P(port),
2858 PMCR_CPU_PORT_SETTING(priv->id));
2859 mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2860 speed, DUPLEX_FULL, true, true);
2861
2862 return 0;
2863}
2864
Landen Chao88bdef82020-09-11 21:48:52 +08002865static void
2866mt7530_mac_port_validate(struct dsa_switch *ds, int port,
2867 unsigned long *supported)
2868{
2869 if (port == 5)
2870 phylink_set(supported, 1000baseX_Full);
2871}
2872
Landen Chaoc2885752020-09-11 21:48:54 +08002873static void mt7531_mac_port_validate(struct dsa_switch *ds, int port,
2874 unsigned long *supported)
2875{
2876 struct mt7530_priv *priv = ds->priv;
2877
2878 mt7531_sgmii_validate(priv, port, supported);
2879}
2880
Landen Chao88bdef82020-09-11 21:48:52 +08002881static void
2882mt753x_phylink_validate(struct dsa_switch *ds, int port,
2883 unsigned long *supported,
2884 struct phylink_link_state *state)
René van Dorstca366d62019-09-02 15:02:24 +02002885{
2886 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Landen Chao88bdef82020-09-11 21:48:52 +08002887 struct mt7530_priv *priv = ds->priv;
René van Dorstca366d62019-09-02 15:02:24 +02002888
Landen Chao88bdef82020-09-11 21:48:52 +08002889 if (state->interface != PHY_INTERFACE_MODE_NA &&
2890 !mt753x_phy_mode_supported(ds, port, state)) {
René van Dorstca366d62019-09-02 15:02:24 +02002891 linkmode_zero(supported);
2892 return;
2893 }
2894
2895 phylink_set_port_modes(mask);
René van Dorstca366d62019-09-02 15:02:24 +02002896
Landen Chaoc2885752020-09-11 21:48:54 +08002897 if (state->interface != PHY_INTERFACE_MODE_TRGMII ||
2898 !phy_interface_mode_is_8023z(state->interface)) {
René van Dorstca366d62019-09-02 15:02:24 +02002899 phylink_set(mask, 10baseT_Half);
2900 phylink_set(mask, 10baseT_Full);
2901 phylink_set(mask, 100baseT_Half);
2902 phylink_set(mask, 100baseT_Full);
Landen Chao88bdef82020-09-11 21:48:52 +08002903 phylink_set(mask, Autoneg);
René van Dorst38f790a2019-09-02 15:02:26 +02002904 }
René van Dorstca366d62019-09-02 15:02:24 +02002905
Landen Chao88bdef82020-09-11 21:48:52 +08002906 /* This switch only supports 1G full-duplex. */
2907 if (state->interface != PHY_INTERFACE_MODE_MII)
2908 phylink_set(mask, 1000baseT_Full);
2909
2910 priv->info->mac_port_validate(ds, port, mask);
2911
René van Dorstca366d62019-09-02 15:02:24 +02002912 phylink_set(mask, Pause);
2913 phylink_set(mask, Asym_Pause);
2914
2915 linkmode_and(supported, supported, mask);
2916 linkmode_and(state->advertising, state->advertising, mask);
Landen Chaoc2885752020-09-11 21:48:54 +08002917
2918 /* We can only operate at 2500BaseX or 1000BaseX. If requested
2919 * to advertise both, only report advertising at 2500BaseX.
2920 */
2921 phylink_helper_basex_speed(state);
René van Dorstca366d62019-09-02 15:02:24 +02002922}
2923
2924static int
2925mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
2926 struct phylink_link_state *state)
2927{
2928 struct mt7530_priv *priv = ds->priv;
2929 u32 pmsr;
2930
2931 if (port < 0 || port >= MT7530_NUM_PORTS)
2932 return -EINVAL;
2933
2934 pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
2935
2936 state->link = (pmsr & PMSR_LINK);
2937 state->an_complete = state->link;
2938 state->duplex = !!(pmsr & PMSR_DPX);
2939
2940 switch (pmsr & PMSR_SPEED_MASK) {
2941 case PMSR_SPEED_10:
2942 state->speed = SPEED_10;
2943 break;
2944 case PMSR_SPEED_100:
2945 state->speed = SPEED_100;
2946 break;
2947 case PMSR_SPEED_1000:
2948 state->speed = SPEED_1000;
2949 break;
2950 default:
2951 state->speed = SPEED_UNKNOWN;
2952 break;
2953 }
2954
2955 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2956 if (pmsr & PMSR_RX_FC)
2957 state->pause |= MLO_PAUSE_RX;
2958 if (pmsr & PMSR_TX_FC)
2959 state->pause |= MLO_PAUSE_TX;
2960
2961 return 1;
2962}
2963
Landen Chao88bdef82020-09-11 21:48:52 +08002964static int
Landen Chaoc2885752020-09-11 21:48:54 +08002965mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port,
2966 struct phylink_link_state *state)
2967{
2968 u32 status, val;
2969 u16 config_reg;
2970
2971 status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port));
2972 state->link = !!(status & MT7531_SGMII_LINK_STATUS);
2973 if (state->interface == PHY_INTERFACE_MODE_SGMII &&
2974 (status & MT7531_SGMII_AN_ENABLE)) {
2975 val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port));
2976 config_reg = val >> 16;
2977
2978 switch (config_reg & LPA_SGMII_SPD_MASK) {
2979 case LPA_SGMII_1000:
2980 state->speed = SPEED_1000;
2981 break;
2982 case LPA_SGMII_100:
2983 state->speed = SPEED_100;
2984 break;
2985 case LPA_SGMII_10:
2986 state->speed = SPEED_10;
2987 break;
2988 default:
2989 dev_err(priv->dev, "invalid sgmii PHY speed\n");
2990 state->link = false;
2991 return -EINVAL;
2992 }
2993
2994 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2995 state->duplex = DUPLEX_FULL;
2996 else
2997 state->duplex = DUPLEX_HALF;
2998 }
2999
3000 return 0;
3001}
3002
3003static int
3004mt7531_phylink_mac_link_state(struct dsa_switch *ds, int port,
3005 struct phylink_link_state *state)
3006{
3007 struct mt7530_priv *priv = ds->priv;
3008
3009 if (state->interface == PHY_INTERFACE_MODE_SGMII)
3010 return mt7531_sgmii_pcs_get_state_an(priv, port, state);
3011
3012 return -EOPNOTSUPP;
3013}
3014
3015static int
Landen Chao88bdef82020-09-11 21:48:52 +08003016mt753x_phylink_mac_link_state(struct dsa_switch *ds, int port,
3017 struct phylink_link_state *state)
3018{
3019 struct mt7530_priv *priv = ds->priv;
3020
3021 return priv->info->mac_port_get_state(ds, port, state);
3022}
3023
3024static int
3025mt753x_setup(struct dsa_switch *ds)
3026{
3027 struct mt7530_priv *priv = ds->priv;
DENG Qingfangba751e22021-05-19 11:32:00 +08003028 int ret = priv->info->sw_setup(ds);
Landen Chao88bdef82020-09-11 21:48:52 +08003029
DENG Qingfangba751e22021-05-19 11:32:00 +08003030 if (ret)
3031 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003032
DENG Qingfangba751e22021-05-19 11:32:00 +08003033 ret = mt7530_setup_irq(priv);
3034 if (ret)
3035 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003036
DENG Qingfangba751e22021-05-19 11:32:00 +08003037 ret = mt7530_setup_mdio(priv);
3038 if (ret && priv->irq)
3039 mt7530_free_irq_common(priv);
Landen Chao88bdef82020-09-11 21:48:52 +08003040
DENG Qingfangba751e22021-05-19 11:32:00 +08003041 return ret;
Landen Chao88bdef82020-09-11 21:48:52 +08003042}
3043
René van Dorst40b5d2f2021-04-12 08:50:31 +02003044static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
3045 struct ethtool_eee *e)
3046{
3047 struct mt7530_priv *priv = ds->priv;
3048 u32 eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port));
3049
3050 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
3051 e->tx_lpi_timer = GET_LPI_THRESH(eeecr);
3052
3053 return 0;
3054}
3055
3056static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
3057 struct ethtool_eee *e)
3058{
3059 struct mt7530_priv *priv = ds->priv;
3060 u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
3061
3062 if (e->tx_lpi_timer > 0xFFF)
3063 return -EINVAL;
3064
3065 set = SET_LPI_THRESH(e->tx_lpi_timer);
3066 if (!e->tx_lpi_enabled)
3067 /* Force LPI Mode without a delay */
3068 set |= LPI_MODE_EN;
3069 mt7530_rmw(priv, MT7530_PMEEECR_P(port), mask, set);
3070
3071 return 0;
3072}
3073
Bhumika Goyald78d6772017-08-09 10:34:15 +05303074static const struct dsa_switch_ops mt7530_switch_ops = {
Sean Wangb8f126a2017-04-07 16:45:09 +08003075 .get_tag_protocol = mtk_get_tag_protocol,
Landen Chao88bdef82020-09-11 21:48:52 +08003076 .setup = mt753x_setup,
Sean Wangb8f126a2017-04-07 16:45:09 +08003077 .get_strings = mt7530_get_strings,
Sean Wangb8f126a2017-04-07 16:45:09 +08003078 .get_ethtool_stats = mt7530_get_ethtool_stats,
3079 .get_sset_count = mt7530_get_sset_count,
DENG Qingfangea6d5c92020-12-08 15:00:28 +08003080 .set_ageing_time = mt7530_set_ageing_time,
Sean Wangb8f126a2017-04-07 16:45:09 +08003081 .port_enable = mt7530_port_enable,
3082 .port_disable = mt7530_port_disable,
DENG Qingfang94701742020-11-03 13:06:18 +08003083 .port_change_mtu = mt7530_port_change_mtu,
3084 .port_max_mtu = mt7530_port_max_mtu,
Sean Wangb8f126a2017-04-07 16:45:09 +08003085 .port_stp_state_set = mt7530_stp_state_set,
DENG Qingfang5a308332021-03-16 01:09:40 +08003086 .port_pre_bridge_flags = mt7530_port_pre_bridge_flags,
3087 .port_bridge_flags = mt7530_port_bridge_flags,
3088 .port_set_mrouter = mt7530_port_set_mrouter,
Sean Wangb8f126a2017-04-07 16:45:09 +08003089 .port_bridge_join = mt7530_port_bridge_join,
3090 .port_bridge_leave = mt7530_port_bridge_leave,
Sean Wangb8f126a2017-04-07 16:45:09 +08003091 .port_fdb_add = mt7530_port_fdb_add,
3092 .port_fdb_del = mt7530_port_fdb_del,
3093 .port_fdb_dump = mt7530_port_fdb_dump,
DENG Qingfang5a308332021-03-16 01:09:40 +08003094 .port_mdb_add = mt7530_port_mdb_add,
3095 .port_mdb_del = mt7530_port_mdb_del,
Sean Wang83163f72017-12-15 12:47:00 +08003096 .port_vlan_filtering = mt7530_port_vlan_filtering,
Sean Wang83163f72017-12-15 12:47:00 +08003097 .port_vlan_add = mt7530_port_vlan_add,
3098 .port_vlan_del = mt7530_port_vlan_del,
Landen Chaoc2885752020-09-11 21:48:54 +08003099 .port_mirror_add = mt753x_port_mirror_add,
3100 .port_mirror_del = mt753x_port_mirror_del,
Landen Chao88bdef82020-09-11 21:48:52 +08003101 .phylink_validate = mt753x_phylink_validate,
3102 .phylink_mac_link_state = mt753x_phylink_mac_link_state,
3103 .phylink_mac_config = mt753x_phylink_mac_config,
Landen Chaoc2885752020-09-11 21:48:54 +08003104 .phylink_mac_an_restart = mt753x_phylink_mac_an_restart,
3105 .phylink_mac_link_down = mt753x_phylink_mac_link_down,
3106 .phylink_mac_link_up = mt753x_phylink_mac_link_up,
René van Dorst40b5d2f2021-04-12 08:50:31 +02003107 .get_mac_eee = mt753x_get_mac_eee,
3108 .set_mac_eee = mt753x_set_mac_eee,
Sean Wangb8f126a2017-04-07 16:45:09 +08003109};
3110
Landen Chao88bdef82020-09-11 21:48:52 +08003111static const struct mt753x_info mt753x_table[] = {
3112 [ID_MT7621] = {
3113 .id = ID_MT7621,
3114 .sw_setup = mt7530_setup,
3115 .phy_read = mt7530_phy_read,
3116 .phy_write = mt7530_phy_write,
3117 .pad_setup = mt7530_pad_clk_setup,
3118 .phy_mode_supported = mt7530_phy_mode_supported,
3119 .mac_port_validate = mt7530_mac_port_validate,
3120 .mac_port_get_state = mt7530_phylink_mac_link_state,
3121 .mac_port_config = mt7530_mac_config,
3122 },
3123 [ID_MT7530] = {
3124 .id = ID_MT7530,
3125 .sw_setup = mt7530_setup,
3126 .phy_read = mt7530_phy_read,
3127 .phy_write = mt7530_phy_write,
3128 .pad_setup = mt7530_pad_clk_setup,
3129 .phy_mode_supported = mt7530_phy_mode_supported,
3130 .mac_port_validate = mt7530_mac_port_validate,
3131 .mac_port_get_state = mt7530_phylink_mac_link_state,
3132 .mac_port_config = mt7530_mac_config,
3133 },
Landen Chaoc2885752020-09-11 21:48:54 +08003134 [ID_MT7531] = {
3135 .id = ID_MT7531,
3136 .sw_setup = mt7531_setup,
3137 .phy_read = mt7531_ind_phy_read,
3138 .phy_write = mt7531_ind_phy_write,
3139 .pad_setup = mt7531_pad_setup,
3140 .cpu_port_config = mt7531_cpu_port_config,
3141 .phy_mode_supported = mt7531_phy_mode_supported,
3142 .mac_port_validate = mt7531_mac_port_validate,
3143 .mac_port_get_state = mt7531_phylink_mac_link_state,
3144 .mac_port_config = mt7531_mac_config,
3145 .mac_pcs_an_restart = mt7531_sgmii_restart_an,
3146 .mac_pcs_link_up = mt7531_sgmii_link_up_force,
3147 },
Landen Chao88bdef82020-09-11 21:48:52 +08003148};
3149
Greg Ungererddda1ac2019-01-30 11:24:05 +10003150static const struct of_device_id mt7530_of_match[] = {
Landen Chao88bdef82020-09-11 21:48:52 +08003151 { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
3152 { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
Landen Chaoc2885752020-09-11 21:48:54 +08003153 { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
Greg Ungererddda1ac2019-01-30 11:24:05 +10003154 { /* sentinel */ },
3155};
3156MODULE_DEVICE_TABLE(of, mt7530_of_match);
3157
Sean Wangb8f126a2017-04-07 16:45:09 +08003158static int
3159mt7530_probe(struct mdio_device *mdiodev)
3160{
3161 struct mt7530_priv *priv;
3162 struct device_node *dn;
3163
3164 dn = mdiodev->dev.of_node;
3165
3166 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
3167 if (!priv)
3168 return -ENOMEM;
3169
Vivien Didelot7e99e342019-10-21 16:51:30 -04003170 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
Sean Wangb8f126a2017-04-07 16:45:09 +08003171 if (!priv->ds)
3172 return -ENOMEM;
3173
Vivien Didelot7e99e342019-10-21 16:51:30 -04003174 priv->ds->dev = &mdiodev->dev;
3175 priv->ds->num_ports = DSA_MAX_PORTS;
3176
Sean Wangb8f126a2017-04-07 16:45:09 +08003177 /* Use medatek,mcm property to distinguish hardware type that would
3178 * casues a little bit differences on power-on sequence.
3179 */
3180 priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
3181 if (priv->mcm) {
3182 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
3183
3184 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
3185 if (IS_ERR(priv->rstc)) {
3186 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3187 return PTR_ERR(priv->rstc);
3188 }
3189 }
3190
Greg Ungererddda1ac2019-01-30 11:24:05 +10003191 /* Get the hardware identifier from the devicetree node.
3192 * We will need it for some of the clock and regulator setup.
3193 */
Landen Chao88bdef82020-09-11 21:48:52 +08003194 priv->info = of_device_get_match_data(&mdiodev->dev);
3195 if (!priv->info)
3196 return -EINVAL;
3197
3198 /* Sanity check if these required device operations are filled
3199 * properly.
3200 */
3201 if (!priv->info->sw_setup || !priv->info->pad_setup ||
3202 !priv->info->phy_read || !priv->info->phy_write ||
3203 !priv->info->phy_mode_supported ||
3204 !priv->info->mac_port_validate ||
3205 !priv->info->mac_port_get_state || !priv->info->mac_port_config)
3206 return -EINVAL;
3207
3208 priv->id = priv->info->id;
Sean Wangb8f126a2017-04-07 16:45:09 +08003209
Greg Ungererddda1ac2019-01-30 11:24:05 +10003210 if (priv->id == ID_MT7530) {
3211 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
3212 if (IS_ERR(priv->core_pwr))
3213 return PTR_ERR(priv->core_pwr);
3214
3215 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
3216 if (IS_ERR(priv->io_pwr))
3217 return PTR_ERR(priv->io_pwr);
3218 }
Sean Wangb8f126a2017-04-07 16:45:09 +08003219
3220 /* Not MCM that indicates switch works as the remote standalone
3221 * integrated circuit so the GPIO pin would be used to complete
3222 * the reset, otherwise memory-mapped register accessing used
3223 * through syscon provides in the case of MCM.
3224 */
3225 if (!priv->mcm) {
3226 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
3227 GPIOD_OUT_LOW);
3228 if (IS_ERR(priv->reset)) {
3229 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
3230 return PTR_ERR(priv->reset);
3231 }
3232 }
3233
3234 priv->bus = mdiodev->bus;
3235 priv->dev = &mdiodev->dev;
3236 priv->ds->priv = priv;
3237 priv->ds->ops = &mt7530_switch_ops;
3238 mutex_init(&priv->reg_mutex);
3239 dev_set_drvdata(&mdiodev->dev, priv);
3240
Vivien Didelot23c9ee42017-05-26 18:12:51 -04003241 return dsa_register_switch(priv->ds);
Sean Wangb8f126a2017-04-07 16:45:09 +08003242}
3243
3244static void
3245mt7530_remove(struct mdio_device *mdiodev)
3246{
3247 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
3248 int ret = 0;
3249
3250 ret = regulator_disable(priv->core_pwr);
3251 if (ret < 0)
3252 dev_err(priv->dev,
3253 "Failed to disable core power: %d\n", ret);
3254
3255 ret = regulator_disable(priv->io_pwr);
3256 if (ret < 0)
3257 dev_err(priv->dev, "Failed to disable io pwr: %d\n",
3258 ret);
3259
DENG Qingfangba751e22021-05-19 11:32:00 +08003260 if (priv->irq)
3261 mt7530_free_irq(priv);
3262
Sean Wangb8f126a2017-04-07 16:45:09 +08003263 dsa_unregister_switch(priv->ds);
3264 mutex_destroy(&priv->reg_mutex);
3265}
3266
Sean Wangb8f126a2017-04-07 16:45:09 +08003267static struct mdio_driver mt7530_mdio_driver = {
3268 .probe = mt7530_probe,
3269 .remove = mt7530_remove,
3270 .mdiodrv.driver = {
3271 .name = "mt7530",
3272 .of_match_table = mt7530_of_match,
3273 },
3274};
3275
3276mdio_module_driver(mt7530_mdio_driver);
3277
3278MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
3279MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
3280MODULE_LICENSE("GPL");