drivers: net: xgene: Fix backward compatibility
[cascardo/linux.git] / drivers / net / ethernet / apm / xgene / xgene_enet_hw.c
index 91a67a0..321fb19 100644 (file)
@@ -381,59 +381,6 @@ static void xgene_enet_rd_mcx_mac(struct xgene_enet_pdata *pdata,
                           rd_addr);
 }
 
-static int xgene_mii_phy_write(struct xgene_enet_pdata *pdata, int phy_id,
-                              u32 reg, u16 data)
-{
-       u32 addr = 0, wr_data = 0;
-       u32 done;
-       u8 wait = 10;
-
-       PHY_ADDR_SET(&addr, phy_id);
-       REG_ADDR_SET(&addr, reg);
-       xgene_enet_wr_mcx_mac(pdata, MII_MGMT_ADDRESS_ADDR, addr);
-
-       PHY_CONTROL_SET(&wr_data, data);
-       xgene_enet_wr_mcx_mac(pdata, MII_MGMT_CONTROL_ADDR, wr_data);
-       do {
-               usleep_range(5, 10);
-               xgene_enet_rd_mcx_mac(pdata, MII_MGMT_INDICATORS_ADDR, &done);
-       } while ((done & BUSY_MASK) && wait--);
-
-       if (done & BUSY_MASK) {
-               netdev_err(pdata->ndev, "MII_MGMT write failed\n");
-               return -EBUSY;
-       }
-
-       return 0;
-}
-
-static int xgene_mii_phy_read(struct xgene_enet_pdata *pdata,
-                             u8 phy_id, u32 reg)
-{
-       u32 addr = 0;
-       u32 data, done;
-       u8 wait = 10;
-
-       PHY_ADDR_SET(&addr, phy_id);
-       REG_ADDR_SET(&addr, reg);
-       xgene_enet_wr_mcx_mac(pdata, MII_MGMT_ADDRESS_ADDR, addr);
-       xgene_enet_wr_mcx_mac(pdata, MII_MGMT_COMMAND_ADDR, READ_CYCLE_MASK);
-       do {
-               usleep_range(5, 10);
-               xgene_enet_rd_mcx_mac(pdata, MII_MGMT_INDICATORS_ADDR, &done);
-       } while ((done & BUSY_MASK) && wait--);
-
-       if (done & BUSY_MASK) {
-               netdev_err(pdata->ndev, "MII_MGMT read failed\n");
-               return -EBUSY;
-       }
-
-       xgene_enet_rd_mcx_mac(pdata, MII_MGMT_STATUS_ADDR, &data);
-       xgene_enet_wr_mcx_mac(pdata, MII_MGMT_COMMAND_ADDR, 0);
-
-       return data;
-}
-
 static void xgene_gmac_set_mac_addr(struct xgene_enet_pdata *pdata)
 {
        u32 addr0, addr1;
@@ -572,7 +519,9 @@ static void xgene_gmac_init(struct xgene_enet_pdata *pdata)
 {
        u32 value;
 
-       xgene_gmac_reset(pdata);
+       if (!pdata->mdio_driver)
+               xgene_gmac_reset(pdata);
+
        xgene_gmac_set_speed(pdata);
        xgene_gmac_set_mac_addr(pdata);
 
@@ -680,6 +629,11 @@ static int xgene_enet_reset(struct xgene_enet_pdata *pdata)
        if (!xgene_ring_mgr_init(pdata))
                return -ENODEV;
 
+       if (pdata->mdio_driver) {
+               xgene_enet_config_ring_if_assoc(pdata);
+               return 0;
+       }
+
        if (dev->of_node) {
                clk_prepare_enable(pdata->clk);
                udelay(5);
@@ -755,28 +709,6 @@ static void xgene_gport_shutdown(struct xgene_enet_pdata *pdata)
        }
 }
 
-static int xgene_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
-{
-       struct xgene_enet_pdata *pdata = bus->priv;
-       u32 val;
-
-       val = xgene_mii_phy_read(pdata, mii_id, regnum);
-       netdev_dbg(pdata->ndev, "mdio_rd: bus=%d reg=%d val=%x\n",
-                  mii_id, regnum, val);
-
-       return val;
-}
-
-static int xgene_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
-                                u16 val)
-{
-       struct xgene_enet_pdata *pdata = bus->priv;
-
-       netdev_dbg(pdata->ndev, "mdio_wr: bus=%d reg=%d val=%x\n",
-                  mii_id, regnum, val);
-       return xgene_mii_phy_write(pdata, mii_id, regnum, val);
-}
-
 static void xgene_enet_adjust_link(struct net_device *ndev)
 {
        struct xgene_enet_pdata *pdata = netdev_priv(ndev);
@@ -787,34 +719,60 @@ static void xgene_enet_adjust_link(struct net_device *ndev)
                if (pdata->phy_speed != phydev->speed) {
                        pdata->phy_speed = phydev->speed;
                        mac_ops->set_speed(pdata);
-                       xgene_gmac_rx_enable(pdata);
-                       xgene_gmac_tx_enable(pdata);
+                       mac_ops->rx_enable(pdata);
+                       mac_ops->tx_enable(pdata);
                        phy_print_status(phydev);
                }
        } else {
-               xgene_gmac_rx_disable(pdata);
-               xgene_gmac_tx_disable(pdata);
+               mac_ops->rx_disable(pdata);
+               mac_ops->tx_disable(pdata);
                pdata->phy_speed = SPEED_UNKNOWN;
                phy_print_status(phydev);
        }
 }
 
-static int xgene_enet_phy_connect(struct net_device *ndev)
+#ifdef CONFIG_ACPI
+static struct acpi_device *acpi_phy_find_device(struct device *dev)
+{
+       struct acpi_reference_args args;
+       struct fwnode_handle *fw_node;
+       int status;
+
+       fw_node = acpi_fwnode_handle(ACPI_COMPANION(dev));
+       status = acpi_node_get_property_reference(fw_node, "phy-handle", 0,
+                                                 &args);
+       if (ACPI_FAILURE(status)) {
+               dev_dbg(dev, "No matching phy in ACPI table\n");
+               return NULL;
+       }
+
+       return args.adev;
+}
+#endif
+
+int xgene_enet_phy_connect(struct net_device *ndev)
 {
        struct xgene_enet_pdata *pdata = netdev_priv(ndev);
-       struct device_node *phy_np;
+       struct device_node *np;
        struct phy_device *phy_dev;
        struct device *dev = &pdata->pdev->dev;
+       int i;
 
        if (dev->of_node) {
-               phy_np = of_parse_phandle(dev->of_node, "phy-handle", 0);
-               if (!phy_np) {
-                       netdev_dbg(ndev, "No phy-handle found in DT\n");
-                       return -ENODEV;
+               for (i = 0 ; i < 2; i++) {
+                       np = of_parse_phandle(dev->of_node, "phy-handle", i);
+
+                       if (!np)
+                               continue;
+
+                       phy_dev = of_phy_connect(ndev, np,
+                                                &xgene_enet_adjust_link,
+                                                0, pdata->phy_mode);
+                       of_node_put(np);
+                       if (phy_dev)
+                               break;
                }
 
-               phy_dev = of_phy_connect(ndev, phy_np, &xgene_enet_adjust_link,
-                                        0, pdata->phy_mode);
                if (!phy_dev) {
                        netdev_err(ndev, "Could not connect to PHY\n");
                        return -ENODEV;
@@ -822,6 +780,11 @@ static int xgene_enet_phy_connect(struct net_device *ndev)
 
                pdata->phy_dev = phy_dev;
        } else {
+#ifdef CONFIG_ACPI
+               struct acpi_device *adev = acpi_phy_find_device(dev);
+               if (adev)
+                       pdata->phy_dev =  adev->driver_data;
+
                phy_dev = pdata->phy_dev;
 
                if (!phy_dev ||
@@ -830,6 +793,9 @@ static int xgene_enet_phy_connect(struct net_device *ndev)
                        netdev_err(ndev, "Could not connect to PHY\n");
                        return  -ENODEV;
                }
+#else
+               return -ENODEV;
+#endif
        }
 
        pdata->phy_speed = SPEED_UNKNOWN;
@@ -849,8 +815,8 @@ static int xgene_mdiobus_register(struct xgene_enet_pdata *pdata,
        struct phy_device *phy;
        struct device_node *child_np;
        struct device_node *mdio_np = NULL;
+       u32 phy_addr;
        int ret;
-       u32 phy_id;
 
        if (dev->of_node) {
                for_each_child_of_node(dev->of_node, child_np) {
@@ -877,21 +843,17 @@ static int xgene_mdiobus_register(struct xgene_enet_pdata *pdata,
        if (ret)
                return ret;
 
-       ret = device_property_read_u32(dev, "phy-channel", &phy_id);
+       ret = device_property_read_u32(dev, "phy-channel", &phy_addr);
        if (ret)
-               ret = device_property_read_u32(dev, "phy-addr", &phy_id);
+               ret = device_property_read_u32(dev, "phy-addr", &phy_addr);
        if (ret)
                return -EINVAL;
 
-       phy = get_phy_device(mdio, phy_id, false);
-       if (IS_ERR(phy))
+       phy = xgene_enet_phy_register(mdio, phy_addr);
+       if (!phy)
                return -EIO;
 
-       ret = phy_device_register(phy);
-       if (ret)
-               phy_device_free(phy);
-       else
-               pdata->phy_dev = phy;
+       pdata->phy_dev = phy;
 
        return ret;
 }
@@ -907,12 +869,12 @@ int xgene_enet_mdio_config(struct xgene_enet_pdata *pdata)
                return -ENOMEM;
 
        mdio_bus->name = "APM X-Gene MDIO bus";
-       mdio_bus->read = xgene_enet_mdio_read;
-       mdio_bus->write = xgene_enet_mdio_write;
+       mdio_bus->read = xgene_mdio_rgmii_read;
+       mdio_bus->write = xgene_mdio_rgmii_write;
        snprintf(mdio_bus->id, MII_BUS_ID_SIZE, "%s-%s", "xgene-mii",
                 ndev->name);
 
-       mdio_bus->priv = pdata;
+       mdio_bus->priv = (void __force *)pdata->mcx_mac_addr;
        mdio_bus->parent = &pdata->pdev->dev;
 
        ret = xgene_mdiobus_register(pdata, mdio_bus);
@@ -930,6 +892,12 @@ int xgene_enet_mdio_config(struct xgene_enet_pdata *pdata)
        return ret;
 }
 
+void xgene_enet_phy_disconnect(struct xgene_enet_pdata *pdata)
+{
+       if (pdata->phy_dev)
+               phy_disconnect(pdata->phy_dev);
+}
+
 void xgene_enet_mdio_remove(struct xgene_enet_pdata *pdata)
 {
        if (pdata->phy_dev)