kernel: b53: force BCM531x5 port 5 link state if enabled
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / b53 / b53_common.c
index 41b5ae96c390e2eefc92f42a07d35389f2e19704..b884010d28268f317ff7a2920562f2bb31a1104c 100644 (file)
@@ -534,6 +534,19 @@ static int b53_switch_reset(struct b53_device *dev)
                b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
                           mii_port_override | PORT_OVERRIDE_EN |
                           PORT_OVERRIDE_LINK);
+
+               /* BCM47189 has another interface connected to the port 5 */
+               if (dev->enabled_ports & BIT(5)) {
+                       u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(5);
+                       u8 gmii_po;
+
+                       b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
+                       gmii_po |= GMII_PO_LINK |
+                                  GMII_PO_RX_FLOW |
+                                  GMII_PO_TX_FLOW |
+                                  GMII_PO_EN;
+                       b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
+               }
        } else if (is5301x(dev)) {
                if (cpu_port == 8) {
                        u8 mii_port_override;
@@ -794,6 +807,34 @@ 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);
@@ -1022,6 +1063,7 @@ 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,
 };
@@ -1047,6 +1089,7 @@ 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,
 };
@@ -1072,6 +1115,7 @@ 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,
 };
@@ -1339,9 +1383,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,