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