ramips: move and rename out-of-tree mtk eth driver
[openwrt/openwrt.git] / target / linux / ramips / files / drivers / net / ethernet / ralink / mt7530.c
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
13 * Copyright (C) 2016 Vitaly Chekryzhev <13hakta@gmail.com>
14 */
15
16 #include <linux/if.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/netlink.h>
24 #include <linux/bitops.h>
25 #include <net/genetlink.h>
26 #include <linux/switch.h>
27 #include <linux/delay.h>
28 #include <linux/phy.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/lockdep.h>
32 #include <linux/workqueue.h>
33 #include <linux/of_device.h>
34
35 #include "mt7530.h"
36
37 #define MT7530_CPU_PORT 6
38 #define MT7530_NUM_PORTS 8
39 #ifdef CONFIG_SOC_MT7621
40 #define MT7530_NUM_VLANS 4095
41 #else
42 #define MT7530_NUM_VLANS 16
43 #endif
44 #define MT7530_MAX_VID 4095
45 #define MT7530_MIN_VID 0
46
47 #define MT7530_PORT_MIB_TXB_ID 2 /* TxGOC */
48 #define MT7530_PORT_MIB_RXB_ID 6 /* RxGOC */
49
50 #define MT7621_PORT_MIB_TXB_ID 18 /* TxByte */
51 #define MT7621_PORT_MIB_RXB_ID 37 /* RxByte */
52
53 /* registers */
54 #define REG_ESW_WT_MAC_MFC 0x10
55
56 #define REG_ESW_WT_MAC_MFC_MIRROR_ENABLE BIT(3)
57 #define REG_ESW_WT_MAC_MFC_MIRROR_DEST_MASK 0x07
58
59 #define REG_ESW_VLAN_VTCR 0x90
60 #define REG_ESW_VLAN_VAWD1 0x94
61 #define REG_ESW_VLAN_VAWD2 0x98
62 #define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
63
64 #define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
65 #define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
66 #define REG_ESW_VLAN_VAWD1_VALID BIT(0)
67
68 /* vlan egress mode */
69 enum {
70 ETAG_CTRL_UNTAG = 0,
71 ETAG_CTRL_TAG = 2,
72 ETAG_CTRL_SWAP = 1,
73 ETAG_CTRL_STACK = 3,
74 };
75
76 #define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
77 #define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
78 #define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
79
80 #define REG_ESW_PORT_PCR_MIRROR_SRC_RX_BIT BIT(8)
81 #define REG_ESW_PORT_PCR_MIRROR_SRC_TX_BIT BIT(9)
82 #define REG_ESW_PORT_PCR_MIRROR_SRC_RX_MASK 0x0100
83 #define REG_ESW_PORT_PCR_MIRROR_SRC_TX_MASK 0x0200
84
85 #define REG_HWTRAP 0x7804
86
87 #define MIB_DESC(_s , _o, _n) \
88 { \
89 .size = (_s), \
90 .offset = (_o), \
91 .name = (_n), \
92 }
93
94 struct mt7xxx_mib_desc {
95 unsigned int size;
96 unsigned int offset;
97 const char *name;
98 };
99
100 static const struct mt7xxx_mib_desc mt7620_mibs[] = {
101 MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_BCNT0, "PPE_AC_BCNT0"),
102 MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_PCNT0, "PPE_AC_PCNT0"),
103 MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_BCNT63, "PPE_AC_BCNT63"),
104 MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_PCNT63, "PPE_AC_PCNT63"),
105 MIB_DESC(1, MT7620_MIB_STATS_PPE_MTR_CNT0, "PPE_MTR_CNT0"),
106 MIB_DESC(1, MT7620_MIB_STATS_PPE_MTR_CNT63, "PPE_MTR_CNT63"),
107 MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_GBCNT, "GDM1_TX_GBCNT"),
108 MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_GPCNT, "GDM1_TX_GPCNT"),
109 MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_SKIPCNT, "GDM1_TX_SKIPCNT"),
110 MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_COLCNT, "GDM1_TX_COLCNT"),
111 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_GBCNT1, "GDM1_RX_GBCNT1"),
112 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_GPCNT1, "GDM1_RX_GPCNT1"),
113 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_OERCNT, "GDM1_RX_OERCNT"),
114 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_FERCNT, "GDM1_RX_FERCNT"),
115 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_SERCNT, "GDM1_RX_SERCNT"),
116 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_LERCNT, "GDM1_RX_LERCNT"),
117 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_CERCNT, "GDM1_RX_CERCNT"),
118 MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_FCCNT, "GDM1_RX_FCCNT"),
119 MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_GBCNT, "GDM2_TX_GBCNT"),
120 MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_GPCNT, "GDM2_TX_GPCNT"),
121 MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_SKIPCNT, "GDM2_TX_SKIPCNT"),
122 MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_COLCNT, "GDM2_TX_COLCNT"),
123 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_GBCNT, "GDM2_RX_GBCNT"),
124 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_GPCNT, "GDM2_RX_GPCNT"),
125 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_OERCNT, "GDM2_RX_OERCNT"),
126 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_FERCNT, "GDM2_RX_FERCNT"),
127 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_SERCNT, "GDM2_RX_SERCNT"),
128 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_LERCNT, "GDM2_RX_LERCNT"),
129 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_CERCNT, "GDM2_RX_CERCNT"),
130 MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_FCCNT, "GDM2_RX_FCCNT")
131 };
132
133 static const struct mt7xxx_mib_desc mt7620_port_mibs[] = {
134 MIB_DESC(1, MT7620_MIB_STATS_PORT_TGPCN, "TxGPC"),
135 MIB_DESC(1, MT7620_MIB_STATS_PORT_TBOCN, "TxBOC"),
136 MIB_DESC(1, MT7620_MIB_STATS_PORT_TGOCN, "TxGOC"),
137 MIB_DESC(1, MT7620_MIB_STATS_PORT_TEPCN, "TxEPC"),
138 MIB_DESC(1, MT7620_MIB_STATS_PORT_RGPCN, "RxGPC"),
139 MIB_DESC(1, MT7620_MIB_STATS_PORT_RBOCN, "RxBOC"),
140 MIB_DESC(1, MT7620_MIB_STATS_PORT_RGOCN, "RxGOC"),
141 MIB_DESC(1, MT7620_MIB_STATS_PORT_REPC1N, "RxEPC1"),
142 MIB_DESC(1, MT7620_MIB_STATS_PORT_REPC2N, "RxEPC2")
143 };
144
145 static const struct mt7xxx_mib_desc mt7621_mibs[] = {
146 MIB_DESC(1, MT7621_STATS_TDPC, "TxDrop"),
147 MIB_DESC(1, MT7621_STATS_TCRC, "TxCRC"),
148 MIB_DESC(1, MT7621_STATS_TUPC, "TxUni"),
149 MIB_DESC(1, MT7621_STATS_TMPC, "TxMulti"),
150 MIB_DESC(1, MT7621_STATS_TBPC, "TxBroad"),
151 MIB_DESC(1, MT7621_STATS_TCEC, "TxCollision"),
152 MIB_DESC(1, MT7621_STATS_TSCEC, "TxSingleCol"),
153 MIB_DESC(1, MT7621_STATS_TMCEC, "TxMultiCol"),
154 MIB_DESC(1, MT7621_STATS_TDEC, "TxDefer"),
155 MIB_DESC(1, MT7621_STATS_TLCEC, "TxLateCol"),
156 MIB_DESC(1, MT7621_STATS_TXCEC, "TxExcCol"),
157 MIB_DESC(1, MT7621_STATS_TPPC, "TxPause"),
158 MIB_DESC(1, MT7621_STATS_TL64PC, "Tx64Byte"),
159 MIB_DESC(1, MT7621_STATS_TL65PC, "Tx65Byte"),
160 MIB_DESC(1, MT7621_STATS_TL128PC, "Tx128Byte"),
161 MIB_DESC(1, MT7621_STATS_TL256PC, "Tx256Byte"),
162 MIB_DESC(1, MT7621_STATS_TL512PC, "Tx512Byte"),
163 MIB_DESC(1, MT7621_STATS_TL1024PC, "Tx1024Byte"),
164 MIB_DESC(2, MT7621_STATS_TOC, "TxByte"),
165 MIB_DESC(1, MT7621_STATS_RDPC, "RxDrop"),
166 MIB_DESC(1, MT7621_STATS_RFPC, "RxFiltered"),
167 MIB_DESC(1, MT7621_STATS_RUPC, "RxUni"),
168 MIB_DESC(1, MT7621_STATS_RMPC, "RxMulti"),
169 MIB_DESC(1, MT7621_STATS_RBPC, "RxBroad"),
170 MIB_DESC(1, MT7621_STATS_RAEPC, "RxAlignErr"),
171 MIB_DESC(1, MT7621_STATS_RCEPC, "RxCRC"),
172 MIB_DESC(1, MT7621_STATS_RUSPC, "RxUnderSize"),
173 MIB_DESC(1, MT7621_STATS_RFEPC, "RxFragment"),
174 MIB_DESC(1, MT7621_STATS_ROSPC, "RxOverSize"),
175 MIB_DESC(1, MT7621_STATS_RJEPC, "RxJabber"),
176 MIB_DESC(1, MT7621_STATS_RPPC, "RxPause"),
177 MIB_DESC(1, MT7621_STATS_RL64PC, "Rx64Byte"),
178 MIB_DESC(1, MT7621_STATS_RL65PC, "Rx65Byte"),
179 MIB_DESC(1, MT7621_STATS_RL128PC, "Rx128Byte"),
180 MIB_DESC(1, MT7621_STATS_RL256PC, "Rx256Byte"),
181 MIB_DESC(1, MT7621_STATS_RL512PC, "Rx512Byte"),
182 MIB_DESC(1, MT7621_STATS_RL1024PC, "Rx1024Byte"),
183 MIB_DESC(2, MT7621_STATS_ROC, "RxByte"),
184 MIB_DESC(1, MT7621_STATS_RDPC_CTRL, "RxCtrlDrop"),
185 MIB_DESC(1, MT7621_STATS_RDPC_ING, "RxIngDrop"),
186 MIB_DESC(1, MT7621_STATS_RDPC_ARL, "RxARLDrop")
187 };
188
189 enum {
190 /* Global attributes. */
191 MT7530_ATTR_ENABLE_VLAN,
192 };
193
194 struct mt7530_port_entry {
195 u16 pvid;
196 bool mirror_rx;
197 bool mirror_tx;
198 };
199
200 struct mt7530_vlan_entry {
201 u16 vid;
202 u8 member;
203 u8 etags;
204 };
205
206 struct mt7530_priv {
207 void __iomem *base;
208 struct mii_bus *bus;
209 struct switch_dev swdev;
210
211 u8 mirror_dest_port;
212 bool global_vlan_enable;
213 struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
214 struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
215 };
216
217 struct mt7530_mapping {
218 char *name;
219 u16 pvids[MT7530_NUM_PORTS];
220 u8 members[MT7530_NUM_VLANS];
221 u8 etags[MT7530_NUM_VLANS];
222 u16 vids[MT7530_NUM_VLANS];
223 } mt7530_defaults[] = {
224 {
225 .name = "llllw",
226 .pvids = { 1, 1, 1, 1, 2, 1, 1 },
227 .members = { 0, 0x6f, 0x50 },
228 .etags = { 0, 0x40, 0x40 },
229 .vids = { 0, 1, 2 },
230 }, {
231 .name = "wllll",
232 .pvids = { 2, 1, 1, 1, 1, 1, 1 },
233 .members = { 0, 0x7e, 0x41 },
234 .etags = { 0, 0x40, 0x40 },
235 .vids = { 0, 1, 2 },
236 }, {
237 .name = "lwlll",
238 .pvids = { 1, 2, 1, 1, 1, 1, 1 },
239 .members = { 0, 0x7d, 0x42 },
240 .etags = { 0, 0x40, 0x40 },
241 .vids = { 0, 1, 2 },
242 },
243 };
244
245 struct mt7530_mapping*
246 mt7530_find_mapping(struct device_node *np)
247 {
248 const char *map;
249 int i;
250
251 if (of_property_read_string(np, "mediatek,portmap", &map))
252 return NULL;
253
254 for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
255 if (!strcmp(map, mt7530_defaults[i].name))
256 return &mt7530_defaults[i];
257
258 return NULL;
259 }
260
261 static void
262 mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
263 {
264 int i = 0;
265
266 for (i = 0; i < MT7530_NUM_PORTS; i++)
267 mt7530->port_entries[i].pvid = map->pvids[i];
268
269 for (i = 0; i < MT7530_NUM_VLANS; i++) {
270 mt7530->vlan_entries[i].member = map->members[i];
271 mt7530->vlan_entries[i].etags = map->etags[i];
272 mt7530->vlan_entries[i].vid = map->vids[i];
273 }
274 }
275
276 static int
277 mt7530_reset_switch(struct switch_dev *dev)
278 {
279 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
280 int i;
281
282 memset(priv->port_entries, 0, sizeof(priv->port_entries));
283 memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
284
285 /* set default vid of each vlan to the same number of vlan, so the vid
286 * won't need be set explicitly.
287 */
288 for (i = 0; i < MT7530_NUM_VLANS; i++) {
289 priv->vlan_entries[i].vid = i;
290 }
291
292 return 0;
293 }
294
295 static int
296 mt7530_get_vlan_enable(struct switch_dev *dev,
297 const struct switch_attr *attr,
298 struct switch_val *val)
299 {
300 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
301
302 val->value.i = priv->global_vlan_enable;
303
304 return 0;
305 }
306
307 static int
308 mt7530_set_vlan_enable(struct switch_dev *dev,
309 const struct switch_attr *attr,
310 struct switch_val *val)
311 {
312 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
313
314 priv->global_vlan_enable = val->value.i != 0;
315
316 return 0;
317 }
318
319 static u32
320 mt7530_r32(struct mt7530_priv *priv, u32 reg)
321 {
322 u32 val;
323 if (priv->bus) {
324 u16 high, low;
325
326 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
327 low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
328 high = mdiobus_read(priv->bus, 0x1f, 0x10);
329
330 return (high << 16) | (low & 0xffff);
331 }
332
333 val = ioread32(priv->base + reg);
334 pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
335
336 return val;
337 }
338
339 static void
340 mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
341 {
342 if (priv->bus) {
343 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
344 mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
345 mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
346 return;
347 }
348
349 pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
350 iowrite32(val, priv->base + reg);
351 }
352
353 static void
354 mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
355 {
356 int i;
357
358 mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
359
360 for (i = 0; i < 20; i++) {
361 u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
362
363 if ((val & BIT(31)) == 0)
364 break;
365
366 udelay(1000);
367 }
368 if (i == 20)
369 printk("mt7530: vtcr timeout\n");
370 }
371
372 static int
373 mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
374 {
375 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
376
377 if (port >= MT7530_NUM_PORTS)
378 return -EINVAL;
379
380 *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
381 *val &= 0xfff;
382
383 return 0;
384 }
385
386 static int
387 mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
388 {
389 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
390
391 if (port >= MT7530_NUM_PORTS)
392 return -EINVAL;
393
394 if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
395 return -EINVAL;
396
397 priv->port_entries[port].pvid = pvid;
398
399 return 0;
400 }
401
402 static int
403 mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
404 {
405 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
406 u32 member;
407 u32 etags;
408 int i;
409
410 val->len = 0;
411
412 if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
413 return -EINVAL;
414
415 mt7530_vtcr(priv, 0, val->port_vlan);
416
417 member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
418 member >>= 16;
419 member &= 0xff;
420
421 etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
422
423 for (i = 0; i < MT7530_NUM_PORTS; i++) {
424 struct switch_port *p;
425 int etag;
426
427 if (!(member & BIT(i)))
428 continue;
429
430 p = &val->value.ports[val->len++];
431 p->id = i;
432
433 etag = (etags >> (i * 2)) & 0x3;
434
435 if (etag == ETAG_CTRL_TAG)
436 p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
437 else if (etag != ETAG_CTRL_UNTAG)
438 printk("vlan %d port %d egress tag control neither untag nor tag: %d.\n",
439 val->port_vlan, i, etag);
440 }
441
442 return 0;
443 }
444
445 static int
446 mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
447 {
448 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
449 u8 member = 0;
450 u8 etags = 0;
451 int i;
452
453 if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
454 val->len > MT7530_NUM_PORTS)
455 return -EINVAL;
456
457 for (i = 0; i < val->len; i++) {
458 struct switch_port *p = &val->value.ports[i];
459
460 if (p->id >= MT7530_NUM_PORTS)
461 return -EINVAL;
462
463 member |= BIT(p->id);
464
465 if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
466 etags |= BIT(p->id);
467 }
468 priv->vlan_entries[val->port_vlan].member = member;
469 priv->vlan_entries[val->port_vlan].etags = etags;
470
471 return 0;
472 }
473
474 static int
475 mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
476 struct switch_val *val)
477 {
478 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
479 int vlan;
480 u16 vid;
481
482 vlan = val->port_vlan;
483 vid = (u16)val->value.i;
484
485 if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
486 return -EINVAL;
487
488 if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
489 return -EINVAL;
490
491 priv->vlan_entries[vlan].vid = vid;
492 return 0;
493 }
494
495 static int
496 mt7621_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
497 struct switch_val *val)
498 {
499 val->value.i = val->port_vlan;
500 return 0;
501 }
502
503 static int
504 mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
505 struct switch_val *val)
506 {
507 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
508 u32 vid;
509 int vlan;
510
511 vlan = val->port_vlan;
512
513 vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
514 if (vlan & 1)
515 vid = vid >> 12;
516 vid &= 0xfff;
517
518 val->value.i = vid;
519 return 0;
520 }
521
522 static int
523 mt7530_get_mirror_monitor_port(struct switch_dev *dev, const struct switch_attr *attr,
524 struct switch_val *val)
525 {
526 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
527
528 val->value.i = priv->mirror_dest_port;
529
530 return 0;
531 }
532
533 static int
534 mt7530_set_mirror_monitor_port(struct switch_dev *dev, const struct switch_attr *attr,
535 struct switch_val *val)
536 {
537 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
538
539 priv->mirror_dest_port = val->value.i;
540
541 return 0;
542 }
543
544 static int
545 mt7530_get_port_mirror_rx(struct switch_dev *dev, const struct switch_attr *attr,
546 struct switch_val *val)
547 {
548 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
549
550 val->value.i = priv->port_entries[val->port_vlan].mirror_rx;
551
552 return 0;
553 }
554
555 static int
556 mt7530_set_port_mirror_rx(struct switch_dev *dev, const struct switch_attr *attr,
557 struct switch_val *val)
558 {
559 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
560
561 priv->port_entries[val->port_vlan].mirror_rx = val->value.i;
562
563 return 0;
564 }
565
566 static int
567 mt7530_get_port_mirror_tx(struct switch_dev *dev, const struct switch_attr *attr,
568 struct switch_val *val)
569 {
570 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
571
572 val->value.i = priv->port_entries[val->port_vlan].mirror_tx;
573
574 return 0;
575 }
576
577 static int
578 mt7530_set_port_mirror_tx(struct switch_dev *dev, const struct switch_attr *attr,
579 struct switch_val *val)
580 {
581 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
582
583 priv->port_entries[val->port_vlan].mirror_tx = val->value.i;
584
585 return 0;
586 }
587
588 static void
589 mt7530_write_vlan_entry(struct mt7530_priv *priv, int vlan, u16 vid,
590 u8 ports, u8 etags)
591 {
592 int port;
593 u32 val;
594
595 #ifndef CONFIG_SOC_MT7621
596 /* vid of vlan */
597 val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
598 if (vlan % 2 == 0) {
599 val &= 0xfff000;
600 val |= vid;
601 } else {
602 val &= 0xfff;
603 val |= (vid << 12);
604 }
605 mt7530_w32(priv, REG_ESW_VLAN_VTIM(vlan), val);
606 #endif
607
608 /* vlan port membership */
609 if (ports)
610 mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
611 REG_ESW_VLAN_VAWD1_VTAG_EN | (ports << 16) |
612 REG_ESW_VLAN_VAWD1_VALID);
613 else
614 mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
615
616 /* egress mode */
617 val = 0;
618 for (port = 0; port < MT7530_NUM_PORTS; port++) {
619 if (etags & BIT(port))
620 val |= ETAG_CTRL_TAG << (port * 2);
621 else
622 val |= ETAG_CTRL_UNTAG << (port * 2);
623 }
624 mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
625
626 /* write to vlan table */
627 #ifdef CONFIG_SOC_MT7621
628 mt7530_vtcr(priv, 1, vid);
629 #else
630 mt7530_vtcr(priv, 1, vlan);
631 #endif
632 }
633
634 static int
635 mt7530_apply_config(struct switch_dev *dev)
636 {
637 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
638 int i, j;
639 u8 tag_ports;
640 u8 untag_ports;
641 bool is_mirror = false;
642
643 if (!priv->global_vlan_enable) {
644 for (i = 0; i < MT7530_NUM_PORTS; i++)
645 mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00400000);
646
647 mt7530_w32(priv, REG_ESW_PORT_PCR(MT7530_CPU_PORT), 0x00ff0000);
648
649 for (i = 0; i < MT7530_NUM_PORTS; i++)
650 mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
651
652 return 0;
653 }
654
655 /* set all ports as security mode */
656 for (i = 0; i < MT7530_NUM_PORTS; i++)
657 mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
658
659 /* check if a port is used in tag/untag vlan egress mode */
660 tag_ports = 0;
661 untag_ports = 0;
662
663 for (i = 0; i < MT7530_NUM_VLANS; i++) {
664 u8 member = priv->vlan_entries[i].member;
665 u8 etags = priv->vlan_entries[i].etags;
666
667 if (!member)
668 continue;
669
670 for (j = 0; j < MT7530_NUM_PORTS; j++) {
671 if (!(member & BIT(j)))
672 continue;
673
674 if (etags & BIT(j))
675 tag_ports |= 1u << j;
676 else
677 untag_ports |= 1u << j;
678 }
679 }
680
681 /* set all untag-only ports as transparent and the rest as user port */
682 for (i = 0; i < MT7530_NUM_PORTS; i++) {
683 u32 pvc_mode = 0x81000000;
684
685 if (untag_ports & BIT(i) && !(tag_ports & BIT(i)))
686 pvc_mode = 0x810000c0;
687
688 mt7530_w32(priv, REG_ESW_PORT_PVC(i), pvc_mode);
689 }
690
691 /* first clear the swtich vlan table */
692 for (i = 0; i < MT7530_NUM_VLANS; i++)
693 mt7530_write_vlan_entry(priv, i, i, 0, 0);
694
695 /* now program only vlans with members to avoid
696 clobbering remapped entries in later iterations */
697 for (i = 0; i < MT7530_NUM_VLANS; i++) {
698 u16 vid = priv->vlan_entries[i].vid;
699 u8 member = priv->vlan_entries[i].member;
700 u8 etags = priv->vlan_entries[i].etags;
701
702 if (member)
703 mt7530_write_vlan_entry(priv, i, vid, member, etags);
704 }
705
706 /* Port Default PVID */
707 for (i = 0; i < MT7530_NUM_PORTS; i++) {
708 int vlan = priv->port_entries[i].pvid;
709 u16 pvid = 0;
710 u32 val;
711
712 if (vlan < MT7530_NUM_VLANS && priv->vlan_entries[vlan].member)
713 pvid = priv->vlan_entries[vlan].vid;
714
715 val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
716 val &= ~0xfff;
717 val |= pvid;
718 mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
719 }
720
721 /* set mirroring source port */
722 for (i = 0; i < MT7530_NUM_PORTS; i++) {
723 u32 val = mt7530_r32(priv, REG_ESW_PORT_PCR(i));
724 if (priv->port_entries[i].mirror_rx) {
725 val |= REG_ESW_PORT_PCR_MIRROR_SRC_RX_BIT;
726 is_mirror = true;
727 }
728
729 if (priv->port_entries[i].mirror_tx) {
730 val |= REG_ESW_PORT_PCR_MIRROR_SRC_TX_BIT;
731 is_mirror = true;
732 }
733
734 mt7530_w32(priv, REG_ESW_PORT_PCR(i), val);
735 }
736
737 /* set mirroring monitor port */
738 if (is_mirror) {
739 u32 val = mt7530_r32(priv, REG_ESW_WT_MAC_MFC);
740 val |= REG_ESW_WT_MAC_MFC_MIRROR_ENABLE;
741 val &= ~REG_ESW_WT_MAC_MFC_MIRROR_DEST_MASK;
742 val |= priv->mirror_dest_port;
743 mt7530_w32(priv, REG_ESW_WT_MAC_MFC, val);
744 }
745
746 return 0;
747 }
748
749 static int
750 mt7530_get_port_link(struct switch_dev *dev, int port,
751 struct switch_port_link *link)
752 {
753 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
754 u32 speed, pmsr;
755
756 if (port < 0 || port >= MT7530_NUM_PORTS)
757 return -EINVAL;
758
759 pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
760
761 link->link = pmsr & 1;
762 link->duplex = (pmsr >> 1) & 1;
763 speed = (pmsr >> 2) & 3;
764
765 switch (speed) {
766 case 0:
767 link->speed = SWITCH_PORT_SPEED_10;
768 break;
769 case 1:
770 link->speed = SWITCH_PORT_SPEED_100;
771 break;
772 case 2:
773 case 3: /* forced gige speed can be 2 or 3 */
774 link->speed = SWITCH_PORT_SPEED_1000;
775 break;
776 default:
777 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
778 break;
779 }
780
781 return 0;
782 }
783
784 static u64 get_mib_counter(struct mt7530_priv *priv, int i, int port)
785 {
786 unsigned int port_base;
787 u64 lo;
788
789 port_base = MT7621_MIB_COUNTER_BASE +
790 MT7621_MIB_COUNTER_PORT_OFFSET * port;
791
792 lo = mt7530_r32(priv, port_base + mt7621_mibs[i].offset);
793 if (mt7621_mibs[i].size == 2) {
794 u64 hi;
795
796 hi = mt7530_r32(priv, port_base + mt7621_mibs[i].offset + 4);
797 lo |= hi << 32;
798 }
799
800 return lo;
801 }
802
803 static int mt7621_sw_get_port_mib(struct switch_dev *dev,
804 const struct switch_attr *attr,
805 struct switch_val *val)
806 {
807 static char buf[4096];
808 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
809 int i, len = 0;
810
811 if (val->port_vlan >= MT7530_NUM_PORTS)
812 return -EINVAL;
813
814 len += snprintf(buf + len, sizeof(buf) - len,
815 "Port %d MIB counters\n", val->port_vlan);
816
817 for (i = 0; i < ARRAY_SIZE(mt7621_mibs); ++i) {
818 u64 counter;
819 len += snprintf(buf + len, sizeof(buf) - len,
820 "%-11s: ", mt7621_mibs[i].name);
821 counter = get_mib_counter(priv, i, val->port_vlan);
822 len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
823 counter);
824 }
825
826 val->value.s = buf;
827 val->len = len;
828 return 0;
829 }
830
831 static u64 get_mib_counter_7620(struct mt7530_priv *priv, int i)
832 {
833 return mt7530_r32(priv, MT7620_MIB_COUNTER_BASE + mt7620_mibs[i].offset);
834 }
835
836 static u64 get_mib_counter_port_7620(struct mt7530_priv *priv, int i, int port)
837 {
838 return mt7530_r32(priv,
839 MT7620_MIB_COUNTER_BASE_PORT +
840 (MT7620_MIB_COUNTER_PORT_OFFSET * port) +
841 mt7620_port_mibs[i].offset);
842 }
843
844 static int mt7530_sw_get_mib(struct switch_dev *dev,
845 const struct switch_attr *attr,
846 struct switch_val *val)
847 {
848 static char buf[4096];
849 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
850 int i, len = 0;
851
852 len += snprintf(buf + len, sizeof(buf) - len, "Switch MIB counters\n");
853
854 for (i = 0; i < ARRAY_SIZE(mt7620_mibs); ++i) {
855 u64 counter;
856 len += snprintf(buf + len, sizeof(buf) - len,
857 "%-11s: ", mt7620_mibs[i].name);
858 counter = get_mib_counter_7620(priv, i);
859 len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
860 counter);
861 }
862
863 val->value.s = buf;
864 val->len = len;
865 return 0;
866 }
867
868 static int mt7530_sw_get_port_mib(struct switch_dev *dev,
869 const struct switch_attr *attr,
870 struct switch_val *val)
871 {
872 static char buf[4096];
873 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
874 int i, len = 0;
875
876 if (val->port_vlan >= MT7530_NUM_PORTS)
877 return -EINVAL;
878
879 len += snprintf(buf + len, sizeof(buf) - len,
880 "Port %d MIB counters\n", val->port_vlan);
881
882 for (i = 0; i < ARRAY_SIZE(mt7620_port_mibs); ++i) {
883 u64 counter;
884 len += snprintf(buf + len, sizeof(buf) - len,
885 "%-11s: ", mt7620_port_mibs[i].name);
886 counter = get_mib_counter_port_7620(priv, i, val->port_vlan);
887 len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
888 counter);
889 }
890
891 val->value.s = buf;
892 val->len = len;
893 return 0;
894 }
895
896 static int mt7530_get_port_stats(struct switch_dev *dev, int port,
897 struct switch_port_stats *stats)
898 {
899 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
900
901 if (port < 0 || port >= MT7530_NUM_PORTS)
902 return -EINVAL;
903
904 stats->tx_bytes = get_mib_counter_port_7620(priv, MT7530_PORT_MIB_TXB_ID, port);
905 stats->rx_bytes = get_mib_counter_port_7620(priv, MT7530_PORT_MIB_RXB_ID, port);
906
907 return 0;
908 }
909
910 static int mt7621_get_port_stats(struct switch_dev *dev, int port,
911 struct switch_port_stats *stats)
912 {
913 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
914
915 if (port < 0 || port >= MT7530_NUM_PORTS)
916 return -EINVAL;
917
918 stats->tx_bytes = get_mib_counter(priv, MT7621_PORT_MIB_TXB_ID, port);
919 stats->rx_bytes = get_mib_counter(priv, MT7621_PORT_MIB_RXB_ID, port);
920
921 return 0;
922 }
923
924 static const struct switch_attr mt7530_global[] = {
925 {
926 .type = SWITCH_TYPE_INT,
927 .name = "enable_vlan",
928 .description = "VLAN mode (1:enabled)",
929 .max = 1,
930 .id = MT7530_ATTR_ENABLE_VLAN,
931 .get = mt7530_get_vlan_enable,
932 .set = mt7530_set_vlan_enable,
933 }, {
934 .type = SWITCH_TYPE_STRING,
935 .name = "mib",
936 .description = "Get MIB counters for switch",
937 .get = mt7530_sw_get_mib,
938 .set = NULL,
939 }, {
940 .type = SWITCH_TYPE_INT,
941 .name = "mirror_monitor_port",
942 .description = "Mirror monitor port",
943 .set = mt7530_set_mirror_monitor_port,
944 .get = mt7530_get_mirror_monitor_port,
945 .max = MT7530_NUM_PORTS - 1
946 },
947 };
948
949 static const struct switch_attr mt7621_port[] = {
950 {
951 .type = SWITCH_TYPE_STRING,
952 .name = "mib",
953 .description = "Get MIB counters for port",
954 .get = mt7621_sw_get_port_mib,
955 .set = NULL,
956 }, {
957 .type = SWITCH_TYPE_INT,
958 .name = "enable_mirror_rx",
959 .description = "Enable mirroring of RX packets",
960 .set = mt7530_set_port_mirror_rx,
961 .get = mt7530_get_port_mirror_rx,
962 .max = 1,
963 }, {
964 .type = SWITCH_TYPE_INT,
965 .name = "enable_mirror_tx",
966 .description = "Enable mirroring of TX packets",
967 .set = mt7530_set_port_mirror_tx,
968 .get = mt7530_get_port_mirror_tx,
969 .max = 1,
970 },
971 };
972
973 static const struct switch_attr mt7621_vlan[] = {
974 {
975 .type = SWITCH_TYPE_INT,
976 .name = "vid",
977 .description = "VLAN ID (0-4094)",
978 .set = mt7530_set_vid,
979 .get = mt7621_get_vid,
980 .max = 4094,
981 },
982 };
983
984 static const struct switch_attr mt7530_port[] = {
985 {
986 .type = SWITCH_TYPE_STRING,
987 .name = "mib",
988 .description = "Get MIB counters for port",
989 .get = mt7530_sw_get_port_mib,
990 .set = NULL,
991 }, {
992 .type = SWITCH_TYPE_INT,
993 .name = "enable_mirror_rx",
994 .description = "Enable mirroring of RX packets",
995 .set = mt7530_set_port_mirror_rx,
996 .get = mt7530_get_port_mirror_rx,
997 .max = 1,
998 }, {
999 .type = SWITCH_TYPE_INT,
1000 .name = "enable_mirror_tx",
1001 .description = "Enable mirroring of TX packets",
1002 .set = mt7530_set_port_mirror_tx,
1003 .get = mt7530_get_port_mirror_tx,
1004 .max = 1,
1005 },
1006 };
1007
1008 static const struct switch_attr mt7530_vlan[] = {
1009 {
1010 .type = SWITCH_TYPE_INT,
1011 .name = "vid",
1012 .description = "VLAN ID (0-4094)",
1013 .set = mt7530_set_vid,
1014 .get = mt7530_get_vid,
1015 .max = 4094,
1016 },
1017 };
1018
1019 static const struct switch_dev_ops mt7621_ops = {
1020 .attr_global = {
1021 .attr = mt7530_global,
1022 .n_attr = ARRAY_SIZE(mt7530_global),
1023 },
1024 .attr_port = {
1025 .attr = mt7621_port,
1026 .n_attr = ARRAY_SIZE(mt7621_port),
1027 },
1028 .attr_vlan = {
1029 .attr = mt7621_vlan,
1030 .n_attr = ARRAY_SIZE(mt7621_vlan),
1031 },
1032 .get_vlan_ports = mt7530_get_vlan_ports,
1033 .set_vlan_ports = mt7530_set_vlan_ports,
1034 .get_port_pvid = mt7530_get_port_pvid,
1035 .set_port_pvid = mt7530_set_port_pvid,
1036 .get_port_link = mt7530_get_port_link,
1037 .get_port_stats = mt7621_get_port_stats,
1038 .apply_config = mt7530_apply_config,
1039 .reset_switch = mt7530_reset_switch,
1040 };
1041
1042 static const struct switch_dev_ops mt7530_ops = {
1043 .attr_global = {
1044 .attr = mt7530_global,
1045 .n_attr = ARRAY_SIZE(mt7530_global),
1046 },
1047 .attr_port = {
1048 .attr = mt7530_port,
1049 .n_attr = ARRAY_SIZE(mt7530_port),
1050 },
1051 .attr_vlan = {
1052 .attr = mt7530_vlan,
1053 .n_attr = ARRAY_SIZE(mt7530_vlan),
1054 },
1055 .get_vlan_ports = mt7530_get_vlan_ports,
1056 .set_vlan_ports = mt7530_set_vlan_ports,
1057 .get_port_pvid = mt7530_get_port_pvid,
1058 .set_port_pvid = mt7530_set_port_pvid,
1059 .get_port_link = mt7530_get_port_link,
1060 .get_port_stats = mt7530_get_port_stats,
1061 .apply_config = mt7530_apply_config,
1062 .reset_switch = mt7530_reset_switch,
1063 };
1064
1065 int
1066 mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
1067 {
1068 struct switch_dev *swdev;
1069 struct mt7530_priv *mt7530;
1070 struct mt7530_mapping *map;
1071 int ret;
1072
1073 mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
1074 if (!mt7530)
1075 return -ENOMEM;
1076
1077 mt7530->base = base;
1078 mt7530->bus = bus;
1079 mt7530->global_vlan_enable = vlan;
1080
1081 swdev = &mt7530->swdev;
1082 if (bus) {
1083 swdev->alias = "mt7530";
1084 swdev->name = "mt7530";
1085 } else if (IS_ENABLED(CONFIG_SOC_MT7621)) {
1086 swdev->alias = "mt7621";
1087 swdev->name = "mt7621";
1088 } else {
1089 swdev->alias = "mt7620";
1090 swdev->name = "mt7620";
1091 }
1092 swdev->cpu_port = MT7530_CPU_PORT;
1093 swdev->ports = MT7530_NUM_PORTS;
1094 swdev->vlans = MT7530_NUM_VLANS;
1095 if (IS_ENABLED(CONFIG_SOC_MT7621))
1096 swdev->ops = &mt7621_ops;
1097 else
1098 swdev->ops = &mt7530_ops;
1099
1100 ret = register_switch(swdev, NULL);
1101 if (ret) {
1102 dev_err(dev, "failed to register mt7530\n");
1103 return ret;
1104 }
1105
1106
1107 map = mt7530_find_mapping(dev->of_node);
1108 if (map)
1109 mt7530_apply_mapping(mt7530, map);
1110 mt7530_apply_config(swdev);
1111
1112 /* magic vodoo */
1113 if (!IS_ENABLED(CONFIG_SOC_MT7621) && bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
1114 dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
1115 mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
1116 }
1117 dev_info(dev, "loaded %s driver\n", swdev->name);
1118
1119 return 0;
1120 }