net: phy: Add Edge-rate driver for Microsemi PHYs.
[cascardo/linux.git] / drivers / net / phy / mscc.c
index d350deb..a17573e 100644 (file)
@@ -11,6 +11,8 @@
 #include <linux/mdio.h>
 #include <linux/mii.h>
 #include <linux/phy.h>
+#include <linux/of.h>
+#include <dt-bindings/net/mscc-phy-vsc8531.h>
 
 enum rgmii_rx_clock_delay {
        RGMII_RX_CLK_DELAY_0_2_NS = 0,
@@ -37,6 +39,10 @@ enum rgmii_rx_clock_delay {
 #define MII_VSC85XX_INT_MASK_MASK        0xa000
 #define MII_VSC85XX_INT_STATUS           26
 
+#define MSCC_PHY_WOL_MAC_CONTROL          27
+#define EDGE_RATE_CNTL_POS                5
+#define EDGE_RATE_CNTL_MASK               0x00E0
+
 #define MSCC_EXT_PAGE_ACCESS             31
 #define MSCC_PHY_PAGE_STANDARD           0x0000 /* Standard registers */
 #define MSCC_PHY_PAGE_EXTENDED_2         0x0002 /* Extended reg - page 2 */
@@ -50,6 +56,23 @@ enum rgmii_rx_clock_delay {
 #define PHY_ID_VSC8531                   0x00070570
 #define PHY_ID_VSC8541                   0x00070770
 
+struct edge_rate_table {
+       u16 vddmac;
+       int slowdown[MSCC_SLOWDOWN_MAX];
+};
+
+struct edge_rate_table edge_table[MSCC_VDDMAC_MAX] = {
+       {3300, { 0, -2, -4,  -7,  -10, -17, -29, -53} },
+       {2500, { 0, -3, -6,  -10, -14, -23, -37, -63} },
+       {1800, { 0, -5, -9,  -16, -23, -35, -52, -76} },
+       {1500, { 0, -6, -14, -21, -29, -42, -58, -77} },
+};
+
+struct vsc8531_private {
+       u8 edge_slowdown;
+       u16 vddmac;
+};
+
 static int vsc85xx_phy_page_set(struct phy_device *phydev, u8 page)
 {
        int rc;
@@ -58,6 +81,51 @@ static int vsc85xx_phy_page_set(struct phy_device *phydev, u8 page)
        return rc;
 }
 
+static u8 edge_rate_magic_get(u16 vddmac,
+                             int slowdown)
+{
+       int rc = (MSCC_SLOWDOWN_MAX - 1);
+       u8 vdd;
+       u8 sd;
+
+       for (vdd = 0; vdd < MSCC_VDDMAC_MAX; vdd++) {
+               if (edge_table[vdd].vddmac == vddmac) {
+                       for (sd = 0; sd < MSCC_SLOWDOWN_MAX; sd++) {
+                               if (edge_table[vdd].slowdown[sd] <= slowdown) {
+                                       rc = (MSCC_SLOWDOWN_MAX - sd - 1);
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       return rc;
+}
+
+static int vsc85xx_edge_rate_cntl_set(struct phy_device *phydev,
+                                     u8 edge_rate)
+{
+       int rc;
+       u16 reg_val;
+
+       mutex_lock(&phydev->lock);
+       rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2);
+       if (rc != 0)
+               goto out_unlock;
+       reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
+       reg_val &= ~(EDGE_RATE_CNTL_MASK);
+       reg_val |= (edge_rate << EDGE_RATE_CNTL_POS);
+       rc = phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val);
+       if (rc != 0)
+               goto out_unlock;
+       rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
+
+out_unlock:
+       mutex_unlock(&phydev->lock);
+
+       return rc;
+}
+
 static int vsc85xx_mac_if_set(struct phy_device *phydev,
                              phy_interface_t interface)
 {
@@ -116,9 +184,45 @@ out_unlock:
        return rc;
 }
 
+#ifdef CONFIG_OF_MDIO
+static int vsc8531_of_init(struct phy_device *phydev)
+{
+       int rc;
+       struct vsc8531_private *vsc8531 = phydev->priv;
+       struct device *dev = &phydev->mdio.dev;
+       struct device_node *of_node = dev->of_node;
+
+       if (!of_node)
+               return -ENODEV;
+
+       rc = of_property_read_u16(of_node, "vsc8531,vddmac",
+                                 &vsc8531->vddmac);
+       if (rc == -EINVAL)
+               vsc8531->vddmac = MSCC_VDDMAC_3300;
+       rc = of_property_read_u8(of_node, "vsc8531,edge-slowdown",
+                                &vsc8531->edge_slowdown);
+       if (rc == -EINVAL)
+               vsc8531->edge_slowdown = 0;
+
+       rc = 0;
+       return rc;
+}
+#else
+static int vsc8531_of_init(struct phy_device *phydev)
+{
+       return 0;
+}
+#endif /* CONFIG_OF_MDIO */
+
 static int vsc85xx_config_init(struct phy_device *phydev)
 {
        int rc;
+       struct vsc8531_private *vsc8531 = phydev->priv;
+       u8 edge_rate;
+
+       rc = vsc8531_of_init(phydev);
+       if (rc)
+               return rc;
 
        rc = vsc85xx_default_config(phydev);
        if (rc)
@@ -128,6 +232,12 @@ static int vsc85xx_config_init(struct phy_device *phydev)
        if (rc)
                return rc;
 
+       edge_rate = edge_rate_magic_get(vsc8531->vddmac,
+                                       -(int)vsc8531->edge_slowdown);
+       rc = vsc85xx_edge_rate_cntl_set(phydev, edge_rate);
+       if (rc)
+               return rc;
+
        rc = genphy_config_init(phydev);
 
        return rc;
@@ -160,6 +270,19 @@ static int vsc85xx_config_intr(struct phy_device *phydev)
        return rc;
 }
 
+static int vsc85xx_probe(struct phy_device *phydev)
+{
+       struct vsc8531_private *vsc8531;
+
+       vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
+       if (!vsc8531)
+               return -ENOMEM;
+
+       phydev->priv = vsc8531;
+
+       return 0;
+}
+
 /* Microsemi VSC85xx PHYs */
 static struct phy_driver vsc85xx_driver[] = {
 {
@@ -177,6 +300,7 @@ static struct phy_driver vsc85xx_driver[] = {
        .config_intr    = &vsc85xx_config_intr,
        .suspend        = &genphy_suspend,
        .resume         = &genphy_resume,
+       .probe          = &vsc85xx_probe,
 },
 {
        .phy_id         = PHY_ID_VSC8541,
@@ -193,6 +317,7 @@ static struct phy_driver vsc85xx_driver[] = {
        .config_intr    = &vsc85xx_config_intr,
        .suspend        = &genphy_suspend,
        .resume         = &genphy_resume,
+       .probe          = &vsc85xx_probe,
 }
 
 };