generic: ar8216: don't use 0 as default VID on AR8327
[openwrt.git] / target / linux / generic / files / drivers / net / phy / ar8216.c
index a69d038..8dcaf0a 100644 (file)
@@ -361,7 +361,7 @@ ar8216_reg_wait(struct ar8216_priv *priv, u32 reg, u32 mask, u32 val,
 }
 
 static int
-ar8216_mib_capture(struct ar8216_priv *priv)
+ar8216_mib_op(struct ar8216_priv *priv, u32 op)
 {
        unsigned mib_func;
        int ret;
@@ -373,9 +373,10 @@ ar8216_mib_capture(struct ar8216_priv *priv)
        else
                mib_func = AR8216_REG_MIB_FUNC;
 
+       mutex_lock(&priv->reg_mutex);
        /* Capture the hardware statistics for all ports */
-       ar8216_rmw(priv, mib_func, AR8216_MIB_FUNC,
-                  (AR8216_MIB_FUNC_CAPTURE << AR8216_MIB_FUNC_S));
+       ar8216_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
+       mutex_unlock(&priv->reg_mutex);
 
        /* Wait for the capturing to complete. */
        ret = ar8216_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
@@ -389,31 +390,15 @@ out:
 }
 
 static int
-ar8216_mib_flush(struct ar8216_priv *priv)
+ar8216_mib_capture(struct ar8216_priv *priv)
 {
-       unsigned mib_func;
-       int ret;
-
-       lockdep_assert_held(&priv->mib_lock);
-
-       if (chip_is_ar8327(priv))
-               mib_func = AR8327_REG_MIB_FUNC;
-       else
-               mib_func = AR8216_REG_MIB_FUNC;
-
-       /* Flush hardware statistics for all ports */
-       ar8216_rmw(priv, mib_func, AR8216_MIB_FUNC,
-                  (AR8216_MIB_FUNC_FLUSH << AR8216_MIB_FUNC_S));
-
-       /* Wait for the capturing to complete. */
-       ret = ar8216_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
-       if (ret)
-               goto out;
-
-       ret = 0;
+       return ar8216_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
+}
 
-out:
-       return ret;
+static int
+ar8216_mib_flush(struct ar8216_priv *priv)
+{
+       return ar8216_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
 }
 
 static void
@@ -423,6 +408,8 @@ ar8216_mib_fetch_port_stat(struct ar8216_priv *priv, int port, bool flush)
        u64 *mib_stats;
        int i;
 
+       WARN_ON(port >= priv->dev.ports);
+
        lockdep_assert_held(&priv->mib_lock);
 
        if (chip_is_ar8327(priv))
@@ -916,6 +903,20 @@ ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
 
        case AR8327_PAD_MAC_SGMII:
                t = AR8327_PAD_SGMII_EN;
+
+               /*
+                * WAR for the QUalcomm Atheros AP136 board.
+                * It seems that RGMII TX/RX delay settings needs to be
+                * applied for SGMII mode as well, The ethernet is not
+                * reliable without this.
+                */
+               t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
+               t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
+               if (cfg->rxclk_delay_en)
+                       t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
+               if (cfg->txclk_delay_en)
+                       t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
+
                break;
 
        case AR8327_PAD_MAC2PHY_MII:
@@ -1073,19 +1074,13 @@ ar8327_init_globals(struct ar8216_priv *priv)
 }
 
 static void
-ar8327_init_cpuport(struct ar8216_priv *priv)
+ar8327_config_port(struct ar8216_priv *priv, unsigned int port,
+                   struct ar8327_port_cfg *cfg)
 {
-       struct ar8327_platform_data *pdata;
-       struct ar8327_port_cfg *cfg;
        u32 t;
 
-       pdata = priv->phy->dev.platform_data;
-       if (!pdata)
-               return;
-
-       cfg = &pdata->cpuport_cfg;
-       if (!cfg->force_link) {
-               priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU),
+       if (!cfg || !cfg->force_link) {
+               priv->write(priv, AR8327_REG_PORT_STATUS(port),
                            AR8216_PORT_STATUS_LINK_AUTO);
                return;
        }
@@ -1094,6 +1089,7 @@ ar8327_init_cpuport(struct ar8216_priv *priv)
        t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
        t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
        t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
+
        switch (cfg->speed) {
        case AR8327_PORT_SPEED_10:
                t |= AR8216_PORT_SPEED_10M;
@@ -1106,24 +1102,32 @@ ar8327_init_cpuport(struct ar8216_priv *priv)
                break;
        }
 
-       priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU), t);
+       priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
 }
 
 static void
 ar8327_init_port(struct ar8216_priv *priv, int port)
 {
+       struct ar8327_platform_data *pdata;
+       struct ar8327_port_cfg *cfg;
        u32 t;
 
-       if (port == AR8216_PORT_CPU) {
-               ar8327_init_cpuport(priv);
-       } else {
-               t = AR8216_PORT_STATUS_LINK_AUTO;
-               priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
-       }
+       pdata = priv->phy->dev.platform_data;
+
+       if (pdata && port == AR8216_PORT_CPU)
+               cfg = &pdata->port0_cfg;
+       else if (pdata && port == 6)
+               cfg = &pdata->port6_cfg;
+       else
+               cfg = NULL;
 
+       ar8327_config_port(priv, port, cfg);
+       
        priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
 
-       priv->write(priv, AR8327_REG_PORT_VLAN0(port), 0);
+       t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
+       t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
+       priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
 
        t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
        priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
@@ -1547,7 +1551,6 @@ ar8216_sw_get_port_mib(struct switch_dev *dev,
                goto unlock;
 
        ar8216_mib_fetch_port_stat(priv, port, false);
-       mutex_unlock(&priv->mib_lock);
 
        len += snprintf(buf + len, sizeof(priv->buf) - len,
                        "Port %d MIB counters\n",
@@ -1709,7 +1712,7 @@ ar8xxx_mib_work_func(struct work_struct *work)
 
 next_port:
        priv->mib_next_port++;
-       if (priv->mib_next_port > priv->dev.ports)
+       if (priv->mib_next_port >= priv->dev.ports)
                priv->mib_next_port = 0;
 
        mutex_unlock(&priv->mib_lock);