b53: allow ports with higher numbers than CPU port
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / b53 / b53_common.c
index 2b05d5d464d9804442c5c2c86162780c93074d5f..d96d8b8b6d7eeb872ea1d82b602740a6fe7cce1b 100644 (file)
@@ -794,6 +794,54 @@ static int b53_port_get_link(struct switch_dev *dev, int port,
 
 }
 
+static int b53_port_set_link(struct switch_dev *sw_dev, int port,
+                            struct switch_port_link *link)
+{
+       struct b53_device *dev = sw_to_b53(sw_dev);
+
+       /*
+        * TODO: BCM63XX requires special handling as it can have external phys
+        * and ports might be GE or only FE
+        */
+       if (is63xx(dev))
+               return -ENOTSUPP;
+
+       if (port == sw_dev->cpu_port)
+               return -EINVAL;
+
+       if (!(BIT(port) & dev->enabled_ports))
+               return -EINVAL;
+
+       if (link->speed == SWITCH_PORT_SPEED_1000 &&
+           (is5325(dev) || is5365(dev)))
+               return -EINVAL;
+
+       if (link->speed == SWITCH_PORT_SPEED_1000 && !link->duplex)
+               return -EINVAL;
+
+       return switch_generic_set_link(sw_dev, port, link);
+}
+
+static int b53_phy_read16(struct switch_dev *dev, int addr, u8 reg, u16 *value)
+{
+       struct b53_device *priv = sw_to_b53(dev);
+
+       if (priv->ops->phy_read16)
+               return priv->ops->phy_read16(priv, addr, reg, value);
+
+       return b53_read16(priv, B53_PORT_MII_PAGE(addr), reg, value);
+}
+
+static int b53_phy_write16(struct switch_dev *dev, int addr, u8 reg, u16 value)
+{
+       struct b53_device *priv = sw_to_b53(dev);
+
+       if (priv->ops->phy_write16)
+               return priv->ops->phy_write16(priv, addr, reg, value);
+
+       return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg, value);
+}
+
 static int b53_global_reset_switch(struct switch_dev *dev)
 {
        struct b53_device *priv = sw_to_b53(dev);
@@ -803,8 +851,8 @@ static int b53_global_reset_switch(struct switch_dev *dev)
        priv->enable_jumbo = 0;
        priv->allow_vid_4095 = 0;
 
-       memset(priv->vlans, 0, sizeof(priv->vlans) * dev->vlans);
-       memset(priv->ports, 0, sizeof(priv->ports) * dev->ports);
+       memset(priv->vlans, 0, sizeof(*priv->vlans) * dev->vlans);
+       memset(priv->ports, 0, sizeof(*priv->ports) * dev->ports);
 
        return b53_switch_reset(priv);
 }
@@ -1002,6 +1050,9 @@ static const struct switch_dev_ops b53_switch_ops_25 = {
        .apply_config = b53_global_apply_config,
        .reset_switch = b53_global_reset_switch,
        .get_port_link = b53_port_get_link,
+       .set_port_link = b53_port_set_link,
+       .phy_read16 = b53_phy_read16,
+       .phy_write16 = b53_phy_write16,
 };
 
 static const struct switch_dev_ops b53_switch_ops_65 = {
@@ -1025,6 +1076,9 @@ static const struct switch_dev_ops b53_switch_ops_65 = {
        .apply_config = b53_global_apply_config,
        .reset_switch = b53_global_reset_switch,
        .get_port_link = b53_port_get_link,
+       .set_port_link = b53_port_set_link,
+       .phy_read16 = b53_phy_read16,
+       .phy_write16 = b53_phy_write16,
 };
 
 static const struct switch_dev_ops b53_switch_ops = {
@@ -1048,6 +1102,9 @@ static const struct switch_dev_ops b53_switch_ops = {
        .apply_config = b53_global_apply_config,
        .reset_switch = b53_global_reset_switch,
        .get_port_link = b53_port_get_link,
+       .set_port_link = b53_port_set_link,
+       .phy_read16 = b53_phy_read16,
+       .phy_write16 = b53_phy_write16,
 };
 
 struct b53_chip_data {
@@ -1201,7 +1258,7 @@ static const struct b53_chip_data b53_switch_chips[] = {
                .dev_name = "BCM53011",
                .alias = "bcm53011",
                .vlans = 4096,
-               .enabled_ports = 0x1f,
+               .enabled_ports = 0x1bf,
                .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
                .vta_regs = B53_VTA_REGS,
                .duplex_reg = B53_DUPLEX_STAT_GE,
@@ -1313,9 +1370,8 @@ static int b53_switch_init(struct b53_device *dev)
                        sw_dev->cpu_port = 5;
        }
 
-       /* cpu port is always last */
-       sw_dev->ports = sw_dev->cpu_port + 1;
        dev->enabled_ports |= BIT(sw_dev->cpu_port);
+       sw_dev->ports = fls(dev->enabled_ports);
 
        dev->ports = devm_kzalloc(dev->dev,
                                  sizeof(struct b53_port) * sw_dev->ports,