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.
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.
12 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/if_ether.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/netlink.h>
23 #include <linux/bitops.h>
24 #include <net/genetlink.h>
25 #include <linux/switch.h>
26 #include <linux/delay.h>
27 #include <linux/phy.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/lockdep.h>
31 #include <linux/workqueue.h>
32 #include <linux/of_device.h>
36 #define MT7530_CPU_PORT 6
37 #define MT7530_NUM_PORTS 8
38 #define MT7530_NUM_VLANS 16
39 #define MT7530_MAX_VID 4095
40 #define MT7530_MIN_VID 0
43 #define REG_ESW_VLAN_VTCR 0x90
44 #define REG_ESW_VLAN_VAWD1 0x94
45 #define REG_ESW_VLAN_VAWD2 0x98
46 #define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
48 #define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
49 #define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
50 #define REG_ESW_VLAN_VAWD1_VALID BIT(0)
52 /* vlan egress mode */
60 #define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
61 #define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
62 #define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
64 #define REG_HWTRAP 0x7804
67 /* Global attributes. */
68 MT7530_ATTR_ENABLE_VLAN,
71 struct mt7530_port_entry {
75 struct mt7530_vlan_entry {
84 struct switch_dev swdev;
86 bool global_vlan_enable;
87 struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
88 struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
91 struct mt7530_mapping {
93 u16 pvids[MT7530_NUM_PORTS];
94 u8 members[MT7530_NUM_VLANS];
95 u8 etags[MT7530_NUM_VLANS];
96 u16 vids[MT7530_NUM_VLANS];
97 } mt7530_defaults[] = {
100 .pvids = { 1, 1, 1, 1, 2, 1, 1 },
101 .members = { 0, 0x6f, 0x50 },
102 .etags = { 0, 0x40, 0x40 },
106 .pvids = { 2, 1, 1, 1, 1, 1, 1 },
107 .members = { 0, 0x7e, 0x41 },
108 .etags = { 0, 0x40, 0x40 },
113 struct mt7530_mapping*
114 mt7530_find_mapping(struct device_node *np)
119 if (of_property_read_string(np, "ralink,port-map", &map))
122 for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
123 if (!strcmp(map, mt7530_defaults[i].name))
124 return &mt7530_defaults[i];
130 mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
134 for (i = 0; i < MT7530_NUM_PORTS; i++)
135 mt7530->port_entries[i].pvid = map->pvids[i];
137 for (i = 0; i < MT7530_NUM_VLANS; i++) {
138 mt7530->vlan_entries[i].member = map->members[i];
139 mt7530->vlan_entries[i].etags = map->etags[i];
140 mt7530->vlan_entries[i].vid = map->vids[i];
145 mt7530_reset_switch(struct switch_dev *dev)
147 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
150 memset(priv->port_entries, 0, sizeof(priv->port_entries));
151 memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
153 /* set default vid of each vlan to the same number of vlan, so the vid
154 * won't need be set explicitly.
156 for (i = 0; i < MT7530_NUM_VLANS; i++) {
157 priv->vlan_entries[i].vid = i;
164 mt7530_get_vlan_enable(struct switch_dev *dev,
165 const struct switch_attr *attr,
166 struct switch_val *val)
168 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
170 val->value.i = priv->global_vlan_enable;
176 mt7530_set_vlan_enable(struct switch_dev *dev,
177 const struct switch_attr *attr,
178 struct switch_val *val)
180 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
182 priv->global_vlan_enable = val->value.i != 0;
188 mt7530_r32(struct mt7530_priv *priv, u32 reg)
194 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
195 low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
196 high = mdiobus_read(priv->bus, 0x1f, 0x10);
198 return (high << 16) | (low & 0xffff);
201 val = ioread32(priv->base + reg);
202 pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
208 mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
211 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
212 mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
213 mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
217 pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
218 iowrite32(val, priv->base + reg);
222 mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
226 mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
228 for (i = 0; i < 20; i++) {
229 u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
231 if ((val & BIT(31)) == 0)
237 printk("mt7530: vtcr timeout\n");
241 mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
243 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
245 if (port >= MT7530_NUM_PORTS)
248 *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
255 mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
257 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
259 if (port >= MT7530_NUM_PORTS)
262 if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
265 priv->port_entries[port].pvid = pvid;
271 mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
273 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
280 if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
283 mt7530_vtcr(priv, 0, val->port_vlan);
285 member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
289 etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
291 for (i = 0; i < MT7530_NUM_PORTS; i++) {
292 struct switch_port *p;
295 if (!(member & BIT(i)))
298 p = &val->value.ports[val->len++];
301 etag = (etags >> (i * 2)) & 0x3;
303 if (etag == ETAG_CTRL_TAG)
304 p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
305 else if (etag != ETAG_CTRL_UNTAG)
306 printk("vlan egress tag control neither untag nor tag.\n");
313 mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
315 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
320 if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
321 val->len > MT7530_NUM_PORTS)
324 for (i = 0; i < val->len; i++) {
325 struct switch_port *p = &val->value.ports[i];
327 if (p->id >= MT7530_NUM_PORTS)
330 member |= BIT(p->id);
332 if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
335 priv->vlan_entries[val->port_vlan].member = member;
336 priv->vlan_entries[val->port_vlan].etags = etags;
342 mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
343 struct switch_val *val)
345 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
349 vlan = val->port_vlan;
350 vid = (u16)val->value.i;
352 if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
355 if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
358 priv->vlan_entries[vlan].vid = vid;
363 mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
364 struct switch_val *val)
366 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
370 vlan = val->port_vlan;
372 vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
382 mt7530_apply_config(struct switch_dev *dev)
384 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
387 if (!priv->global_vlan_enable) {
388 for (i = 0; i < MT7530_NUM_PORTS; i++)
389 mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
391 for (i = 0; i < MT7530_NUM_PORTS; i++)
392 mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
397 /* set all ports as security mode */
398 for (i = 0; i < MT7530_NUM_PORTS; i++)
399 mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
401 /* set all ports as user port */
402 for (i = 0; i < MT7530_NUM_PORTS; i++)
403 mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x81000000);
405 for (i = 0; i < MT7530_NUM_VLANS; i++) {
406 u16 vid = priv->vlan_entries[i].vid;
407 u8 member = priv->vlan_entries[i].member;
408 u8 etags = priv->vlan_entries[i].etags;
412 val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
420 mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
422 /* vlan port membership */
424 mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
425 REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
426 REG_ESW_VLAN_VAWD1_VALID);
428 mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
432 for (j = 0; j < MT7530_NUM_PORTS; j++) {
434 val |= ETAG_CTRL_TAG << (j * 2);
436 val |= ETAG_CTRL_UNTAG << (j * 2);
438 mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
440 /* write to vlan table */
441 mt7530_vtcr(priv, 1, i);
444 /* Port Default PVID */
445 for (i = 0; i < MT7530_NUM_PORTS; i++) {
447 val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
449 val |= priv->port_entries[i].pvid;
450 mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
457 mt7530_get_port_link(struct switch_dev *dev, int port,
458 struct switch_port_link *link)
460 struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
463 if (port < 0 || port >= MT7530_NUM_PORTS)
466 pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
468 link->link = pmsr & 1;
469 link->duplex = (pmsr >> 1) & 1;
470 speed = (pmsr >> 2) & 3;
474 link->speed = SWITCH_PORT_SPEED_10;
477 link->speed = SWITCH_PORT_SPEED_100;
480 case 3: /* forced gige speed can be 2 or 3 */
481 link->speed = SWITCH_PORT_SPEED_1000;
484 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
491 static const struct switch_attr mt7530_global[] = {
493 .type = SWITCH_TYPE_INT,
494 .name = "enable_vlan",
495 .description = "VLAN mode (1:enabled)",
497 .id = MT7530_ATTR_ENABLE_VLAN,
498 .get = mt7530_get_vlan_enable,
499 .set = mt7530_set_vlan_enable,
503 static const struct switch_attr mt7530_port[] = {
506 static const struct switch_attr mt7530_vlan[] = {
508 .type = SWITCH_TYPE_INT,
510 .description = "VLAN ID (0-4094)",
511 .set = mt7530_set_vid,
512 .get = mt7530_get_vid,
517 static const struct switch_dev_ops mt7530_ops = {
519 .attr = mt7530_global,
520 .n_attr = ARRAY_SIZE(mt7530_global),
524 .n_attr = ARRAY_SIZE(mt7530_port),
528 .n_attr = ARRAY_SIZE(mt7530_vlan),
530 .get_vlan_ports = mt7530_get_vlan_ports,
531 .set_vlan_ports = mt7530_set_vlan_ports,
532 .get_port_pvid = mt7530_get_port_pvid,
533 .set_port_pvid = mt7530_set_port_pvid,
534 .get_port_link = mt7530_get_port_link,
535 .apply_config = mt7530_apply_config,
536 .reset_switch = mt7530_reset_switch,
540 mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
542 struct switch_dev *swdev;
543 struct mt7530_priv *mt7530;
544 struct mt7530_mapping *map;
547 mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
553 mt7530->global_vlan_enable = vlan;
555 swdev = &mt7530->swdev;
557 swdev->alias = "mt7530";
558 swdev->name = "mt7530";
560 swdev->alias = "mt7620";
561 swdev->name = "mt7620";
563 swdev->cpu_port = MT7530_CPU_PORT;
564 swdev->ports = MT7530_NUM_PORTS;
565 swdev->vlans = MT7530_NUM_VLANS;
566 swdev->ops = &mt7530_ops;
568 ret = register_switch(swdev, NULL);
570 dev_err(dev, "failed to register mt7530\n");
575 map = mt7530_find_mapping(dev->of_node);
577 mt7530_apply_mapping(mt7530, map);
578 mt7530_apply_config(swdev);
581 if (!IS_ENABLED(CONFIG_SOC_MT7621) && bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
582 dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
583 mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
585 dev_info(dev, "loaded %s driver\n", swdev->name);