realtek: Add support for clause45 PHYs
[openwrt/staging/zorun.git] / target / linux / realtek / files-5.4 / drivers / net / dsa / rtl83xx / rtl838x.c
index d0e5162b66f86b6470b05a8a17865bf0cafecb81..773225b3c40f633d54a1c6f51e19570644d9d649 100644 (file)
@@ -513,6 +513,79 @@ timeout:
        return -ETIMEDOUT;
 }
 
+/*
+ * Read an mmd register of a PHY
+ */
+int rtl838x_read_mmd_phy(u32 port, u32 addr, u32 reg, u32 *val)
+{
+       u32 v;
+
+       mutex_lock(&smi_lock);
+
+       if (rtl838x_smi_wait_op(10000))
+               goto timeout;
+
+       sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
+       mdelay(10);
+
+       sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
+
+       v = addr << 16 | reg;
+       sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_3);
+
+       /* mmd-access | read | cmd-start */
+       v = 1 << 1 | 0 << 2 | 1;
+       sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
+
+       if (rtl838x_smi_wait_op(10000))
+               goto timeout;
+
+       *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
+
+       mutex_unlock(&smi_lock);
+       return 0;
+
+timeout:
+       mutex_unlock(&smi_lock);
+       return -ETIMEDOUT;
+}
+
+/*
+ * Write to an mmd register of a PHY
+ */
+int rtl838x_write_mmd_phy(u32 port, u32 addr, u32 reg, u32 val)
+{
+       u32 v;
+
+       pr_debug("MMD write: port %d, dev %d, reg %d, val %x\n", port, addr, reg, val);
+       val &= 0xffff;
+       mutex_lock(&smi_lock);
+
+       if (rtl838x_smi_wait_op(10000))
+               goto timeout;
+
+       sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
+       mdelay(10);
+
+       sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
+
+       sw_w32_mask(0x1f << 16, addr << 16, RTL838X_SMI_ACCESS_PHY_CTRL_3);
+       sw_w32_mask(0xffff, reg, RTL838X_SMI_ACCESS_PHY_CTRL_3);
+       /* mmd-access | write | cmd-start */
+       v = 1 << 1 | 1 << 2 | 1;
+       sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
+
+       if (rtl838x_smi_wait_op(10000))
+               goto timeout;
+
+       mutex_unlock(&smi_lock);
+       return 0;
+
+timeout:
+       mutex_unlock(&smi_lock);
+       return -ETIMEDOUT;
+}
+
 void rtl8380_get_version(struct rtl838x_switch_priv *priv)
 {
        u32 rw_save, info_save;