6c101377c30f72f7560209e9988392648404bf56
[openwrt/staging/wigyori.git] / target / linux / mediatek / patches-4.9 / 0092-dsa5.patch
1 From patchwork Wed Mar 29 09:38:23 2017
2 Content-Type: text/plain; charset="utf-8"
3 MIME-Version: 1.0
4 Content-Transfer-Encoding: 7bit
5 Subject: [net-next, v3,
6 5/5] net-next: dsa: add dsa support for Mediatek MT7530 switch
7 From: sean.wang@mediatek.com
8 X-Patchwork-Id: 9651095
9 Message-Id: <1490780303-18598-6-git-send-email-sean.wang@mediatek.com>
10 To: <andrew@lunn.ch>, <f.fainelli@gmail.com>,
11 <vivien.didelot@savoirfairelinux.com>, <matthias.bgg@gmail.com>,
12 <robh+dt@kernel.org>, <mark.rutland@arm.com>
13 Cc: devicetree@vger.kernel.org, Landen.Chao@mediatek.com, keyhaede@gmail.com,
14 netdev@vger.kernel.org, sean.wang@mediatek.com,
15 linux-kernel@vger.kernel.org,
16 linux-mediatek@lists.infradead.org, objelf@gmail.com, davem@davemloft.net
17 Date: Wed, 29 Mar 2017 17:38:23 +0800
18
19 From: Sean Wang <sean.wang@mediatek.com>
20
21 MT7530 is a 7-ports Gigabit Ethernet Switch that could be found on
22 Mediatek router platforms such as MT7623A or MT7623N platform which
23 includes 7-port Gigabit Ethernet MAC and 5-port Gigabit Ethernet PHY.
24 Among these ports, The port from 0 to 4 are the user ports connecting
25 with the remote devices while the port 5 and 6 are the CPU ports
26 connecting into Mediatek Ethernet GMAC.
27
28 For port 6, it can communicate with the CPU via Mediatek Ethernet GMAC
29 through either the TRGMII or RGMII which could be controlled by phy-mode
30 in the dt-bindings to specify which mode is preferred to use. And for
31 port 5, only RGMII can be specified. However, currently, only port 6 is
32 being supported in this DSA driver.
33
34 The driver is made with the reference to qca8k and other existing DSA
35 driver. The most of the essential callbacks of the DSA are already
36 support in the driver, including tag insert for user port distinguishing,
37 port control, bridge offloading, STP setup and ethtool operation to allow
38 DSA to model each user port into a standalone netdevice as the other DSA
39 driver had done.
40
41 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
42 Signed-off-by: Landen Chao <Landen.Chao@mediatek.com>
43 ---
44 drivers/net/dsa/Kconfig | 8 +
45 drivers/net/dsa/Makefile | 2 +-
46 drivers/net/dsa/mt7530.c | 1126 ++++++++++++++++++++++++++++++++++++++++++++++
47 drivers/net/dsa/mt7530.h | 390 ++++++++++++++++
48 4 files changed, 1525 insertions(+), 1 deletion(-)
49 create mode 100644 drivers/net/dsa/mt7530.c
50 create mode 100644 drivers/net/dsa/mt7530.h
51
52 diff --git a/drivers/net/dsa/Kconfig b/drivers/net/dsa/Kconfig
53 index 0659846..5b322b4 100644
54 --- a/drivers/net/dsa/Kconfig
55 +++ b/drivers/net/dsa/Kconfig
56 @@ -34,4 +34,12 @@ config NET_DSA_QCA8K
57 This enables support for the Qualcomm Atheros QCA8K Ethernet
58 switch chips.
59
60 +config NET_DSA_MT7530
61 + tristate "Mediatek MT7530 Ethernet switch support"
62 + depends on NET_DSA
63 + select NET_DSA_TAG_MTK
64 + ---help---
65 + This enables support for the Mediatek MT7530 Ethernet switch
66 + chip.
67 +
68 endmenu
69 diff --git a/drivers/net/dsa/Makefile b/drivers/net/dsa/Makefile
70 index a3c9416..8e629c1 100644
71 --- a/drivers/net/dsa/Makefile
72 +++ b/drivers/net/dsa/Makefile
73 @@ -2,6 +2,6 @@ obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
74 obj-$(CONFIG_NET_DSA_BCM_SF2) += bcm-sf2.o
75 bcm-sf2-objs := bcm_sf2.o bcm_sf2_cfp.o
76 obj-$(CONFIG_NET_DSA_QCA8K) += qca8k.o
77 -
78 +obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o
79 obj-y += b53/
80 obj-y += mv88e6xxx/
81 diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
82 new file mode 100644
83 index 0000000..ad2e6f8
84 --- /dev/null
85 +++ b/drivers/net/dsa/mt7530.c
86 @@ -0,0 +1,1126 @@
87 +/*
88 + * Mediatek MT7530 DSA Switch driver
89 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
90 + *
91 + * This program is free software; you can redistribute it and/or modify
92 + * it under the terms of the GNU General Public License version 2 as
93 + * published by the Free Software Foundation.
94 + *
95 + * This program is distributed in the hope that it will be useful,
96 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
97 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 + * GNU General Public License for more details.
99 + */
100 +#include <linux/etherdevice.h>
101 +#include <linux/if_bridge.h>
102 +#include <linux/iopoll.h>
103 +#include <linux/mdio.h>
104 +#include <linux/mfd/syscon.h>
105 +#include <linux/module.h>
106 +#include <linux/netdevice.h>
107 +#include <linux/of_gpio.h>
108 +#include <linux/of_mdio.h>
109 +#include <linux/of_net.h>
110 +#include <linux/of_platform.h>
111 +#include <linux/phy.h>
112 +#include <linux/regmap.h>
113 +#include <linux/regulator/consumer.h>
114 +#include <linux/reset.h>
115 +#include <net/dsa.h>
116 +#include <net/switchdev.h>
117 +
118 +#include "mt7530.h"
119 +
120 +/* String, offset, and register size in bytes if different from 4 bytes */
121 +static const struct mt7530_mib_desc mt7530_mib[] = {
122 + MIB_DESC(1, 0x00, "TxDrop"),
123 + MIB_DESC(1, 0x04, "TxCrcErr"),
124 + MIB_DESC(1, 0x08, "TxUnicast"),
125 + MIB_DESC(1, 0x0c, "TxMulticast"),
126 + MIB_DESC(1, 0x10, "TxBroadcast"),
127 + MIB_DESC(1, 0x14, "TxCollision"),
128 + MIB_DESC(1, 0x18, "TxSingleCollision"),
129 + MIB_DESC(1, 0x1c, "TxMultipleCollision"),
130 + MIB_DESC(1, 0x20, "TxDeferred"),
131 + MIB_DESC(1, 0x24, "TxLateCollision"),
132 + MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
133 + MIB_DESC(1, 0x2c, "TxPause"),
134 + MIB_DESC(1, 0x30, "TxPktSz64"),
135 + MIB_DESC(1, 0x34, "TxPktSz65To127"),
136 + MIB_DESC(1, 0x38, "TxPktSz128To255"),
137 + MIB_DESC(1, 0x3c, "TxPktSz256To511"),
138 + MIB_DESC(1, 0x40, "TxPktSz512To1023"),
139 + MIB_DESC(1, 0x44, "Tx1024ToMax"),
140 + MIB_DESC(2, 0x48, "TxBytes"),
141 + MIB_DESC(1, 0x60, "RxDrop"),
142 + MIB_DESC(1, 0x64, "RxFiltering"),
143 + MIB_DESC(1, 0x6c, "RxMulticast"),
144 + MIB_DESC(1, 0x70, "RxBroadcast"),
145 + MIB_DESC(1, 0x74, "RxAlignErr"),
146 + MIB_DESC(1, 0x78, "RxCrcErr"),
147 + MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
148 + MIB_DESC(1, 0x80, "RxFragErr"),
149 + MIB_DESC(1, 0x84, "RxOverSzErr"),
150 + MIB_DESC(1, 0x88, "RxJabberErr"),
151 + MIB_DESC(1, 0x8c, "RxPause"),
152 + MIB_DESC(1, 0x90, "RxPktSz64"),
153 + MIB_DESC(1, 0x94, "RxPktSz65To127"),
154 + MIB_DESC(1, 0x98, "RxPktSz128To255"),
155 + MIB_DESC(1, 0x9c, "RxPktSz256To511"),
156 + MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
157 + MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
158 + MIB_DESC(2, 0xa8, "RxBytes"),
159 + MIB_DESC(1, 0xb0, "RxCtrlDrop"),
160 + MIB_DESC(1, 0xb4, "RxIngressDrop"),
161 + MIB_DESC(1, 0xb8, "RxArlDrop"),
162 +};
163 +
164 +static struct mt7530_priv *lpriv;
165 +static void mt7530_port_disable(struct dsa_switch *ds, int port,
166 + struct phy_device *phy);
167 +static int mt7530_cpu_port_enable(struct mt7530_priv *priv,
168 + int port);
169 +
170 +static int
171 +mt7623_trgmii_write(struct mt7530_priv *priv, u32 reg, u32 val)
172 +{
173 + int ret;
174 +
175 + ret = regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
176 + if (ret < 0)
177 + dev_err(priv->dev,
178 + "failed to priv write register\n");
179 + return ret;
180 +}
181 +
182 +static u32
183 +mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
184 +{
185 + int ret;
186 + u32 val;
187 +
188 + ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
189 + if (ret < 0) {
190 + dev_err(priv->dev,
191 + "failed to priv read register\n");
192 + return ret;
193 + }
194 +
195 + return val;
196 +}
197 +
198 +static void
199 +mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
200 + u32 mask, u32 set)
201 +{
202 + u32 val;
203 +
204 + val = mt7623_trgmii_read(priv, reg);
205 + val &= ~mask;
206 + val |= set;
207 + mt7623_trgmii_write(priv, reg, val);
208 +}
209 +
210 +static void
211 +mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
212 +{
213 + mt7623_trgmii_rmw(priv, reg, 0, val);
214 +}
215 +
216 +static void
217 +mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
218 +{
219 + mt7623_trgmii_rmw(priv, reg, val, 0);
220 +}
221 +
222 +static int
223 +core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
224 +{
225 + struct mii_bus *bus = priv->bus;
226 + int value, ret;
227 +
228 + /* Write the desired MMD Devad */
229 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
230 + if (ret < 0)
231 + goto err;
232 +
233 + /* Write the desired MMD register address */
234 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
235 + if (ret < 0)
236 + goto err;
237 +
238 + /* Select the Function : DATA with no post increment */
239 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
240 + if (ret < 0)
241 + goto err;
242 +
243 + /* Read the content of the MMD's selected register */
244 + value = bus->read(bus, 0, MII_MMD_DATA);
245 +
246 + return value;
247 +err:
248 + dev_err(&bus->dev, "failed to read mmd register\n");
249 +
250 + return ret;
251 +}
252 +
253 +static int
254 +core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
255 + int devad, u32 data)
256 +{
257 + struct mii_bus *bus = priv->bus;
258 + int ret;
259 +
260 + /* Write the desired MMD Devad */
261 + ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
262 + if (ret < 0)
263 + goto err;
264 +
265 + /* Write the desired MMD register address */
266 + ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
267 + if (ret < 0)
268 + goto err;
269 +
270 + /* Select the Function : DATA with no post increment */
271 + ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
272 + if (ret < 0)
273 + goto err;
274 +
275 + /* Write the data into MMD's selected register */
276 + ret = bus->write(bus, 0, MII_MMD_DATA, data);
277 +err:
278 + if (ret < 0)
279 + dev_err(&bus->dev,
280 + "failed to write mmd register\n");
281 + return ret;
282 +}
283 +
284 +static void
285 +core_write(struct mt7530_priv *priv, u32 reg, u32 val)
286 +{
287 + struct mii_bus *bus = priv->bus;
288 +
289 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
290 +
291 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
292 +
293 + mutex_unlock(&bus->mdio_lock);
294 +}
295 +
296 +static void
297 +core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
298 +{
299 + struct mii_bus *bus = priv->bus;
300 + u32 val;
301 +
302 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
303 +
304 + val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
305 + val &= ~mask;
306 + val |= set;
307 + core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
308 +
309 + mutex_unlock(&bus->mdio_lock);
310 +}
311 +
312 +static void
313 +core_set(struct mt7530_priv *priv, u32 reg, u32 val)
314 +{
315 + core_rmw(priv, reg, 0, val);
316 +}
317 +
318 +static void
319 +core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
320 +{
321 + core_rmw(priv, reg, val, 0);
322 +}
323 +
324 +static int
325 +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
326 +{
327 + struct mii_bus *bus = priv->bus;
328 + u16 page, r, lo, hi;
329 + int ret;
330 +
331 + page = (reg >> 6) & 0x3ff;
332 + r = (reg >> 2) & 0xf;
333 + lo = val & 0xffff;
334 + hi = val >> 16;
335 +
336 + /* MT7530 uses 31 as the pseudo port */
337 + ret = bus->write(bus, 0x1f, 0x1f, page);
338 + if (ret < 0)
339 + goto err;
340 +
341 + ret = bus->write(bus, 0x1f, r, lo);
342 + if (ret < 0)
343 + goto err;
344 +
345 + ret = bus->write(bus, 0x1f, 0x10, hi);
346 +err:
347 + if (ret < 0)
348 + dev_err(&bus->dev,
349 + "failed to write mt7530 register\n");
350 + return ret;
351 +}
352 +
353 +static u32
354 +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
355 +{
356 + struct mii_bus *bus = priv->bus;
357 + u16 page, r, lo, hi;
358 + int ret;
359 +
360 + page = (reg >> 6) & 0x3ff;
361 + r = (reg >> 2) & 0xf;
362 +
363 + /* MT7530 uses 31 as the pseudo port */
364 + ret = bus->write(bus, 0x1f, 0x1f, page);
365 + if (ret < 0) {
366 + dev_err(&bus->dev,
367 + "failed to read mt7530 register\n");
368 + return ret;
369 + }
370 +
371 + lo = bus->read(bus, 0x1f, r);
372 + hi = bus->read(bus, 0x1f, 0x10);
373 +
374 + return (hi << 16) | (lo & 0xffff);
375 +}
376 +
377 +static void
378 +mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
379 +{
380 + struct mii_bus *bus = priv->bus;
381 +
382 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
383 +
384 + mt7530_mii_write(priv, reg, val);
385 +
386 + mutex_unlock(&bus->mdio_lock);
387 +}
388 +
389 +static u32
390 +_mt7530_read(u32 reg)
391 +{
392 + struct mt7530_priv *priv = lpriv;
393 + struct mii_bus *bus = priv->bus;
394 + u32 val;
395 +
396 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
397 +
398 + val = mt7530_mii_read(priv, reg);
399 +
400 + mutex_unlock(&bus->mdio_lock);
401 +
402 + return val;
403 +}
404 +
405 +static u32
406 +mt7530_read(struct mt7530_priv *priv, u32 reg)
407 +{
408 + return _mt7530_read(reg);
409 +}
410 +
411 +static void
412 +mt7530_rmw(struct mt7530_priv *priv, u32 reg,
413 + u32 mask, u32 set)
414 +{
415 + struct mii_bus *bus = priv->bus;
416 + u32 val;
417 +
418 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
419 +
420 + val = mt7530_mii_read(priv, reg);
421 + val &= ~mask;
422 + val |= set;
423 + mt7530_mii_write(priv, reg, val);
424 +
425 + mutex_unlock(&bus->mdio_lock);
426 +}
427 +
428 +static void
429 +mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
430 +{
431 + mt7530_rmw(priv, reg, 0, val);
432 +}
433 +
434 +static void
435 +mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
436 +{
437 + mt7530_rmw(priv, reg, val, 0);
438 +}
439 +
440 +static int
441 +mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
442 +{
443 + u32 val;
444 + int ret;
445 +
446 + /* Set the command operating upon the MAC address entries */
447 + val = ATC_BUSY | ATC_MAT(0) | cmd;
448 + mt7530_write(priv, MT7530_ATC, val);
449 +
450 + ret = readx_poll_timeout(_mt7530_read, MT7530_ATC, val,
451 + !(val & ATC_BUSY), 20, 20000);
452 + if (ret < 0) {
453 + dev_err(priv->dev, "reset timeout\n");
454 + return ret;
455 + }
456 +
457 + /* Additional sanity for read command if the specified
458 + * entry is invalid
459 + */
460 + val = mt7530_read(priv, MT7530_ATC);
461 + if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
462 + return -EINVAL;
463 +
464 + if (rsp)
465 + *rsp = val;
466 +
467 + return 0;
468 +}
469 +
470 +static void
471 +mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
472 +{
473 + u32 reg[3];
474 + int i;
475 +
476 + /* Read from ARL table into an array */
477 + for (i = 0; i < 3; i++) {
478 + reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
479 +
480 + dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
481 + __func__, __LINE__, i, reg[i]);
482 + }
483 +
484 + fdb->vid = (reg[1] >> CVID) & CVID_MASK;
485 + fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
486 + fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
487 + fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
488 + fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
489 + fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
490 + fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
491 + fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
492 + fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
493 + fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
494 +}
495 +
496 +static void
497 +mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
498 + u8 port_mask, const u8 *mac,
499 + u8 aging, u8 type)
500 +{
501 + u32 reg[3] = { 0 };
502 + int i;
503 +
504 + reg[1] |= vid & CVID_MASK;
505 + reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
506 + reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
507 + /* STATIC_ENT indicate that entry is static wouldn't
508 + * be aged out and STATIC_EMP specified as erasing an
509 + * entry
510 + */
511 + reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
512 + reg[1] |= mac[5] << MAC_BYTE_5;
513 + reg[1] |= mac[4] << MAC_BYTE_4;
514 + reg[0] |= mac[3] << MAC_BYTE_3;
515 + reg[0] |= mac[2] << MAC_BYTE_2;
516 + reg[0] |= mac[1] << MAC_BYTE_1;
517 + reg[0] |= mac[0] << MAC_BYTE_0;
518 +
519 + /* Write array into the ARL table */
520 + for (i = 0; i < 3; i++)
521 + mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
522 +}
523 +
524 +static int
525 +mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
526 +{
527 + struct mt7530_priv *priv = ds->priv;
528 + u32 ncpo1, ssc_delta, trgint, i;
529 +
530 + switch (mode) {
531 + case PHY_INTERFACE_MODE_RGMII:
532 + trgint = 0;
533 + ncpo1 = 0x0c80;
534 + ssc_delta = 0x87;
535 + break;
536 + case PHY_INTERFACE_MODE_TRGMII:
537 + trgint = 1;
538 + ncpo1 = 0x1400;
539 + ssc_delta = 0x57;
540 + break;
541 + default:
542 + dev_err(priv->dev, "xMII mode %d not supported\n", mode);
543 + return -EINVAL;
544 + }
545 +
546 + mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
547 + P6_INTF_MODE(trgint));
548 +
549 + /* Lower Tx Driving for TRGMII path */
550 + for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
551 + mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
552 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
553 +
554 + /* Setup core clock for MT7530 */
555 + if (!trgint) {
556 + /* Disable MT7530 core clock */
557 + core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
558 +
559 + /* Disable PLL, since phy_device has not yet been created
560 + * provided for phy_[read,write]_mmd_indirect is called, we
561 + * provide our own core_write_mmd_indirect to complete this
562 + * function.
563 + */
564 + core_write_mmd_indirect(priv,
565 + CORE_GSWPLL_GRP1,
566 + MDIO_MMD_VEND2,
567 + 0);
568 +
569 + /* Set core clock into 500Mhz */
570 + core_write(priv, CORE_GSWPLL_GRP2,
571 + RG_GSWPLL_POSDIV_500M(1) |
572 + RG_GSWPLL_FBKDIV_500M(25));
573 +
574 + /* Enable PLL */
575 + core_write(priv, CORE_GSWPLL_GRP1,
576 + RG_GSWPLL_EN_PRE |
577 + RG_GSWPLL_POSDIV_200M(2) |
578 + RG_GSWPLL_FBKDIV_200M(32));
579 +
580 + /* Enable MT7530 core clock */
581 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
582 + }
583 +
584 + /* Setup the MT7530 TRGMII Tx Clock */
585 + core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
586 + core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
587 + core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
588 + core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
589 + core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
590 + core_write(priv, CORE_PLL_GROUP4,
591 + RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
592 + RG_SYSPLL_BIAS_LPF_EN);
593 + core_write(priv, CORE_PLL_GROUP2,
594 + RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
595 + RG_SYSPLL_POSDIV(1));
596 + core_write(priv, CORE_PLL_GROUP7,
597 + RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
598 + RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
599 + core_set(priv, CORE_TRGMII_GSW_CLK_CG,
600 + REG_GSWCK_EN | REG_TRGMIICK_EN);
601 +
602 + if (!trgint)
603 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
604 + mt7530_rmw(priv, MT7530_TRGMII_RD(i),
605 + RD_TAP_MASK, RD_TAP(16));
606 + else
607 + mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
608 +
609 + return 0;
610 +}
611 +
612 +static int
613 +mt7623_pad_clk_setup(struct dsa_switch *ds)
614 +{
615 + struct mt7530_priv *priv = ds->priv;
616 + int i;
617 +
618 + for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
619 + mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
620 + TD_DM_DRVP(8) | TD_DM_DRVN(8));
621 +
622 + mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
623 + mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
624 +
625 + return 0;
626 +}
627 +
628 +static void
629 +mt7530_mib_reset(struct dsa_switch *ds)
630 +{
631 + struct mt7530_priv *priv = ds->priv;
632 +
633 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
634 + mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
635 +}
636 +
637 +static void
638 +mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
639 +{
640 + u32 mask = PMCR_TX_EN | PMCR_RX_EN;
641 +
642 + if (enable)
643 + mt7530_set(priv, MT7530_PMCR_P(port), mask);
644 + else
645 + mt7530_clear(priv, MT7530_PMCR_P(port), mask);
646 +}
647 +
648 +static int
649 +mt7530_setup(struct dsa_switch *ds)
650 +{
651 + struct mt7530_priv *priv = ds->priv;
652 + int ret, i;
653 + u32 id, val;
654 + struct device_node *dn;
655 +
656 + /* The parent node of master_netdev which holds the common system
657 + * controller also is the container for two GMACs nodes representing
658 + * as two netdev instances.
659 + */
660 + dn = ds->master_netdev->dev.of_node->parent;
661 + priv->ethernet = syscon_node_to_regmap(dn);
662 + if (IS_ERR(priv->ethernet))
663 + return PTR_ERR(priv->ethernet);
664 +
665 + regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
666 + ret = regulator_enable(priv->core_pwr);
667 + if (ret < 0) {
668 + dev_err(priv->dev,
669 + "Failed to enable core power: %d\n", ret);
670 + return ret;
671 + }
672 +
673 + regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
674 + ret = regulator_enable(priv->io_pwr);
675 + if (ret < 0) {
676 + dev_err(priv->dev, "Failed to enable io pwr: %d\n",
677 + ret);
678 + return ret;
679 + }
680 +
681 + /* Reset whole chip through gpio pin or memory-mapped registers for
682 + * different type of hardware
683 + */
684 + if (priv->mcm) {
685 + reset_control_assert(priv->rstc);
686 + usleep_range(1000, 1100);
687 + reset_control_deassert(priv->rstc);
688 + } else {
689 + gpiod_set_value_cansleep(priv->reset, 0);
690 + usleep_range(1000, 1100);
691 + gpiod_set_value_cansleep(priv->reset, 1);
692 + }
693 +
694 + /* Waiting for MT7530 got to stable */
695 + ret = readx_poll_timeout(_mt7530_read, MT7530_HWTRAP, val, val != 0,
696 + 20, 1000000);
697 + if (ret < 0) {
698 + dev_err(priv->dev, "reset timeout\n");
699 + return ret;
700 + }
701 +
702 + id = mt7530_read(priv, MT7530_CREV);
703 + id >>= CHIP_NAME_SHIFT;
704 + if (id != MT7530_ID) {
705 + dev_err(priv->dev, "chip %x can't be supported\n", id);
706 + return -ENODEV;
707 + }
708 +
709 + /* Reset the switch through internal reset */
710 + mt7530_write(priv, MT7530_SYS_CTRL,
711 + SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
712 + SYS_CTRL_REG_RST);
713 +
714 + /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
715 + val = mt7530_read(priv, MT7530_MHWTRAP);
716 + val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
717 + val |= MHWTRAP_MANUAL;
718 + mt7530_write(priv, MT7530_MHWTRAP, val);
719 +
720 + /* Enable and reset MIB counters */
721 + mt7530_mib_reset(ds);
722 +
723 + mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
724 +
725 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
726 + /* Disable forwarding by default on all ports */
727 + mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
728 + PCR_MATRIX_CLR);
729 +
730 + if (dsa_is_cpu_port(ds, i))
731 + mt7530_cpu_port_enable(priv, i);
732 + else
733 + mt7530_port_disable(ds, i, NULL);
734 + }
735 +
736 + /* Flush the FDB table */
737 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
738 + if (ret < 0)
739 + return ret;
740 +
741 + return 0;
742 +}
743 +
744 +static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
745 +{
746 + struct mt7530_priv *priv = ds->priv;
747 +
748 + return mdiobus_read_nested(priv->bus, port, regnum);
749 +}
750 +
751 +int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
752 +{
753 + struct mt7530_priv *priv = ds->priv;
754 +
755 + return mdiobus_write_nested(priv->bus, port, regnum, val);
756 +}
757 +
758 +static void
759 +mt7530_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
760 +{
761 + int i;
762 +
763 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
764 + strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
765 + ETH_GSTRING_LEN);
766 +}
767 +
768 +static void
769 +mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
770 + uint64_t *data)
771 +{
772 + struct mt7530_priv *priv = ds->priv;
773 + const struct mt7530_mib_desc *mib;
774 + u32 reg, i;
775 + u64 hi;
776 +
777 + for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
778 + mib = &mt7530_mib[i];
779 + reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
780 +
781 + data[i] = mt7530_read(priv, reg);
782 + if (mib->size == 2) {
783 + hi = mt7530_read(priv, reg + 4);
784 + data[i] |= hi << 32;
785 + }
786 + }
787 +}
788 +
789 +static int
790 +mt7530_get_sset_count(struct dsa_switch *ds)
791 +{
792 + return ARRAY_SIZE(mt7530_mib);
793 +}
794 +
795 +static void mt7530_adjust_link(struct dsa_switch *ds, int port,
796 + struct phy_device *phydev)
797 +{
798 + struct mt7530_priv *priv = ds->priv;
799 +
800 + if (phy_is_pseudo_fixed_link(phydev)) {
801 + dev_dbg(priv->dev, "phy-mode for master device = %x\n",
802 + phydev->interface);
803 +
804 + /* Setup TX circuit incluing relevant PAD and driving */
805 + mt7530_pad_clk_setup(ds, phydev->interface);
806 +
807 + /* Setup RX circuit, relevant PAD and driving on the host
808 + * which must be placed after the setup on the device side is
809 + * all finished.
810 + */
811 + mt7623_pad_clk_setup(ds);
812 + }
813 +}
814 +
815 +static int
816 +mt7530_cpu_port_enable(struct mt7530_priv *priv,
817 + int port)
818 +{
819 + /* Enable Mediatek header mode on the cpu port */
820 + mt7530_write(priv, MT7530_PVC_P(port),
821 + PORT_SPEC_TAG);
822 +
823 + /* Setup the MAC by default for the cpu port */
824 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
825 +
826 + /* Disable auto learning on the cpu port */
827 + mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
828 +
829 + /* Unknown unicast frame fordwarding to the cpu port */
830 + mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
831 +
832 + /* CPU port gets connected to all user ports of
833 + * the switch
834 + */
835 + mt7530_write(priv, MT7530_PCR_P(port),
836 + PCR_MATRIX(priv->ds->enabled_port_mask));
837 +
838 + return 0;
839 +}
840 +
841 +static int
842 +mt7530_port_enable(struct dsa_switch *ds, int port,
843 + struct phy_device *phy)
844 +{
845 + struct mt7530_priv *priv = ds->priv;
846 +
847 + mutex_lock(&priv->reg_mutex);
848 +
849 + /* Setup the MAC for the user port */
850 + mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
851 +
852 + /* Allow the user port gets connected to the cpu port and also
853 + * restore the port matrix if the port is the member of a certain
854 + * bridge.
855 + */
856 + priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
857 + priv->ports[port].enable = true;
858 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
859 + priv->ports[port].pm);
860 + mt7530_port_set_status(priv, port, 1);
861 +
862 + mutex_unlock(&priv->reg_mutex);
863 +
864 + return 0;
865 +}
866 +
867 +static void
868 +mt7530_port_disable(struct dsa_switch *ds, int port,
869 + struct phy_device *phy)
870 +{
871 + struct mt7530_priv *priv = ds->priv;
872 +
873 + mutex_lock(&priv->reg_mutex);
874 +
875 + /* Clear up all port matrix which could be restored in the next
876 + * enablement for the port.
877 + */
878 + priv->ports[port].enable = false;
879 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
880 + PCR_MATRIX_CLR);
881 + mt7530_port_set_status(priv, port, 0);
882 +
883 + mutex_unlock(&priv->reg_mutex);
884 +}
885 +
886 +static void
887 +mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
888 +{
889 + struct mt7530_priv *priv = ds->priv;
890 + u32 stp_state;
891 +
892 + switch (state) {
893 + case BR_STATE_DISABLED:
894 + stp_state = MT7530_STP_DISABLED;
895 + break;
896 + case BR_STATE_BLOCKING:
897 + stp_state = MT7530_STP_BLOCKING;
898 + break;
899 + case BR_STATE_LISTENING:
900 + stp_state = MT7530_STP_LISTENING;
901 + break;
902 + case BR_STATE_LEARNING:
903 + stp_state = MT7530_STP_LEARNING;
904 + break;
905 + case BR_STATE_FORWARDING:
906 + default:
907 + stp_state = MT7530_STP_FORWARDING;
908 + break;
909 + }
910 +
911 + mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
912 +}
913 +
914 +static int
915 +mt7530_port_bridge_join(struct dsa_switch *ds, int port,
916 + struct net_device *bridge)
917 +{
918 + struct mt7530_priv *priv = ds->priv;
919 + u32 port_bitmap = BIT(MT7530_CPU_PORT);
920 + int i;
921 +
922 + mutex_lock(&priv->reg_mutex);
923 +
924 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
925 + /* Add this port to the port matrix of the other ports in the
926 + * same bridge. If the port is disabled, port matrix is kept
927 + * and not being setup until the port becomes enabled.
928 + */
929 + if (ds->enabled_port_mask & BIT(i) && i != port) {
930 + if (ds->ports[i].bridge_dev != bridge)
931 + continue;
932 + if (priv->ports[i].enable)
933 + mt7530_set(priv, MT7530_PCR_P(i),
934 + PCR_MATRIX(BIT(port)));
935 + priv->ports[i].pm |= PCR_MATRIX(BIT(port));
936 +
937 + port_bitmap |= BIT(i);
938 + }
939 + }
940 +
941 + /* Add the all other ports to this port matrix. */
942 + if (priv->ports[port].enable)
943 + mt7530_rmw(priv, MT7530_PCR_P(port),
944 + PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
945 + priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
946 +
947 + mutex_unlock(&priv->reg_mutex);
948 +
949 + return 0;
950 +}
951 +
952 +static void
953 +mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
954 + struct net_device *bridge)
955 +{
956 + struct mt7530_priv *priv = ds->priv;
957 + int i;
958 +
959 + mutex_lock(&priv->reg_mutex);
960 +
961 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
962 + /* Remove this port from the port matrix of the other ports
963 + * in the same bridge. If the port is disabled, port matrix
964 + * is kept and not being setup until the port becomes enabled.
965 + */
966 + if (ds->enabled_port_mask & BIT(i) && i != port) {
967 + if (ds->ports[i].bridge_dev != bridge)
968 + continue;
969 + if (priv->ports[i].enable)
970 + mt7530_clear(priv, MT7530_PCR_P(i),
971 + PCR_MATRIX(BIT(port)));
972 + priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
973 + }
974 + }
975 +
976 + /* Set the cpu port to be the only one in the port matrix of
977 + * this port.
978 + */
979 + if (priv->ports[port].enable)
980 + mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
981 + PCR_MATRIX(BIT(MT7530_CPU_PORT)));
982 + priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
983 +
984 + mutex_unlock(&priv->reg_mutex);
985 +}
986 +
987 +static int
988 +mt7530_port_fdb_prepare(struct dsa_switch *ds, int port,
989 + const struct switchdev_obj_port_fdb *fdb,
990 + struct switchdev_trans *trans)
991 +{
992 + struct mt7530_priv *priv = ds->priv;
993 + int ret;
994 +
995 + /* Because auto-learned entrie shares the same FDB table.
996 + * an entry is reserved with no port_mask to make sure fdb_add
997 + * is called while the entry is still available.
998 + */
999 + mutex_lock(&priv->reg_mutex);
1000 + mt7530_fdb_write(priv, fdb->vid, 0, fdb->addr, -1, STATIC_ENT);
1001 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1002 + mutex_unlock(&priv->reg_mutex);
1003 +
1004 + return ret;
1005 +}
1006 +
1007 +static void
1008 +mt7530_port_fdb_add(struct dsa_switch *ds, int port,
1009 + const struct switchdev_obj_port_fdb *fdb,
1010 + struct switchdev_trans *trans)
1011 +{
1012 + struct mt7530_priv *priv = ds->priv;
1013 + u8 port_mask = BIT(port);
1014 +
1015 + mutex_lock(&priv->reg_mutex);
1016 + mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_ENT);
1017 + mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1018 + mutex_unlock(&priv->reg_mutex);
1019 +}
1020 +
1021 +static int
1022 +mt7530_port_fdb_del(struct dsa_switch *ds, int port,
1023 + const struct switchdev_obj_port_fdb *fdb)
1024 +{
1025 + struct mt7530_priv *priv = ds->priv;
1026 + int ret;
1027 + u8 port_mask = BIT(port);
1028 +
1029 + mutex_lock(&priv->reg_mutex);
1030 + mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_EMP);
1031 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
1032 + mutex_unlock(&priv->reg_mutex);
1033 +
1034 + return ret;
1035 +}
1036 +
1037 +static int
1038 +mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
1039 + struct switchdev_obj_port_fdb *fdb,
1040 + int (*cb)(struct switchdev_obj *obj))
1041 +{
1042 + struct mt7530_priv *priv = ds->priv;
1043 + struct mt7530_fdb _fdb = { 0 };
1044 + int cnt = MT7530_NUM_FDB_RECORDS;
1045 + int ret = 0;
1046 + u32 rsp = 0;
1047 +
1048 + mutex_lock(&priv->reg_mutex);
1049 +
1050 + ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
1051 + if (ret < 0)
1052 + goto err;
1053 +
1054 + do {
1055 + if (rsp & ATC_SRCH_HIT) {
1056 + mt7530_fdb_read(priv, &_fdb);
1057 + if (_fdb.port_mask & BIT(port)) {
1058 + ether_addr_copy(fdb->addr, _fdb.mac);
1059 + fdb->vid = _fdb.vid;
1060 + fdb->ndm_state = _fdb.noarp ?
1061 + NUD_NOARP : NUD_REACHABLE;
1062 + ret = cb(&fdb->obj);
1063 + if (ret < 0)
1064 + break;
1065 + }
1066 + }
1067 + } while (--cnt &&
1068 + !(rsp & ATC_SRCH_END) &&
1069 + !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
1070 +err:
1071 + mutex_unlock(&priv->reg_mutex);
1072 +
1073 + return 0;
1074 +}
1075 +
1076 +static enum dsa_tag_protocol
1077 +mtk_get_tag_protocol(struct dsa_switch *ds)
1078 +{
1079 + struct mt7530_priv *priv = ds->priv;
1080 +
1081 + if (!dsa_is_cpu_port(ds, MT7530_CPU_PORT)) {
1082 + dev_warn(priv->dev,
1083 + "port not matched with tagging CPU port\n");
1084 + return DSA_TAG_PROTO_NONE;
1085 + } else {
1086 + return DSA_TAG_PROTO_MTK;
1087 + }
1088 +}
1089 +
1090 +static struct dsa_switch_ops mt7530_switch_ops = {
1091 + .get_tag_protocol = mtk_get_tag_protocol,
1092 + .setup = mt7530_setup,
1093 + .get_strings = mt7530_get_strings,
1094 + .phy_read = mt7530_phy_read,
1095 + .phy_write = mt7530_phy_write,
1096 + .get_ethtool_stats = mt7530_get_ethtool_stats,
1097 + .get_sset_count = mt7530_get_sset_count,
1098 + .adjust_link = mt7530_adjust_link,
1099 + .port_enable = mt7530_port_enable,
1100 + .port_disable = mt7530_port_disable,
1101 + .port_stp_state_set = mt7530_stp_state_set,
1102 + .port_bridge_join = mt7530_port_bridge_join,
1103 + .port_bridge_leave = mt7530_port_bridge_leave,
1104 + .port_fdb_prepare = mt7530_port_fdb_prepare,
1105 + .port_fdb_add = mt7530_port_fdb_add,
1106 + .port_fdb_del = mt7530_port_fdb_del,
1107 + .port_fdb_dump = mt7530_port_fdb_dump,
1108 +};
1109 +
1110 +static int
1111 +mt7530_probe(struct mdio_device *mdiodev)
1112 +{
1113 + struct mt7530_priv *priv;
1114 + struct device_node *dn;
1115 +
1116 + dn = mdiodev->dev.of_node;
1117 +
1118 + priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1119 + if (!priv)
1120 + return -ENOMEM;
1121 +
1122 + priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
1123 + if (!priv->ds)
1124 + return -ENOMEM;
1125 +
1126 + /* Use medatek,mcm property to distinguish hardware type that would
1127 + * casues a little bit differences on power-on sequence.
1128 + */
1129 + priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
1130 + if (priv->mcm) {
1131 + dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
1132 +
1133 + priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
1134 + if (IS_ERR(priv->rstc)) {
1135 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1136 + return PTR_ERR(priv->rstc);
1137 + }
1138 + }
1139 +
1140 + priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
1141 + if (IS_ERR(priv->core_pwr))
1142 + return PTR_ERR(priv->core_pwr);
1143 +
1144 + priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
1145 + if (IS_ERR(priv->io_pwr))
1146 + return PTR_ERR(priv->io_pwr);
1147 +
1148 + /* Not MCM that indicates switch works as the remote standalone
1149 + * integrated circuit so the GPIO pin would be used to complete
1150 + * the reset, otherwise memory-mapped register accessing used
1151 + * through syscon provides in the case of MCM.
1152 + */
1153 + if (!priv->mcm) {
1154 + priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
1155 + GPIOD_OUT_LOW);
1156 + if (IS_ERR(priv->reset)) {
1157 + dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
1158 + return PTR_ERR(priv->reset);
1159 + }
1160 + }
1161 +
1162 + priv->bus = mdiodev->bus;
1163 + priv->dev = &mdiodev->dev;
1164 + priv->ds->priv = priv;
1165 + priv->ds->ops = &mt7530_switch_ops;
1166 + mutex_init(&priv->reg_mutex);
1167 + lpriv = priv;
1168 + dev_set_drvdata(&mdiodev->dev, priv);
1169 +
1170 + return dsa_register_switch(priv->ds, &mdiodev->dev);
1171 +}
1172 +
1173 +static void
1174 +mt7530_remove(struct mdio_device *mdiodev)
1175 +{
1176 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
1177 + int ret = 0;
1178 +
1179 + ret = regulator_disable(priv->core_pwr);
1180 + if (ret < 0)
1181 + dev_err(priv->dev,
1182 + "Failed to disable core power: %d\n", ret);
1183 +
1184 + ret = regulator_disable(priv->io_pwr);
1185 + if (ret < 0)
1186 + dev_err(priv->dev, "Failed to disable io pwr: %d\n",
1187 + ret);
1188 +
1189 + dsa_unregister_switch(priv->ds);
1190 + mutex_destroy(&priv->reg_mutex);
1191 +}
1192 +
1193 +static const struct of_device_id mt7530_of_match[] = {
1194 + { .compatible = "mediatek,mt7530" },
1195 + { /* sentinel */ },
1196 +};
1197 +
1198 +static struct mdio_driver mt7530_mdio_driver = {
1199 + .probe = mt7530_probe,
1200 + .remove = mt7530_remove,
1201 + .mdiodrv.driver = {
1202 + .name = "mt7530",
1203 + .of_match_table = mt7530_of_match,
1204 + },
1205 +};
1206 +
1207 +mdio_module_driver(mt7530_mdio_driver);
1208 +
1209 +MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1210 +MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
1211 +MODULE_LICENSE("GPL");
1212 +MODULE_ALIAS("platform:mediatek-mt7530");
1213 diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
1214 new file mode 100644
1215 index 0000000..05a612f
1216 --- /dev/null
1217 +++ b/drivers/net/dsa/mt7530.h
1218 @@ -0,0 +1,390 @@
1219 +/*
1220 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
1221 + *
1222 + * This program is free software; you can redistribute it and/or modify
1223 + * it under the terms of the GNU General Public License version 2 as
1224 + * published by the Free Software Foundation.
1225 + *
1226 + * This program is distributed in the hope that it will be useful,
1227 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1228 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1229 + * GNU General Public License for more details.
1230 + */
1231 +
1232 +#ifndef __MT7530_H
1233 +#define __MT7530_H
1234 +
1235 +#define MT7530_NUM_PORTS 7
1236 +#define MT7530_CPU_PORT 6
1237 +#define MT7530_NUM_FDB_RECORDS 2048
1238 +
1239 +#define NUM_TRGMII_CTRL 5
1240 +
1241 +#define TRGMII_BASE(x) (0x10000 + (x))
1242 +
1243 +/* Registers to ethsys access */
1244 +#define ETHSYS_CLKCFG0 0x2c
1245 +#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
1246 +
1247 +#define SYSC_REG_RSTCTRL 0x34
1248 +#define RESET_MCM BIT(2)
1249 +
1250 +/* Registers to mac forward control for unknown frames */
1251 +#define MT7530_MFC 0x10
1252 +#define BC_FFP(x) (((x) & 0xff) << 24)
1253 +#define UNM_FFP(x) (((x) & 0xff) << 16)
1254 +#define UNU_FFP(x) (((x) & 0xff) << 8)
1255 +#define UNU_FFP_MASK UNU_FFP(~0)
1256 +
1257 +/* Registers for address table access */
1258 +#define MT7530_ATA1 0x74
1259 +#define STATIC_EMP 0
1260 +#define STATIC_ENT 3
1261 +#define MT7530_ATA2 0x78
1262 +
1263 +/* Register for address table write data */
1264 +#define MT7530_ATWD 0x7c
1265 +
1266 +/* Register for address table control */
1267 +#define MT7530_ATC 0x80
1268 +#define ATC_HASH (((x) & 0xfff) << 16)
1269 +#define ATC_BUSY BIT(15)
1270 +#define ATC_SRCH_END BIT(14)
1271 +#define ATC_SRCH_HIT BIT(13)
1272 +#define ATC_INVALID BIT(12)
1273 +#define ATC_MAT(x) (((x) & 0xf) << 8)
1274 +#define ATC_MAT_MACTAB ATC_MAT(0)
1275 +
1276 +enum mt7530_fdb_cmd {
1277 + MT7530_FDB_READ = 0,
1278 + MT7530_FDB_WRITE = 1,
1279 + MT7530_FDB_FLUSH = 2,
1280 + MT7530_FDB_START = 4,
1281 + MT7530_FDB_NEXT = 5,
1282 +};
1283 +
1284 +/* Registers for table search read address */
1285 +#define MT7530_TSRA1 0x84
1286 +#define MAC_BYTE_0 24
1287 +#define MAC_BYTE_1 16
1288 +#define MAC_BYTE_2 8
1289 +#define MAC_BYTE_3 0
1290 +#define MAC_BYTE_MASK 0xff
1291 +
1292 +#define MT7530_TSRA2 0x88
1293 +#define MAC_BYTE_4 24
1294 +#define MAC_BYTE_5 16
1295 +#define CVID 0
1296 +#define CVID_MASK 0xfff
1297 +
1298 +#define MT7530_ATRD 0x8C
1299 +#define AGE_TIMER 24
1300 +#define AGE_TIMER_MASK 0xff
1301 +#define PORT_MAP 4
1302 +#define PORT_MAP_MASK 0xff
1303 +#define ENT_STATUS 2
1304 +#define ENT_STATUS_MASK 0x3
1305 +
1306 +/* Register for vlan table control */
1307 +#define MT7530_VTCR 0x90
1308 +#define VTCR_BUSY BIT(31)
1309 +#define VTCR_FUNC (((x) & 0xf) << 12)
1310 +#define VTCR_FUNC_RD_VID 0x1
1311 +#define VTCR_FUNC_WR_VID 0x2
1312 +#define VTCR_FUNC_INV_VID 0x3
1313 +#define VTCR_FUNC_VAL_VID 0x4
1314 +#define VTCR_VID ((x) & 0xfff)
1315 +
1316 +/* Register for setup vlan and acl write data */
1317 +#define MT7530_VAWD1 0x94
1318 +#define PORT_STAG BIT(31)
1319 +#define IVL_MAC BIT(30)
1320 +#define PORT_MEM(x) (((x) & 0xff) << 16)
1321 +#define VALID BIT(1)
1322 +
1323 +#define MT7530_VAWD2 0x98
1324 +
1325 +/* Register for port STP state control */
1326 +#define MT7530_SSP_P(x) (0x2000 + ((x) * 0x100))
1327 +#define FID_PST(x) ((x) & 0x3)
1328 +#define FID_PST_MASK FID_PST(0x3)
1329 +
1330 +enum mt7530_stp_state {
1331 + MT7530_STP_DISABLED = 0,
1332 + MT7530_STP_BLOCKING = 1,
1333 + MT7530_STP_LISTENING = 1,
1334 + MT7530_STP_LEARNING = 2,
1335 + MT7530_STP_FORWARDING = 3
1336 +};
1337 +
1338 +/* Register for port control */
1339 +#define MT7530_PCR_P(x) (0x2004 + ((x) * 0x100))
1340 +#define PORT_VLAN(x) ((x) & 0x3)
1341 +#define PCR_MATRIX(x) (((x) & 0xff) << 16)
1342 +#define PORT_PRI(x) (((x) & 0x7) << 24)
1343 +#define EG_TAG(x) (((x) & 0x3) << 28)
1344 +#define PCR_MATRIX_MASK PCR_MATRIX(0xff)
1345 +#define PCR_MATRIX_CLR PCR_MATRIX(0)
1346 +
1347 +/* Register for port security control */
1348 +#define MT7530_PSC_P(x) (0x200c + ((x) * 0x100))
1349 +#define SA_DIS BIT(4)
1350 +
1351 +/* Register for port vlan control */
1352 +#define MT7530_PVC_P(x) (0x2010 + ((x) * 0x100))
1353 +#define PORT_SPEC_TAG BIT(5)
1354 +#define VLAN_ATTR(x) (((x) & 0x3) << 6)
1355 +#define STAG_VPID (((x) & 0xffff) << 16)
1356 +
1357 +/* Register for port port-and-protocol based vlan 1 control */
1358 +#define MT7530_PPBV1_P(x) (0x2014 + ((x) * 0x100))
1359 +
1360 +/* Register for port MAC control register */
1361 +#define MT7530_PMCR_P(x) (0x3000 + ((x) * 0x100))
1362 +#define PMCR_IFG_XMIT(x) (((x) & 0x3) << 18)
1363 +#define PMCR_MAC_MODE BIT(16)
1364 +#define PMCR_FORCE_MODE BIT(15)
1365 +#define PMCR_TX_EN BIT(14)
1366 +#define PMCR_RX_EN BIT(13)
1367 +#define PMCR_BACKOFF_EN BIT(9)
1368 +#define PMCR_BACKPR_EN BIT(8)
1369 +#define PMCR_TX_FC_EN BIT(5)
1370 +#define PMCR_RX_FC_EN BIT(4)
1371 +#define PMCR_FORCE_SPEED_1000 BIT(3)
1372 +#define PMCR_FORCE_FDX BIT(1)
1373 +#define PMCR_FORCE_LNK BIT(0)
1374 +#define PMCR_COMMON_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1375 + PMCR_BACKOFF_EN | PMCR_BACKPR_EN | \
1376 + PMCR_TX_EN | PMCR_RX_EN | \
1377 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1378 +#define PMCR_CPUP_LINK (PMCR_COMMON_LINK | PMCR_FORCE_MODE | \
1379 + PMCR_FORCE_SPEED_1000 | \
1380 + PMCR_FORCE_FDX | \
1381 + PMCR_FORCE_LNK)
1382 +#define PMCR_USERP_LINK PMCR_COMMON_LINK
1383 +#define PMCR_FIXED_LINK (PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
1384 + PMCR_FORCE_MODE | PMCR_TX_EN | \
1385 + PMCR_RX_EN | PMCR_BACKPR_EN | \
1386 + PMCR_BACKOFF_EN | \
1387 + PMCR_FORCE_SPEED_1000 | \
1388 + PMCR_FORCE_FDX | \
1389 + PMCR_FORCE_LNK)
1390 +#define PMCR_FIXED_LINK_FC (PMCR_FIXED_LINK | \
1391 + PMCR_TX_FC_EN | PMCR_RX_FC_EN)
1392 +
1393 +#define MT7530_PMSR_P(x) (0x3008 + (x) * 0x100)
1394 +
1395 +/* Register for MIB */
1396 +#define MT7530_PORT_MIB_COUNTER(x) (0x4000 + (x) * 0x100)
1397 +#define MT7530_MIB_CCR 0x4fe0
1398 +#define CCR_MIB_ENABLE BIT(31)
1399 +#define CCR_RX_OCT_CNT_GOOD BIT(7)
1400 +#define CCR_RX_OCT_CNT_BAD BIT(6)
1401 +#define CCR_TX_OCT_CNT_GOOD BIT(5)
1402 +#define CCR_TX_OCT_CNT_BAD BIT(4)
1403 +#define CCR_MIB_FLUSH (CCR_RX_OCT_CNT_GOOD | \
1404 + CCR_RX_OCT_CNT_BAD | \
1405 + CCR_TX_OCT_CNT_GOOD | \
1406 + CCR_TX_OCT_CNT_BAD)
1407 +#define CCR_MIB_ACTIVATE (CCR_MIB_ENABLE | \
1408 + CCR_RX_OCT_CNT_GOOD | \
1409 + CCR_RX_OCT_CNT_BAD | \
1410 + CCR_TX_OCT_CNT_GOOD | \
1411 + CCR_TX_OCT_CNT_BAD)
1412 +/* Register for system reset */
1413 +#define MT7530_SYS_CTRL 0x7000
1414 +#define SYS_CTRL_PHY_RST BIT(2)
1415 +#define SYS_CTRL_SW_RST BIT(1)
1416 +#define SYS_CTRL_REG_RST BIT(0)
1417 +
1418 +/* Register for hw trap status */
1419 +#define MT7530_HWTRAP 0x7800
1420 +
1421 +/* Register for hw trap modification */
1422 +#define MT7530_MHWTRAP 0x7804
1423 +#define MHWTRAP_MANUAL BIT(16)
1424 +#define MHWTRAP_P5_MAC_SEL BIT(13)
1425 +#define MHWTRAP_P6_DIS BIT(8)
1426 +#define MHWTRAP_P5_RGMII_MODE BIT(7)
1427 +#define MHWTRAP_P5_DIS BIT(6)
1428 +#define MHWTRAP_PHY_ACCESS BIT(5)
1429 +
1430 +/* Register for TOP signal control */
1431 +#define MT7530_TOP_SIG_CTRL 0x7808
1432 +#define TOP_SIG_CTRL_NORMAL (BIT(17) | BIT(16))
1433 +
1434 +#define MT7530_IO_DRV_CR 0x7810
1435 +#define P5_IO_CLK_DRV(x) ((x) & 0x3)
1436 +#define P5_IO_DATA_DRV(x) (((x) & 0x3) << 4)
1437 +
1438 +#define MT7530_P6ECR 0x7830
1439 +#define P6_INTF_MODE_MASK 0x3
1440 +#define P6_INTF_MODE(x) ((x) & 0x3)
1441 +
1442 +/* Registers for TRGMII on the both side */
1443 +#define MT7530_TRGMII_RCK_CTRL 0x7a00
1444 +#define GSW_TRGMII_RCK_CTRL 0x300
1445 +#define RX_RST BIT(31)
1446 +#define RXC_DQSISEL BIT(30)
1447 +#define DQSI1_TAP_MASK (0x7f << 8)
1448 +#define DQSI0_TAP_MASK 0x7f
1449 +#define DQSI1_TAP(x) (((x) & 0x7f) << 8)
1450 +#define DQSI0_TAP(x) ((x) & 0x7f)
1451 +
1452 +#define MT7530_TRGMII_RCK_RTT 0x7a04
1453 +#define GSW_TRGMII_RCK_RTT 0x304
1454 +#define DQS1_GATE BIT(31)
1455 +#define DQS0_GATE BIT(30)
1456 +
1457 +#define MT7530_TRGMII_RD(x) (0x7a10 + (x) * 8)
1458 +#define GSW_TRGMII_RD(x) (0x310 + (x) * 8)
1459 +#define BSLIP_EN BIT(31)
1460 +#define EDGE_CHK BIT(30)
1461 +#define RD_TAP_MASK 0x7f
1462 +#define RD_TAP(x) ((x) & 0x7f)
1463 +
1464 +#define GSW_TRGMII_TXCTRL 0x340
1465 +#define MT7530_TRGMII_TXCTRL 0x7a40
1466 +#define TRAIN_TXEN BIT(31)
1467 +#define TXC_INV BIT(30)
1468 +#define TX_RST BIT(28)
1469 +
1470 +#define MT7530_TRGMII_TD_ODT(i) (0x7a54 + 8 * (i))
1471 +#define GSW_TRGMII_TD_ODT(i) (0x354 + 8 * (i))
1472 +#define TD_DM_DRVP(x) ((x) & 0xf)
1473 +#define TD_DM_DRVN(x) (((x) & 0xf) << 4)
1474 +
1475 +#define GSW_INTF_MODE 0x390
1476 +#define INTF_MODE_TRGMII BIT(1)
1477 +
1478 +#define MT7530_TRGMII_TCK_CTRL 0x7a78
1479 +#define TCK_TAP(x) (((x) & 0xf) << 8)
1480 +
1481 +#define MT7530_P5RGMIIRXCR 0x7b00
1482 +#define CSR_RGMII_EDGE_ALIGN BIT(8)
1483 +#define CSR_RGMII_RXC_0DEG_CFG(x) ((x) & 0xf)
1484 +
1485 +#define MT7530_P5RGMIITXCR 0x7b04
1486 +#define CSR_RGMII_TXC_CFG(x) ((x) & 0x1f)
1487 +
1488 +#define MT7530_CREV 0x7ffc
1489 +#define CHIP_NAME_SHIFT 16
1490 +#define MT7530_ID 0x7530
1491 +
1492 +/* Registers for core PLL access through mmd indirect */
1493 +#define CORE_PLL_GROUP2 0x401
1494 +#define RG_SYSPLL_EN_NORMAL BIT(15)
1495 +#define RG_SYSPLL_VODEN BIT(14)
1496 +#define RG_SYSPLL_LF BIT(13)
1497 +#define RG_SYSPLL_RST_DLY(x) (((x) & 0x3) << 12)
1498 +#define RG_SYSPLL_LVROD_EN BIT(10)
1499 +#define RG_SYSPLL_PREDIV(x) (((x) & 0x3) << 8)
1500 +#define RG_SYSPLL_POSDIV(x) (((x) & 0x3) << 5)
1501 +#define RG_SYSPLL_FBKSEL BIT(4)
1502 +#define RT_SYSPLL_EN_AFE_OLT BIT(0)
1503 +
1504 +#define CORE_PLL_GROUP4 0x403
1505 +#define RG_SYSPLL_DDSFBK_EN BIT(12)
1506 +#define RG_SYSPLL_BIAS_EN BIT(11)
1507 +#define RG_SYSPLL_BIAS_LPF_EN BIT(10)
1508 +
1509 +#define CORE_PLL_GROUP5 0x404
1510 +#define RG_LCDDS_PCW_NCPO1(x) ((x) & 0xffff)
1511 +
1512 +#define CORE_PLL_GROUP6 0x405
1513 +#define RG_LCDDS_PCW_NCPO0(x) ((x) & 0xffff)
1514 +
1515 +#define CORE_PLL_GROUP7 0x406
1516 +#define RG_LCDDS_PWDB BIT(15)
1517 +#define RG_LCDDS_ISO_EN BIT(13)
1518 +#define RG_LCCDS_C(x) (((x) & 0x7) << 4)
1519 +#define RG_LCDDS_PCW_NCPO_CHG BIT(3)
1520 +
1521 +#define CORE_PLL_GROUP10 0x409
1522 +#define RG_LCDDS_SSC_DELTA(x) ((x) & 0xfff)
1523 +
1524 +#define CORE_PLL_GROUP11 0x40a
1525 +#define RG_LCDDS_SSC_DELTA1(x) ((x) & 0xfff)
1526 +
1527 +#define CORE_GSWPLL_GRP1 0x40d
1528 +#define RG_GSWPLL_PREDIV(x) (((x) & 0x3) << 14)
1529 +#define RG_GSWPLL_POSDIV_200M(x) (((x) & 0x3) << 12)
1530 +#define RG_GSWPLL_EN_PRE BIT(11)
1531 +#define RG_GSWPLL_FBKSEL BIT(10)
1532 +#define RG_GSWPLL_BP BIT(9)
1533 +#define RG_GSWPLL_BR BIT(8)
1534 +#define RG_GSWPLL_FBKDIV_200M(x) ((x) & 0xff)
1535 +
1536 +#define CORE_GSWPLL_GRP2 0x40e
1537 +#define RG_GSWPLL_POSDIV_500M(x) (((x) & 0x3) << 8)
1538 +#define RG_GSWPLL_FBKDIV_500M(x) ((x) & 0xff)
1539 +
1540 +#define CORE_TRGMII_GSW_CLK_CG 0x410
1541 +#define REG_GSWCK_EN BIT(0)
1542 +#define REG_TRGMIICK_EN BIT(1)
1543 +
1544 +#define MIB_DESC(_s, _o, _n) \
1545 + { \
1546 + .size = (_s), \
1547 + .offset = (_o), \
1548 + .name = (_n), \
1549 + }
1550 +
1551 +struct mt7530_mib_desc {
1552 + unsigned int size;
1553 + unsigned int offset;
1554 + const char *name;
1555 +};
1556 +
1557 +struct mt7530_fdb {
1558 + u16 vid;
1559 + u8 port_mask;
1560 + u8 aging;
1561 + u8 mac[6];
1562 + bool noarp;
1563 +};
1564 +
1565 +struct mt7530_port {
1566 + bool enable;
1567 + u32 pm;
1568 +};
1569 +
1570 +/* struct mt7530_priv - This is the main data structure for holding the state
1571 + * of the driver
1572 + * @dev: The device pointer
1573 + * @ds: The pointer to the dsa core structure
1574 + * @bus: The bus used for the device and built-in PHY
1575 + * @rstc: The pointer to reset control used by MCM
1576 + * @ethernet: The regmap used for access TRGMII-based registers
1577 + * @core_pwr: The power supplied into the core
1578 + * @io_pwr: The power supplied into the I/O
1579 + * @reset: The descriptor for GPIO line tied to its reset pin
1580 + * @mcm: Flag for distinguishing if standalone IC or module
1581 + * coupling
1582 + * @ports: Holding the state among ports
1583 + * @reg_mutex: The lock for protecting among process accessing
1584 + * registers
1585 + */
1586 +struct mt7530_priv {
1587 + struct device *dev;
1588 + struct dsa_switch *ds;
1589 + struct mii_bus *bus;
1590 + struct reset_control *rstc;
1591 + struct regmap *ethernet;
1592 + struct regulator *core_pwr;
1593 + struct regulator *io_pwr;
1594 + struct gpio_desc *reset;
1595 + bool mcm;
1596 +
1597 + struct mt7530_port ports[MT7530_NUM_PORTS];
1598 + /* protect among processes for registers access*/
1599 + struct mutex reg_mutex;
1600 +};
1601 +
1602 +struct mt7530_hw_stats {
1603 + const char *string;
1604 + u16 reg;
1605 + u8 sizeof_stat;
1606 +};
1607 +
1608 +#endif /* __MT7530_H */