Commit ddd998af authored by David S. Miller's avatar David S. Miller

Merge branch 'qca8k-improvements'

Ansuel Smith says:

====================
Multiple improvement to qca8k stability

Currently qca8337 switch are widely used on ipq8064 based router.
On these particular router it was notice a very unstable switch with
port not link detected as link with unknown speed, port dropping
randomly and general unreliability. Lots of testing and comparison
between this dsa driver and the original qsdk driver showed lack of some
additional delay and values. A main difference arised from the original
driver and the dsa one. The original driver didn't use MASTER regs to
read phy status and the dedicated mdio driver worked correctly. Now that
the dsa driver actually use these regs, it was found that these special
read/write operation required mutual exclusion to normal
qca8k_read/write operation. The add of mutex for these operation fixed
the random port dropping and now only the actual linked port randomly
dropped. Adding additional delay for set_page operation and fixing a bug
in the mdio dedicated driver fixed also this problem. The current driver
requires also more time to apply vlan switch. All of these changes and
tweak permit a now very stable and reliable dsa driver and 0 port
dropping. This series is currently tested by at least 5 user with
different routers and all reports positive results and no problems.

Changes v6:
- Fix spelling mistake
- Change ms to ns (confirmed by datasheet)
Changes v5:
- Removed mdio patch (sent separetly to try to reduce the series)
  I know it was asked to reduced this series since it big, but rework
  the new changes to skip and error check looks wrong. Since half of them
  are actually already reviewed I think it's better to keep this series as is.
- Improve rgmii configurable patch
- Move qca8k phy dedicated driver to at803x phy driver
- Add support for dedicated internal mdio driver for qca8k
Changes v4:
- Use iopoll for busy_wait function
- Better describe and split some confusing commits
- Fix bad rgmii delay configurable patch
- Drop phy generic patch to pass flags with phylink_connect_phy
- Add dsa2 patch to declare mdio node in the switch node
- Add dsa patch to permit dsa driver to declare custom get_phys_mii_mask
    Some background about the last 2 patch.
    The qca8k switch doesn't have a 1:1 map between port reg and phy reg.
    Currently it's used a function to convert port to the internal phy reg.
    I added some patch to fix this.
    - The dsa driver now check if the mdio node is present and use the of variant
      of the mdiobus_register
    - A custom phy_mii_mask is required as currently the mask is generated from
      the port reg, but in our case the mask would be different as it should be
      generated from the phy reg. To generalize this I added an extra function
      that driver can provide to pass custom phy_mii_mask.
Changes v3:
- Revert mdio writel changes (use regmap with REGCACHE disabled)
- Split propagate error patch to 4 different patch
Changes v2:
- Implemented phy driver for internal PHYs
  I'm testing cable test functions as I found some documentation that
  actually declare regs about it. Problem is that it doesn't actually
  work. It seems that the value set are ignored by the phy.
- Made the rgmii delay configurable
- Reordered patch
- Split mdio patches to more specific ones
- Reworked mdio driver to use readl/writel instead of regmap
- Reworked the entire driver to make it aware of any read/write error.
- Added phy generic patch to pass flags with phylink_connect_phy
  function
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 709c0314 272833b9
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Required properties: Required properties:
- compatible: should be one of: - compatible: should be one of:
"qca,qca8327"
"qca,qca8334" "qca,qca8334"
"qca,qca8337" "qca,qca8337"
...@@ -20,6 +21,10 @@ described in dsa/dsa.txt. If the QCA8K switch is connect to a SoC's external ...@@ -20,6 +21,10 @@ described in dsa/dsa.txt. If the QCA8K switch is connect to a SoC's external
mdio-bus each subnode describing a port needs to have a valid phandle mdio-bus each subnode describing a port needs to have a valid phandle
referencing the internal PHY it is connected to. This is because there's no referencing the internal PHY it is connected to. This is because there's no
N:N mapping of port and PHY id. N:N mapping of port and PHY id.
To declare the internal mdio-bus configuration, declare a mdio node in the
switch node and declare the phandle for the port referencing the internal
PHY is connected to. In this config a internal mdio-bus is registered and
the mdio MASTER is used as communication.
Don't use mixed external and internal mdio-bus configurations, as this is Don't use mixed external and internal mdio-bus configurations, as this is
not supported by the hardware. not supported by the hardware.
...@@ -149,26 +154,61 @@ for the internal master mdio-bus configuration: ...@@ -149,26 +154,61 @@ for the internal master mdio-bus configuration:
port@1 { port@1 {
reg = <1>; reg = <1>;
label = "lan1"; label = "lan1";
phy-mode = "internal";
phy-handle = <&phy_port1>;
}; };
port@2 { port@2 {
reg = <2>; reg = <2>;
label = "lan2"; label = "lan2";
phy-mode = "internal";
phy-handle = <&phy_port2>;
}; };
port@3 { port@3 {
reg = <3>; reg = <3>;
label = "lan3"; label = "lan3";
phy-mode = "internal";
phy-handle = <&phy_port3>;
}; };
port@4 { port@4 {
reg = <4>; reg = <4>;
label = "lan4"; label = "lan4";
phy-mode = "internal";
phy-handle = <&phy_port4>;
}; };
port@5 { port@5 {
reg = <5>; reg = <5>;
label = "wan"; label = "wan";
phy-mode = "internal";
phy-handle = <&phy_port5>;
};
};
mdio {
#address-cells = <1>;
#size-cells = <0>;
phy_port1: phy@0 {
reg = <0>;
};
phy_port2: phy@1 {
reg = <1>;
};
phy_port3: phy@2 {
reg = <2>;
};
phy_port4: phy@3 {
reg = <3>;
};
phy_port5: phy@4 {
reg = <4>;
}; };
}; };
}; };
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <net/dsa.h> #include <net/dsa.h>
#include <linux/of_net.h> #include <linux/of_net.h>
#include <linux/of_mdio.h>
#include <linux/of_platform.h> #include <linux/of_platform.h>
#include <linux/if_bridge.h> #include <linux/if_bridge.h>
#include <linux/mdio.h> #include <linux/mdio.h>
...@@ -127,90 +128,125 @@ qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val) ...@@ -127,90 +128,125 @@ qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val)
"failed to write qca8k 32bit register\n"); "failed to write qca8k 32bit register\n");
} }
static void static int
qca8k_set_page(struct mii_bus *bus, u16 page) qca8k_set_page(struct mii_bus *bus, u16 page)
{ {
int ret;
if (page == qca8k_current_page) if (page == qca8k_current_page)
return; return 0;
if (bus->write(bus, 0x18, 0, page) < 0) ret = bus->write(bus, 0x18, 0, page);
if (ret < 0) {
dev_err_ratelimited(&bus->dev, dev_err_ratelimited(&bus->dev,
"failed to set qca8k page\n"); "failed to set qca8k page\n");
return ret;
}
qca8k_current_page = page; qca8k_current_page = page;
usleep_range(1000, 2000);
return 0;
} }
static u32 static u32
qca8k_read(struct qca8k_priv *priv, u32 reg) qca8k_read(struct qca8k_priv *priv, u32 reg)
{ {
struct mii_bus *bus = priv->bus;
u16 r1, r2, page; u16 r1, r2, page;
u32 val; u32 val;
qca8k_split_addr(reg, &r1, &r2, &page); qca8k_split_addr(reg, &r1, &r2, &page);
mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED); mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
qca8k_set_page(priv->bus, page); val = qca8k_set_page(bus, page);
val = qca8k_mii_read32(priv->bus, 0x10 | r2, r1); if (val < 0)
goto exit;
mutex_unlock(&priv->bus->mdio_lock); val = qca8k_mii_read32(bus, 0x10 | r2, r1);
exit:
mutex_unlock(&bus->mdio_lock);
return val; return val;
} }
static void static int
qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val) qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
{ {
struct mii_bus *bus = priv->bus;
u16 r1, r2, page; u16 r1, r2, page;
int ret;
qca8k_split_addr(reg, &r1, &r2, &page); qca8k_split_addr(reg, &r1, &r2, &page);
mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED); mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
ret = qca8k_set_page(bus, page);
if (ret < 0)
goto exit;
qca8k_set_page(priv->bus, page); qca8k_mii_write32(bus, 0x10 | r2, r1, val);
qca8k_mii_write32(priv->bus, 0x10 | r2, r1, val);
mutex_unlock(&priv->bus->mdio_lock); exit:
mutex_unlock(&bus->mdio_lock);
return ret;
} }
static u32 static int
qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 val) qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 write_val)
{ {
struct mii_bus *bus = priv->bus;
u16 r1, r2, page; u16 r1, r2, page;
u32 ret; u32 val;
int ret;
qca8k_split_addr(reg, &r1, &r2, &page); qca8k_split_addr(reg, &r1, &r2, &page);
mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED); mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
ret = qca8k_set_page(bus, page);
if (ret < 0)
goto exit;
val = qca8k_mii_read32(bus, 0x10 | r2, r1);
if (val < 0) {
ret = val;
goto exit;
}
qca8k_set_page(priv->bus, page); val &= ~mask;
ret = qca8k_mii_read32(priv->bus, 0x10 | r2, r1); val |= write_val;
ret &= ~mask; qca8k_mii_write32(bus, 0x10 | r2, r1, val);
ret |= val;
qca8k_mii_write32(priv->bus, 0x10 | r2, r1, ret);
mutex_unlock(&priv->bus->mdio_lock); exit:
mutex_unlock(&bus->mdio_lock);
return ret; return ret;
} }
static void static int
qca8k_reg_set(struct qca8k_priv *priv, u32 reg, u32 val) qca8k_reg_set(struct qca8k_priv *priv, u32 reg, u32 val)
{ {
qca8k_rmw(priv, reg, 0, val); return qca8k_rmw(priv, reg, 0, val);
} }
static void static int
qca8k_reg_clear(struct qca8k_priv *priv, u32 reg, u32 val) qca8k_reg_clear(struct qca8k_priv *priv, u32 reg, u32 val)
{ {
qca8k_rmw(priv, reg, val, 0); return qca8k_rmw(priv, reg, val, 0);
} }
static int static int
qca8k_regmap_read(void *ctx, uint32_t reg, uint32_t *val) qca8k_regmap_read(void *ctx, uint32_t reg, uint32_t *val)
{ {
struct qca8k_priv *priv = (struct qca8k_priv *)ctx; struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
int ret;
ret = qca8k_read(priv, reg);
if (ret < 0)
return ret;
*val = qca8k_read(priv, reg); *val = ret;
return 0; return 0;
} }
...@@ -220,9 +256,7 @@ qca8k_regmap_write(void *ctx, uint32_t reg, uint32_t val) ...@@ -220,9 +256,7 @@ qca8k_regmap_write(void *ctx, uint32_t reg, uint32_t val)
{ {
struct qca8k_priv *priv = (struct qca8k_priv *)ctx; struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
qca8k_write(priv, reg, val); return qca8k_write(priv, reg, val);
return 0;
} }
static const struct regmap_range qca8k_readable_ranges[] = { static const struct regmap_range qca8k_readable_ranges[] = {
...@@ -262,32 +296,36 @@ static struct regmap_config qca8k_regmap_config = { ...@@ -262,32 +296,36 @@ static struct regmap_config qca8k_regmap_config = {
static int static int
qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask) qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask)
{ {
unsigned long timeout; u32 val;
int ret;
timeout = jiffies + msecs_to_jiffies(20);
/* loop until the busy flag has cleared */ ret = read_poll_timeout(qca8k_read, val, !(val & mask),
do { 0, QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
u32 val = qca8k_read(priv, reg); priv, reg);
int busy = val & mask;
if (!busy) /* Check if qca8k_read has failed for a different reason
break; * before returning -ETIMEDOUT
cond_resched(); */
} while (!time_after_eq(jiffies, timeout)); if (ret < 0 && val < 0)
return val;
return time_after_eq(jiffies, timeout); return ret;
} }
static void static int
qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb) qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
{ {
u32 reg[4]; u32 reg[4], val;
int i; int i;
/* load the ARL table into an array */ /* load the ARL table into an array */
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++) {
reg[i] = qca8k_read(priv, QCA8K_REG_ATU_DATA0 + (i * 4)); val = qca8k_read(priv, QCA8K_REG_ATU_DATA0 + (i * 4));
if (val < 0)
return val;
reg[i] = val;
}
/* vid - 83:72 */ /* vid - 83:72 */
fdb->vid = (reg[2] >> QCA8K_ATU_VID_S) & QCA8K_ATU_VID_M; fdb->vid = (reg[2] >> QCA8K_ATU_VID_S) & QCA8K_ATU_VID_M;
...@@ -302,6 +340,8 @@ qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb) ...@@ -302,6 +340,8 @@ qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
fdb->mac[3] = (reg[0] >> QCA8K_ATU_ADDR3_S) & 0xff; fdb->mac[3] = (reg[0] >> QCA8K_ATU_ADDR3_S) & 0xff;
fdb->mac[4] = (reg[0] >> QCA8K_ATU_ADDR4_S) & 0xff; fdb->mac[4] = (reg[0] >> QCA8K_ATU_ADDR4_S) & 0xff;
fdb->mac[5] = reg[0] & 0xff; fdb->mac[5] = reg[0] & 0xff;
return 0;
} }
static void static void
...@@ -334,6 +374,7 @@ static int ...@@ -334,6 +374,7 @@ static int
qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port) qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
{ {
u32 reg; u32 reg;
int ret;
/* Set the command and FDB index */ /* Set the command and FDB index */
reg = QCA8K_ATU_FUNC_BUSY; reg = QCA8K_ATU_FUNC_BUSY;
...@@ -344,15 +385,20 @@ qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port) ...@@ -344,15 +385,20 @@ qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
} }
/* Write the function register triggering the table access */ /* Write the function register triggering the table access */
qca8k_write(priv, QCA8K_REG_ATU_FUNC, reg); ret = qca8k_write(priv, QCA8K_REG_ATU_FUNC, reg);
if (ret)
return ret;
/* wait for completion */ /* wait for completion */
if (qca8k_busy_wait(priv, QCA8K_REG_ATU_FUNC, QCA8K_ATU_FUNC_BUSY)) ret = qca8k_busy_wait(priv, QCA8K_REG_ATU_FUNC, QCA8K_ATU_FUNC_BUSY);
return -1; if (ret)
return ret;
/* Check for table full violation when adding an entry */ /* Check for table full violation when adding an entry */
if (cmd == QCA8K_FDB_LOAD) { if (cmd == QCA8K_FDB_LOAD) {
reg = qca8k_read(priv, QCA8K_REG_ATU_FUNC); reg = qca8k_read(priv, QCA8K_REG_ATU_FUNC);
if (reg < 0)
return reg;
if (reg & QCA8K_ATU_FUNC_FULL) if (reg & QCA8K_ATU_FUNC_FULL)
return -1; return -1;
} }
...@@ -367,10 +413,10 @@ qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port) ...@@ -367,10 +413,10 @@ qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port)
qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging); qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging);
ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port); ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port);
if (ret >= 0) if (ret < 0)
qca8k_fdb_read(priv, fdb); return ret;
return ret; return qca8k_fdb_read(priv, fdb);
} }
static int static int
...@@ -412,6 +458,7 @@ static int ...@@ -412,6 +458,7 @@ static int
qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid) qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid)
{ {
u32 reg; u32 reg;
int ret;
/* Set the command and VLAN index */ /* Set the command and VLAN index */
reg = QCA8K_VTU_FUNC1_BUSY; reg = QCA8K_VTU_FUNC1_BUSY;
...@@ -419,15 +466,20 @@ qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid) ...@@ -419,15 +466,20 @@ qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid)
reg |= vid << QCA8K_VTU_FUNC1_VID_S; reg |= vid << QCA8K_VTU_FUNC1_VID_S;
/* Write the function register triggering the table access */ /* Write the function register triggering the table access */
qca8k_write(priv, QCA8K_REG_VTU_FUNC1, reg); ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC1, reg);
if (ret)
return ret;
/* wait for completion */ /* wait for completion */
if (qca8k_busy_wait(priv, QCA8K_REG_VTU_FUNC1, QCA8K_VTU_FUNC1_BUSY)) ret = qca8k_busy_wait(priv, QCA8K_REG_VTU_FUNC1, QCA8K_VTU_FUNC1_BUSY);
return -ETIMEDOUT; if (ret)
return ret;
/* Check for table full violation when adding an entry */ /* Check for table full violation when adding an entry */
if (cmd == QCA8K_VLAN_LOAD) { if (cmd == QCA8K_VLAN_LOAD) {
reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC1); reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC1);
if (reg < 0)
return reg;
if (reg & QCA8K_VTU_FUNC1_FULL) if (reg & QCA8K_VTU_FUNC1_FULL)
return -ENOMEM; return -ENOMEM;
} }
...@@ -454,6 +506,8 @@ qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged) ...@@ -454,6 +506,8 @@ qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged)
goto out; goto out;
reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC0); reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC0);
if (reg < 0)
return reg;
reg |= QCA8K_VTU_FUNC0_VALID | QCA8K_VTU_FUNC0_IVL_EN; reg |= QCA8K_VTU_FUNC0_VALID | QCA8K_VTU_FUNC0_IVL_EN;
reg &= ~(QCA8K_VTU_FUNC0_EG_MODE_MASK << QCA8K_VTU_FUNC0_EG_MODE_S(port)); reg &= ~(QCA8K_VTU_FUNC0_EG_MODE_MASK << QCA8K_VTU_FUNC0_EG_MODE_S(port));
if (untagged) if (untagged)
...@@ -463,7 +517,9 @@ qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged) ...@@ -463,7 +517,9 @@ qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged)
reg |= QCA8K_VTU_FUNC0_EG_MODE_TAG << reg |= QCA8K_VTU_FUNC0_EG_MODE_TAG <<
QCA8K_VTU_FUNC0_EG_MODE_S(port); QCA8K_VTU_FUNC0_EG_MODE_S(port);
qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg); ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
if (ret)
return ret;
ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid); ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);
out: out:
...@@ -485,6 +541,8 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid) ...@@ -485,6 +541,8 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid)
goto out; goto out;
reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC0); reg = qca8k_read(priv, QCA8K_REG_VTU_FUNC0);
if (reg < 0)
return reg;
reg &= ~(3 << QCA8K_VTU_FUNC0_EG_MODE_S(port)); reg &= ~(3 << QCA8K_VTU_FUNC0_EG_MODE_S(port));
reg |= QCA8K_VTU_FUNC0_EG_MODE_NOT << reg |= QCA8K_VTU_FUNC0_EG_MODE_NOT <<
QCA8K_VTU_FUNC0_EG_MODE_S(port); QCA8K_VTU_FUNC0_EG_MODE_S(port);
...@@ -504,7 +562,9 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid) ...@@ -504,7 +562,9 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid)
if (del) { if (del) {
ret = qca8k_vlan_access(priv, QCA8K_VLAN_PURGE, vid); ret = qca8k_vlan_access(priv, QCA8K_VLAN_PURGE, vid);
} else { } else {
qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg); ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
if (ret)
return ret;
ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid); ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);
} }
...@@ -514,15 +574,29 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid) ...@@ -514,15 +574,29 @@ qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid)
return ret; return ret;
} }
static void static int
qca8k_mib_init(struct qca8k_priv *priv) qca8k_mib_init(struct qca8k_priv *priv)
{ {
int ret;
mutex_lock(&priv->reg_mutex); mutex_lock(&priv->reg_mutex);
qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_FLUSH | QCA8K_MIB_BUSY); ret = qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_FLUSH | QCA8K_MIB_BUSY);
qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY); if (ret)
qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_CPU_KEEP); goto exit;
qca8k_write(priv, QCA8K_REG_MODULE_EN, QCA8K_MODULE_EN_MIB);
ret = qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY);
if (ret)
goto exit;
ret = qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_CPU_KEEP);
if (ret)
goto exit;
ret = qca8k_write(priv, QCA8K_REG_MODULE_EN, QCA8K_MODULE_EN_MIB);
exit:
mutex_unlock(&priv->reg_mutex); mutex_unlock(&priv->reg_mutex);
return ret;
} }
static void static void
...@@ -556,52 +630,107 @@ qca8k_port_to_phy(int port) ...@@ -556,52 +630,107 @@ qca8k_port_to_phy(int port)
} }
static int static int
qca8k_mdio_write(struct qca8k_priv *priv, int port, u32 regnum, u16 data) qca8k_mdio_busy_wait(struct mii_bus *bus, u32 reg, u32 mask)
{
u16 r1, r2, page;
u32 val;
int ret;
qca8k_split_addr(reg, &r1, &r2, &page);
ret = read_poll_timeout(qca8k_mii_read32, val, !(val & mask), 0,
QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
bus, 0x10 | r2, r1);
/* Check if qca8k_read has failed for a different reason
* before returnting -ETIMEDOUT
*/
if (ret < 0 && val < 0)
return val;
return ret;
}
static int
qca8k_mdio_write(struct mii_bus *salve_bus, int phy, int regnum, u16 data)
{ {
u32 phy, val; struct qca8k_priv *priv = salve_bus->priv;
struct mii_bus *bus = priv->bus;
u16 r1, r2, page;
u32 val;
int ret;
if (regnum >= QCA8K_MDIO_MASTER_MAX_REG) if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
return -EINVAL; return -EINVAL;
/* callee is responsible for not passing bad ports,
* but we still would like to make spills impossible.
*/
phy = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN | val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
QCA8K_MDIO_MASTER_WRITE | QCA8K_MDIO_MASTER_PHY_ADDR(phy) | QCA8K_MDIO_MASTER_WRITE | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
QCA8K_MDIO_MASTER_REG_ADDR(regnum) | QCA8K_MDIO_MASTER_REG_ADDR(regnum) |
QCA8K_MDIO_MASTER_DATA(data); QCA8K_MDIO_MASTER_DATA(data);
qca8k_write(priv, QCA8K_MDIO_MASTER_CTRL, val); qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);
return qca8k_busy_wait(priv, QCA8K_MDIO_MASTER_CTRL, mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
QCA8K_MDIO_MASTER_BUSY);
ret = qca8k_set_page(bus, page);
if (ret)
goto exit;
qca8k_mii_write32(bus, 0x10 | r2, r1, val);
ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
QCA8K_MDIO_MASTER_BUSY);
exit:
/* even if the busy_wait timeouts try to clear the MASTER_EN */
qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
mutex_unlock(&bus->mdio_lock);
return ret;
} }
static int static int
qca8k_mdio_read(struct qca8k_priv *priv, int port, u32 regnum) qca8k_mdio_read(struct mii_bus *salve_bus, int phy, int regnum)
{ {
u32 phy, val; struct qca8k_priv *priv = salve_bus->priv;
struct mii_bus *bus = priv->bus;
u16 r1, r2, page;
u32 val;
int ret;
if (regnum >= QCA8K_MDIO_MASTER_MAX_REG) if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
return -EINVAL; return -EINVAL;
/* callee is responsible for not passing bad ports,
* but we still would like to make spills impossible.
*/
phy = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN | val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
QCA8K_MDIO_MASTER_READ | QCA8K_MDIO_MASTER_PHY_ADDR(phy) | QCA8K_MDIO_MASTER_READ | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
QCA8K_MDIO_MASTER_REG_ADDR(regnum); QCA8K_MDIO_MASTER_REG_ADDR(regnum);
qca8k_write(priv, QCA8K_MDIO_MASTER_CTRL, val); qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);
mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
ret = qca8k_set_page(bus, page);
if (ret)
goto exit;
qca8k_mii_write32(bus, 0x10 | r2, r1, val);
ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
QCA8K_MDIO_MASTER_BUSY);
if (ret)
goto exit;
if (qca8k_busy_wait(priv, QCA8K_MDIO_MASTER_CTRL, val = qca8k_mii_read32(bus, 0x10 | r2, r1);
QCA8K_MDIO_MASTER_BUSY))
return -ETIMEDOUT;
val = (qca8k_read(priv, QCA8K_MDIO_MASTER_CTRL) & exit:
QCA8K_MDIO_MASTER_DATA_MASK); /* even if the busy_wait timeouts try to clear the MASTER_EN */
qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
mutex_unlock(&bus->mdio_lock);
if (val >= 0)
val &= QCA8K_MDIO_MASTER_DATA_MASK;
return val; return val;
} }
...@@ -611,7 +740,14 @@ qca8k_phy_write(struct dsa_switch *ds, int port, int regnum, u16 data) ...@@ -611,7 +740,14 @@ qca8k_phy_write(struct dsa_switch *ds, int port, int regnum, u16 data)
{ {
struct qca8k_priv *priv = ds->priv; struct qca8k_priv *priv = ds->priv;
return qca8k_mdio_write(priv, port, regnum, data); /* Check if the legacy mapping should be used and the
* port is not correctly mapped to the right PHY in the
* devicetree
*/
if (priv->legacy_phy_port_mapping)
port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
return qca8k_mdio_write(priv->bus, port, regnum, data);
} }
static int static int
...@@ -620,7 +756,14 @@ qca8k_phy_read(struct dsa_switch *ds, int port, int regnum) ...@@ -620,7 +756,14 @@ qca8k_phy_read(struct dsa_switch *ds, int port, int regnum)
struct qca8k_priv *priv = ds->priv; struct qca8k_priv *priv = ds->priv;
int ret; int ret;
ret = qca8k_mdio_read(priv, port, regnum); /* Check if the legacy mapping should be used and the
* port is not correctly mapped to the right PHY in the
* devicetree
*/
if (priv->legacy_phy_port_mapping)
port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
ret = qca8k_mdio_read(priv->bus, port, regnum);
if (ret < 0) if (ret < 0)
return 0xffff; return 0xffff;
...@@ -628,14 +771,44 @@ qca8k_phy_read(struct dsa_switch *ds, int port, int regnum) ...@@ -628,14 +771,44 @@ qca8k_phy_read(struct dsa_switch *ds, int port, int regnum)
return ret; return ret;
} }
static int
qca8k_mdio_register(struct qca8k_priv *priv, struct device_node *mdio)
{
struct dsa_switch *ds = priv->ds;
struct mii_bus *bus;
bus = devm_mdiobus_alloc(ds->dev);
if (!bus)
return -ENOMEM;
bus->priv = (void *)priv;
bus->name = "qca8k slave mii";
bus->read = qca8k_mdio_read;
bus->write = qca8k_mdio_write;
snprintf(bus->id, MII_BUS_ID_SIZE, "qca8k-%d",
ds->index);
bus->parent = ds->dev;
bus->phy_mask = ~ds->phys_mii_mask;
ds->slave_mii_bus = bus;
return devm_of_mdiobus_register(priv->dev, bus, mdio);
}
static int static int
qca8k_setup_mdio_bus(struct qca8k_priv *priv) qca8k_setup_mdio_bus(struct qca8k_priv *priv)
{ {
u32 internal_mdio_mask = 0, external_mdio_mask = 0, reg; u32 internal_mdio_mask = 0, external_mdio_mask = 0, reg;
struct device_node *ports, *port; struct device_node *ports, *port, *mdio;
phy_interface_t mode;
int err; int err;
ports = of_get_child_by_name(priv->dev->of_node, "ports"); ports = of_get_child_by_name(priv->dev->of_node, "ports");
if (!ports)
ports = of_get_child_by_name(priv->dev->of_node, "ethernet-ports");
if (!ports) if (!ports)
return -EINVAL; return -EINVAL;
...@@ -650,7 +823,10 @@ qca8k_setup_mdio_bus(struct qca8k_priv *priv) ...@@ -650,7 +823,10 @@ qca8k_setup_mdio_bus(struct qca8k_priv *priv)
if (!dsa_is_user_port(priv->ds, reg)) if (!dsa_is_user_port(priv->ds, reg))
continue; continue;
if (of_property_read_bool(port, "phy-handle")) of_get_phy_mode(port, &mode);
if (of_property_read_bool(port, "phy-handle") &&
mode != PHY_INTERFACE_MODE_INTERNAL)
external_mdio_mask |= BIT(reg); external_mdio_mask |= BIT(reg);
else else
internal_mdio_mask |= BIT(reg); internal_mdio_mask |= BIT(reg);
...@@ -683,13 +859,89 @@ qca8k_setup_mdio_bus(struct qca8k_priv *priv) ...@@ -683,13 +859,89 @@ qca8k_setup_mdio_bus(struct qca8k_priv *priv)
* a dt-overlay and driver reload changed the configuration * a dt-overlay and driver reload changed the configuration
*/ */
qca8k_reg_clear(priv, QCA8K_MDIO_MASTER_CTRL, return qca8k_reg_clear(priv, QCA8K_MDIO_MASTER_CTRL,
QCA8K_MDIO_MASTER_EN); QCA8K_MDIO_MASTER_EN);
return 0; }
/* Check if the devicetree declare the port:phy mapping */
mdio = of_get_child_by_name(priv->dev->of_node, "mdio");
if (of_device_is_available(mdio)) {
err = qca8k_mdio_register(priv, mdio);
if (err)
of_node_put(mdio);
return err;
} }
/* If a mapping can't be found the legacy mapping is used,
* using the qca8k_port_to_phy function
*/
priv->legacy_phy_port_mapping = true;
priv->ops.phy_read = qca8k_phy_read; priv->ops.phy_read = qca8k_phy_read;
priv->ops.phy_write = qca8k_phy_write; priv->ops.phy_write = qca8k_phy_write;
return 0;
}
static int
qca8k_setup_of_rgmii_delay(struct qca8k_priv *priv)
{
struct device_node *port_dn;
phy_interface_t mode;
struct dsa_port *dp;
u32 val;
/* CPU port is already checked */
dp = dsa_to_port(priv->ds, 0);
port_dn = dp->dn;
/* Check if port 0 is set to the correct type */
of_get_phy_mode(port_dn, &mode);
if (mode != PHY_INTERFACE_MODE_RGMII_ID &&
mode != PHY_INTERFACE_MODE_RGMII_RXID &&
mode != PHY_INTERFACE_MODE_RGMII_TXID) {
return 0;
}
switch (mode) {
case PHY_INTERFACE_MODE_RGMII_ID:
case PHY_INTERFACE_MODE_RGMII_RXID:
if (of_property_read_u32(port_dn, "rx-internal-delay-ps", &val))
val = 2;
else
/* Switch regs accept value in ns, convert ps to ns */
val = val / 1000;
if (val > QCA8K_MAX_DELAY) {
dev_err(priv->dev, "rgmii rx delay is limited to a max value of 3ns, setting to the max value");
val = 3;
}
priv->rgmii_rx_delay = val;
/* Stop here if we need to check only for rx delay */
if (mode != PHY_INTERFACE_MODE_RGMII_ID)
break;
fallthrough;
case PHY_INTERFACE_MODE_RGMII_TXID:
if (of_property_read_u32(port_dn, "tx-internal-delay-ps", &val))
val = 1;
else
/* Switch regs accept value in ns, convert ps to ns */
val = val / 1000;
if (val > QCA8K_MAX_DELAY) {
dev_err(priv->dev, "rgmii tx delay is limited to a max value of 3ns, setting to the max value");
val = 3;
}
priv->rgmii_tx_delay = val;
break;
default:
return 0;
}
return 0; return 0;
} }
...@@ -698,10 +950,11 @@ qca8k_setup(struct dsa_switch *ds) ...@@ -698,10 +950,11 @@ qca8k_setup(struct dsa_switch *ds)
{ {
struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
int ret, i; int ret, i;
u32 mask;
/* Make sure that port 0 is the cpu port */ /* Make sure that port 0 is the cpu port */
if (!dsa_is_cpu_port(ds, 0)) { if (!dsa_is_cpu_port(ds, 0)) {
pr_err("port 0 is not the CPU port\n"); dev_err(priv->dev, "port 0 is not the CPU port");
return -EINVAL; return -EINVAL;
} }
...@@ -711,76 +964,163 @@ qca8k_setup(struct dsa_switch *ds) ...@@ -711,76 +964,163 @@ qca8k_setup(struct dsa_switch *ds)
priv->regmap = devm_regmap_init(ds->dev, NULL, priv, priv->regmap = devm_regmap_init(ds->dev, NULL, priv,
&qca8k_regmap_config); &qca8k_regmap_config);
if (IS_ERR(priv->regmap)) if (IS_ERR(priv->regmap))
pr_warn("regmap initialization failed"); dev_warn(priv->dev, "regmap initialization failed");
ret = qca8k_setup_mdio_bus(priv); ret = qca8k_setup_mdio_bus(priv);
if (ret) if (ret)
return ret; return ret;
ret = qca8k_setup_of_rgmii_delay(priv);
if (ret)
return ret;
/* Enable CPU Port */ /* Enable CPU Port */
qca8k_reg_set(priv, QCA8K_REG_GLOBAL_FW_CTRL0, ret = qca8k_reg_set(priv, QCA8K_REG_GLOBAL_FW_CTRL0,
QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN); QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN);
if (ret) {
dev_err(priv->dev, "failed enabling CPU port");
return ret;
}
/* Enable MIB counters */ /* Enable MIB counters */
qca8k_mib_init(priv); ret = qca8k_mib_init(priv);
if (ret)
dev_warn(priv->dev, "mib init failed");
/* Enable QCA header mode on the cpu port */ /* Enable QCA header mode on the cpu port */
qca8k_write(priv, QCA8K_REG_PORT_HDR_CTRL(QCA8K_CPU_PORT), ret = qca8k_write(priv, QCA8K_REG_PORT_HDR_CTRL(QCA8K_CPU_PORT),
QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_TX_S | QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_TX_S |
QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_RX_S); QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_RX_S);
if (ret) {
dev_err(priv->dev, "failed enabling QCA header mode");
return ret;
}
/* Disable forwarding by default on all ports */ /* Disable forwarding by default on all ports */
for (i = 0; i < QCA8K_NUM_PORTS; i++) for (i = 0; i < QCA8K_NUM_PORTS; i++) {
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
QCA8K_PORT_LOOKUP_MEMBER, 0); QCA8K_PORT_LOOKUP_MEMBER, 0);
if (ret)
return ret;
}
/* Disable MAC by default on all ports */ /* Disable MAC by default on all ports */
for (i = 1; i < QCA8K_NUM_PORTS; i++) for (i = 1; i < QCA8K_NUM_PORTS; i++)
qca8k_port_set_status(priv, i, 0); qca8k_port_set_status(priv, i, 0);
/* Forward all unknown frames to CPU port for Linux processing */ /* Forward all unknown frames to CPU port for Linux processing */
qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1, ret = qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1,
BIT(0) << QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_S | BIT(0) << QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_S |
BIT(0) << QCA8K_GLOBAL_FW_CTRL1_BC_DP_S | BIT(0) << QCA8K_GLOBAL_FW_CTRL1_BC_DP_S |
BIT(0) << QCA8K_GLOBAL_FW_CTRL1_MC_DP_S | BIT(0) << QCA8K_GLOBAL_FW_CTRL1_MC_DP_S |
BIT(0) << QCA8K_GLOBAL_FW_CTRL1_UC_DP_S); BIT(0) << QCA8K_GLOBAL_FW_CTRL1_UC_DP_S);
if (ret)
return ret;
/* Setup connection between CPU port & user ports */ /* Setup connection between CPU port & user ports */
for (i = 0; i < QCA8K_NUM_PORTS; i++) { for (i = 0; i < QCA8K_NUM_PORTS; i++) {
/* CPU port gets connected to all user ports of the switch */ /* CPU port gets connected to all user ports of the switch */
if (dsa_is_cpu_port(ds, i)) { if (dsa_is_cpu_port(ds, i)) {
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(QCA8K_CPU_PORT), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(QCA8K_CPU_PORT),
QCA8K_PORT_LOOKUP_MEMBER, dsa_user_ports(ds)); QCA8K_PORT_LOOKUP_MEMBER, dsa_user_ports(ds));
if (ret)
return ret;
} }
/* Individual user ports get connected to CPU port only */ /* Individual user ports get connected to CPU port only */
if (dsa_is_user_port(ds, i)) { if (dsa_is_user_port(ds, i)) {
int shift = 16 * (i % 2); int shift = 16 * (i % 2);
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
QCA8K_PORT_LOOKUP_MEMBER, QCA8K_PORT_LOOKUP_MEMBER,
BIT(QCA8K_CPU_PORT)); BIT(QCA8K_CPU_PORT));
if (ret)
return ret;
/* Enable ARP Auto-learning by default */ /* Enable ARP Auto-learning by default */
qca8k_reg_set(priv, QCA8K_PORT_LOOKUP_CTRL(i), ret = qca8k_reg_set(priv, QCA8K_PORT_LOOKUP_CTRL(i),
QCA8K_PORT_LOOKUP_LEARN); QCA8K_PORT_LOOKUP_LEARN);
if (ret)
return ret;
/* For port based vlans to work we need to set the /* For port based vlans to work we need to set the
* default egress vid * default egress vid
*/ */
qca8k_rmw(priv, QCA8K_EGRESS_VLAN(i), ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(i),
0xfff << shift, 0xfff << shift,
QCA8K_PORT_VID_DEF << shift); QCA8K_PORT_VID_DEF << shift);
qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(i), if (ret)
QCA8K_PORT_VLAN_CVID(QCA8K_PORT_VID_DEF) | return ret;
QCA8K_PORT_VLAN_SVID(QCA8K_PORT_VID_DEF));
ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(i),
QCA8K_PORT_VLAN_CVID(QCA8K_PORT_VID_DEF) |
QCA8K_PORT_VLAN_SVID(QCA8K_PORT_VID_DEF));
if (ret)
return ret;
} }
} }
/* The port 5 of the qca8337 have some problem in flood condition. The
* original legacy driver had some specific buffer and priority settings
* for the different port suggested by the QCA switch team. Add this
* missing settings to improve switch stability under load condition.
* This problem is limited to qca8337 and other qca8k switch are not affected.
*/
if (priv->switch_id == QCA8K_ID_QCA8337) {
for (i = 0; i < QCA8K_NUM_PORTS; i++) {
switch (i) {
/* The 2 CPU port and port 5 requires some different
* priority than any other ports.
*/
case 0:
case 5:
case 6:
mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x4) |
QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x4) |
QCA8K_PORT_HOL_CTRL0_EG_PRI4(0x6) |
QCA8K_PORT_HOL_CTRL0_EG_PRI5(0x8) |
QCA8K_PORT_HOL_CTRL0_EG_PORT(0x1e);
break;
default:
mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x6) |
QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x8) |
QCA8K_PORT_HOL_CTRL0_EG_PORT(0x19);
}
qca8k_write(priv, QCA8K_REG_PORT_HOL_CTRL0(i), mask);
mask = QCA8K_PORT_HOL_CTRL1_ING(0x6) |
QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
QCA8K_PORT_HOL_CTRL1_WRED_EN;
qca8k_rmw(priv, QCA8K_REG_PORT_HOL_CTRL1(i),
QCA8K_PORT_HOL_CTRL1_ING_BUF |
QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
QCA8K_PORT_HOL_CTRL1_WRED_EN,
mask);
}
}
/* Special GLOBAL_FC_THRESH value are needed for ar8327 switch */
if (priv->switch_id == QCA8K_ID_QCA8327) {
mask = QCA8K_GLOBAL_FC_GOL_XON_THRES(288) |
QCA8K_GLOBAL_FC_GOL_XOFF_THRES(496);
qca8k_rmw(priv, QCA8K_REG_GLOBAL_FC_THRESH,
QCA8K_GLOBAL_FC_GOL_XON_THRES_S |
QCA8K_GLOBAL_FC_GOL_XOFF_THRES_S,
mask);
}
/* Setup our port MTUs to match power on defaults */ /* Setup our port MTUs to match power on defaults */
for (i = 0; i < QCA8K_NUM_PORTS; i++) for (i = 0; i < QCA8K_NUM_PORTS; i++)
priv->port_mtu[i] = ETH_FRAME_LEN + ETH_FCS_LEN; priv->port_mtu[i] = ETH_FRAME_LEN + ETH_FCS_LEN;
qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, ETH_FRAME_LEN + ETH_FCS_LEN); ret = qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, ETH_FRAME_LEN + ETH_FCS_LEN);
if (ret)
dev_warn(priv->dev, "failed setting MTU settings");
/* Flush the FDB table */ /* Flush the FDB table */
qca8k_fdb_flush(priv); qca8k_fdb_flush(priv);
...@@ -802,6 +1142,8 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode, ...@@ -802,6 +1142,8 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
case 0: /* 1st CPU port */ case 0: /* 1st CPU port */
if (state->interface != PHY_INTERFACE_MODE_RGMII && if (state->interface != PHY_INTERFACE_MODE_RGMII &&
state->interface != PHY_INTERFACE_MODE_RGMII_ID && state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
state->interface != PHY_INTERFACE_MODE_SGMII) state->interface != PHY_INTERFACE_MODE_SGMII)
return; return;
...@@ -817,6 +1159,8 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode, ...@@ -817,6 +1159,8 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
case 6: /* 2nd CPU port / external PHY */ case 6: /* 2nd CPU port / external PHY */
if (state->interface != PHY_INTERFACE_MODE_RGMII && if (state->interface != PHY_INTERFACE_MODE_RGMII &&
state->interface != PHY_INTERFACE_MODE_RGMII_ID && state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
state->interface != PHY_INTERFACE_MODE_SGMII && state->interface != PHY_INTERFACE_MODE_SGMII &&
state->interface != PHY_INTERFACE_MODE_1000BASEX) state->interface != PHY_INTERFACE_MODE_1000BASEX)
return; return;
...@@ -840,16 +1184,22 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode, ...@@ -840,16 +1184,22 @@ qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN); qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN);
break; break;
case PHY_INTERFACE_MODE_RGMII_ID: case PHY_INTERFACE_MODE_RGMII_ID:
case PHY_INTERFACE_MODE_RGMII_TXID:
case PHY_INTERFACE_MODE_RGMII_RXID:
/* RGMII_ID needs internal delay. This is enabled through /* RGMII_ID needs internal delay. This is enabled through
* PORT5_PAD_CTRL for all ports, rather than individual port * PORT5_PAD_CTRL for all ports, rather than individual port
* registers * registers
*/ */
qca8k_write(priv, reg, qca8k_write(priv, reg,
QCA8K_PORT_PAD_RGMII_EN | QCA8K_PORT_PAD_RGMII_EN |
QCA8K_PORT_PAD_RGMII_TX_DELAY(QCA8K_MAX_DELAY) | QCA8K_PORT_PAD_RGMII_TX_DELAY(priv->rgmii_tx_delay) |
QCA8K_PORT_PAD_RGMII_RX_DELAY(QCA8K_MAX_DELAY)); QCA8K_PORT_PAD_RGMII_RX_DELAY(priv->rgmii_rx_delay) |
qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL, QCA8K_PORT_PAD_RGMII_TX_DELAY_EN |
QCA8K_PORT_PAD_RGMII_RX_DELAY_EN); QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
/* QCA8337 requires to set rgmii rx delay */
if (priv->switch_id == QCA8K_ID_QCA8337)
qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL,
QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
break; break;
case PHY_INTERFACE_MODE_SGMII: case PHY_INTERFACE_MODE_SGMII:
case PHY_INTERFACE_MODE_1000BASEX: case PHY_INTERFACE_MODE_1000BASEX:
...@@ -903,6 +1253,8 @@ qca8k_phylink_validate(struct dsa_switch *ds, int port, ...@@ -903,6 +1253,8 @@ qca8k_phylink_validate(struct dsa_switch *ds, int port,
if (state->interface != PHY_INTERFACE_MODE_NA && if (state->interface != PHY_INTERFACE_MODE_NA &&
state->interface != PHY_INTERFACE_MODE_RGMII && state->interface != PHY_INTERFACE_MODE_RGMII &&
state->interface != PHY_INTERFACE_MODE_RGMII_ID && state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
state->interface != PHY_INTERFACE_MODE_SGMII) state->interface != PHY_INTERFACE_MODE_SGMII)
goto unsupported; goto unsupported;
break; break;
...@@ -913,13 +1265,16 @@ qca8k_phylink_validate(struct dsa_switch *ds, int port, ...@@ -913,13 +1265,16 @@ qca8k_phylink_validate(struct dsa_switch *ds, int port,
case 5: case 5:
/* Internal PHY */ /* Internal PHY */
if (state->interface != PHY_INTERFACE_MODE_NA && if (state->interface != PHY_INTERFACE_MODE_NA &&
state->interface != PHY_INTERFACE_MODE_GMII) state->interface != PHY_INTERFACE_MODE_GMII &&
state->interface != PHY_INTERFACE_MODE_INTERNAL)
goto unsupported; goto unsupported;
break; break;
case 6: /* 2nd CPU port / external PHY */ case 6: /* 2nd CPU port / external PHY */
if (state->interface != PHY_INTERFACE_MODE_NA && if (state->interface != PHY_INTERFACE_MODE_NA &&
state->interface != PHY_INTERFACE_MODE_RGMII && state->interface != PHY_INTERFACE_MODE_RGMII &&
state->interface != PHY_INTERFACE_MODE_RGMII_ID && state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
state->interface != PHY_INTERFACE_MODE_SGMII && state->interface != PHY_INTERFACE_MODE_SGMII &&
state->interface != PHY_INTERFACE_MODE_1000BASEX) state->interface != PHY_INTERFACE_MODE_1000BASEX)
goto unsupported; goto unsupported;
...@@ -957,6 +1312,8 @@ qca8k_phylink_mac_link_state(struct dsa_switch *ds, int port, ...@@ -957,6 +1312,8 @@ qca8k_phylink_mac_link_state(struct dsa_switch *ds, int port,
u32 reg; u32 reg;
reg = qca8k_read(priv, QCA8K_REG_PORT_STATUS(port)); reg = qca8k_read(priv, QCA8K_REG_PORT_STATUS(port));
if (reg < 0)
return reg;
state->link = !!(reg & QCA8K_PORT_STATUS_LINK_UP); state->link = !!(reg & QCA8K_PORT_STATUS_LINK_UP);
state->an_complete = state->link; state->an_complete = state->link;
...@@ -1057,18 +1414,26 @@ qca8k_get_ethtool_stats(struct dsa_switch *ds, int port, ...@@ -1057,18 +1414,26 @@ qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
{ {
struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
const struct qca8k_mib_desc *mib; const struct qca8k_mib_desc *mib;
u32 reg, i; u32 reg, i, val;
u64 hi; u64 hi;
for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++) { for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++) {
mib = &ar8327_mib[i]; mib = &ar8327_mib[i];
reg = QCA8K_PORT_MIB_COUNTER(port) + mib->offset; reg = QCA8K_PORT_MIB_COUNTER(port) + mib->offset;
data[i] = qca8k_read(priv, reg); val = qca8k_read(priv, reg);
if (val < 0)
continue;
if (mib->size == 2) { if (mib->size == 2) {
hi = qca8k_read(priv, reg + 4); hi = qca8k_read(priv, reg + 4);
data[i] |= hi << 32; if (hi < 0)
continue;
} }
data[i] = val;
if (mib->size == 2)
data[i] |= hi << 32;
} }
} }
...@@ -1087,17 +1452,24 @@ qca8k_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *eee) ...@@ -1087,17 +1452,24 @@ qca8k_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *eee)
struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
u32 lpi_en = QCA8K_REG_EEE_CTRL_LPI_EN(port); u32 lpi_en = QCA8K_REG_EEE_CTRL_LPI_EN(port);
u32 reg; u32 reg;
int ret;
mutex_lock(&priv->reg_mutex); mutex_lock(&priv->reg_mutex);
reg = qca8k_read(priv, QCA8K_REG_EEE_CTRL); reg = qca8k_read(priv, QCA8K_REG_EEE_CTRL);
if (reg < 0) {
ret = reg;
goto exit;
}
if (eee->eee_enabled) if (eee->eee_enabled)
reg |= lpi_en; reg |= lpi_en;
else else
reg &= ~lpi_en; reg &= ~lpi_en;
qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg); ret = qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg);
mutex_unlock(&priv->reg_mutex);
return 0; exit:
mutex_unlock(&priv->reg_mutex);
return ret;
} }
static int static int
...@@ -1141,7 +1513,7 @@ qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br) ...@@ -1141,7 +1513,7 @@ qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br)
{ {
struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv; struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
int port_mask = BIT(QCA8K_CPU_PORT); int port_mask = BIT(QCA8K_CPU_PORT);
int i; int i, ret;
for (i = 1; i < QCA8K_NUM_PORTS; i++) { for (i = 1; i < QCA8K_NUM_PORTS; i++) {
if (dsa_to_port(ds, i)->bridge_dev != br) if (dsa_to_port(ds, i)->bridge_dev != br)
...@@ -1149,17 +1521,20 @@ qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br) ...@@ -1149,17 +1521,20 @@ qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br)
/* Add this port to the portvlan mask of the other ports /* Add this port to the portvlan mask of the other ports
* in the bridge * in the bridge
*/ */
qca8k_reg_set(priv, ret = qca8k_reg_set(priv,
QCA8K_PORT_LOOKUP_CTRL(i), QCA8K_PORT_LOOKUP_CTRL(i),
BIT(port)); BIT(port));
if (ret)
return ret;
if (i != port) if (i != port)
port_mask |= BIT(i); port_mask |= BIT(i);
} }
/* Add all other ports to this ports portvlan mask */ /* Add all other ports to this ports portvlan mask */
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
QCA8K_PORT_LOOKUP_MEMBER, port_mask); QCA8K_PORT_LOOKUP_MEMBER, port_mask);
return 0; return ret;
} }
static void static void
...@@ -1223,9 +1598,7 @@ qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu) ...@@ -1223,9 +1598,7 @@ qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
mtu = priv->port_mtu[i]; mtu = priv->port_mtu[i];
/* Include L2 header / FCS length */ /* Include L2 header / FCS length */
qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, mtu + ETH_HLEN + ETH_FCS_LEN); return qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, mtu + ETH_HLEN + ETH_FCS_LEN);
return 0;
} }
static int static int
...@@ -1298,18 +1671,19 @@ qca8k_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering, ...@@ -1298,18 +1671,19 @@ qca8k_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
struct qca8k_priv *priv = ds->priv; struct qca8k_priv *priv = ds->priv;
int ret;
if (vlan_filtering) { if (vlan_filtering) {
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
QCA8K_PORT_LOOKUP_VLAN_MODE, QCA8K_PORT_LOOKUP_VLAN_MODE,
QCA8K_PORT_LOOKUP_VLAN_MODE_SECURE); QCA8K_PORT_LOOKUP_VLAN_MODE_SECURE);
} else { } else {
qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port), ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
QCA8K_PORT_LOOKUP_VLAN_MODE, QCA8K_PORT_LOOKUP_VLAN_MODE,
QCA8K_PORT_LOOKUP_VLAN_MODE_NONE); QCA8K_PORT_LOOKUP_VLAN_MODE_NONE);
} }
return 0; return ret;
} }
static int static int
...@@ -1320,7 +1694,7 @@ qca8k_port_vlan_add(struct dsa_switch *ds, int port, ...@@ -1320,7 +1694,7 @@ qca8k_port_vlan_add(struct dsa_switch *ds, int port,
bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
struct qca8k_priv *priv = ds->priv; struct qca8k_priv *priv = ds->priv;
int ret = 0; int ret;
ret = qca8k_vlan_add(priv, port, vlan->vid, untagged); ret = qca8k_vlan_add(priv, port, vlan->vid, untagged);
if (ret) { if (ret) {
...@@ -1331,14 +1705,17 @@ qca8k_port_vlan_add(struct dsa_switch *ds, int port, ...@@ -1331,14 +1705,17 @@ qca8k_port_vlan_add(struct dsa_switch *ds, int port,
if (pvid) { if (pvid) {
int shift = 16 * (port % 2); int shift = 16 * (port % 2);
qca8k_rmw(priv, QCA8K_EGRESS_VLAN(port), ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(port),
0xfff << shift, vlan->vid << shift); 0xfff << shift, vlan->vid << shift);
qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(port), if (ret)
QCA8K_PORT_VLAN_CVID(vlan->vid) | return ret;
QCA8K_PORT_VLAN_SVID(vlan->vid));
ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(port),
QCA8K_PORT_VLAN_CVID(vlan->vid) |
QCA8K_PORT_VLAN_SVID(vlan->vid));
} }
return 0; return ret;
} }
static int static int
...@@ -1346,7 +1723,7 @@ qca8k_port_vlan_del(struct dsa_switch *ds, int port, ...@@ -1346,7 +1723,7 @@ qca8k_port_vlan_del(struct dsa_switch *ds, int port,
const struct switchdev_obj_port_vlan *vlan) const struct switchdev_obj_port_vlan *vlan)
{ {
struct qca8k_priv *priv = ds->priv; struct qca8k_priv *priv = ds->priv;
int ret = 0; int ret;
ret = qca8k_vlan_del(priv, port, vlan->vid); ret = qca8k_vlan_del(priv, port, vlan->vid);
if (ret) if (ret)
...@@ -1355,6 +1732,22 @@ qca8k_port_vlan_del(struct dsa_switch *ds, int port, ...@@ -1355,6 +1732,22 @@ qca8k_port_vlan_del(struct dsa_switch *ds, int port,
return ret; return ret;
} }
static u32 qca8k_get_phy_flags(struct dsa_switch *ds, int port)
{
struct qca8k_priv *priv = ds->priv;
/* Communicate to the phy internal driver the switch revision.
* Based on the switch revision different values needs to be
* set to the dbg and mmd reg on the phy.
* The first 2 bit are used to communicate the switch revision
* to the phy driver.
*/
if (port > 0 && port < 6)
return priv->switch_revision;
return 0;
}
static enum dsa_tag_protocol static enum dsa_tag_protocol
qca8k_get_tag_protocol(struct dsa_switch *ds, int port, qca8k_get_tag_protocol(struct dsa_switch *ds, int port,
enum dsa_tag_protocol mp) enum dsa_tag_protocol mp)
...@@ -1388,13 +1781,43 @@ static const struct dsa_switch_ops qca8k_switch_ops = { ...@@ -1388,13 +1781,43 @@ static const struct dsa_switch_ops qca8k_switch_ops = {
.phylink_mac_config = qca8k_phylink_mac_config, .phylink_mac_config = qca8k_phylink_mac_config,
.phylink_mac_link_down = qca8k_phylink_mac_link_down, .phylink_mac_link_down = qca8k_phylink_mac_link_down,
.phylink_mac_link_up = qca8k_phylink_mac_link_up, .phylink_mac_link_up = qca8k_phylink_mac_link_up,
.get_phy_flags = qca8k_get_phy_flags,
}; };
static int qca8k_read_switch_id(struct qca8k_priv *priv)
{
const struct qca8k_match_data *data;
u32 val;
u8 id;
/* get the switches ID from the compatible */
data = of_device_get_match_data(priv->dev);
if (!data)
return -ENODEV;
val = qca8k_read(priv, QCA8K_REG_MASK_CTRL);
if (val < 0)
return -ENODEV;
id = QCA8K_MASK_CTRL_DEVICE_ID(val & QCA8K_MASK_CTRL_DEVICE_ID_MASK);
if (id != data->id) {
dev_err(priv->dev, "Switch id detected %x but expected %x", id, data->id);
return -ENODEV;
}
priv->switch_id = id;
/* Save revision to communicate to the internal PHY driver */
priv->switch_revision = (val & QCA8K_MASK_CTRL_REV_ID_MASK);
return 0;
}
static int static int
qca8k_sw_probe(struct mdio_device *mdiodev) qca8k_sw_probe(struct mdio_device *mdiodev)
{ {
struct qca8k_priv *priv; struct qca8k_priv *priv;
u32 id; int ret;
/* allocate the private data struct so that we can probe the switches /* allocate the private data struct so that we can probe the switches
* ID register * ID register
...@@ -1420,12 +1843,10 @@ qca8k_sw_probe(struct mdio_device *mdiodev) ...@@ -1420,12 +1843,10 @@ qca8k_sw_probe(struct mdio_device *mdiodev)
gpiod_set_value_cansleep(priv->reset_gpio, 0); gpiod_set_value_cansleep(priv->reset_gpio, 0);
} }
/* read the switches ID register */ /* Check the detected switch id */
id = qca8k_read(priv, QCA8K_REG_MASK_CTRL); ret = qca8k_read_switch_id(priv);
id >>= QCA8K_MASK_CTRL_ID_S; if (ret)
id &= QCA8K_MASK_CTRL_ID_M; return ret;
if (id != QCA8K_ID_QCA8337)
return -ENODEV;
priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL); priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
if (!priv->ds) if (!priv->ds)
...@@ -1490,9 +1911,18 @@ static int qca8k_resume(struct device *dev) ...@@ -1490,9 +1911,18 @@ static int qca8k_resume(struct device *dev)
static SIMPLE_DEV_PM_OPS(qca8k_pm_ops, static SIMPLE_DEV_PM_OPS(qca8k_pm_ops,
qca8k_suspend, qca8k_resume); qca8k_suspend, qca8k_resume);
static const struct qca8k_match_data qca832x = {
.id = QCA8K_ID_QCA8327,
};
static const struct qca8k_match_data qca833x = {
.id = QCA8K_ID_QCA8337,
};
static const struct of_device_id qca8k_of_match[] = { static const struct of_device_id qca8k_of_match[] = {
{ .compatible = "qca,qca8334" }, { .compatible = "qca,qca8327", .data = &qca832x },
{ .compatible = "qca,qca8337" }, { .compatible = "qca,qca8334", .data = &qca833x },
{ .compatible = "qca,qca8337", .data = &qca833x },
{ /* sentinel */ }, { /* sentinel */ },
}; };
......
...@@ -15,9 +15,13 @@ ...@@ -15,9 +15,13 @@
#define QCA8K_NUM_PORTS 7 #define QCA8K_NUM_PORTS 7
#define QCA8K_MAX_MTU 9000 #define QCA8K_MAX_MTU 9000
#define PHY_ID_QCA8327 0x004dd034
#define QCA8K_ID_QCA8327 0x12
#define PHY_ID_QCA8337 0x004dd036 #define PHY_ID_QCA8337 0x004dd036
#define QCA8K_ID_QCA8337 0x13 #define QCA8K_ID_QCA8337 0x13
#define QCA8K_BUSY_WAIT_TIMEOUT 2000
#define QCA8K_NUM_FDB_RECORDS 2048 #define QCA8K_NUM_FDB_RECORDS 2048
#define QCA8K_CPU_PORT 0 #define QCA8K_CPU_PORT 0
...@@ -26,18 +30,19 @@ ...@@ -26,18 +30,19 @@
/* Global control registers */ /* Global control registers */
#define QCA8K_REG_MASK_CTRL 0x000 #define QCA8K_REG_MASK_CTRL 0x000
#define QCA8K_MASK_CTRL_ID_M 0xff #define QCA8K_MASK_CTRL_REV_ID_MASK GENMASK(7, 0)
#define QCA8K_MASK_CTRL_ID_S 8 #define QCA8K_MASK_CTRL_REV_ID(x) ((x) >> 0)
#define QCA8K_MASK_CTRL_DEVICE_ID_MASK GENMASK(15, 8)
#define QCA8K_MASK_CTRL_DEVICE_ID(x) ((x) >> 8)
#define QCA8K_REG_PORT0_PAD_CTRL 0x004 #define QCA8K_REG_PORT0_PAD_CTRL 0x004
#define QCA8K_REG_PORT5_PAD_CTRL 0x008 #define QCA8K_REG_PORT5_PAD_CTRL 0x008
#define QCA8K_REG_PORT6_PAD_CTRL 0x00c #define QCA8K_REG_PORT6_PAD_CTRL 0x00c
#define QCA8K_PORT_PAD_RGMII_EN BIT(26) #define QCA8K_PORT_PAD_RGMII_EN BIT(26)
#define QCA8K_PORT_PAD_RGMII_TX_DELAY(x) \ #define QCA8K_PORT_PAD_RGMII_TX_DELAY(x) ((x) << 22)
((0x8 + (x & 0x3)) << 22) #define QCA8K_PORT_PAD_RGMII_RX_DELAY(x) ((x) << 20)
#define QCA8K_PORT_PAD_RGMII_RX_DELAY(x) \ #define QCA8K_PORT_PAD_RGMII_TX_DELAY_EN BIT(25)
((0x10 + (x & 0x3)) << 20)
#define QCA8K_MAX_DELAY 3
#define QCA8K_PORT_PAD_RGMII_RX_DELAY_EN BIT(24) #define QCA8K_PORT_PAD_RGMII_RX_DELAY_EN BIT(24)
#define QCA8K_MAX_DELAY 3
#define QCA8K_PORT_PAD_SGMII_EN BIT(7) #define QCA8K_PORT_PAD_SGMII_EN BIT(7)
#define QCA8K_REG_PWS 0x010 #define QCA8K_REG_PWS 0x010
#define QCA8K_PWS_SERDES_AEN_DIS BIT(7) #define QCA8K_PWS_SERDES_AEN_DIS BIT(7)
...@@ -164,6 +169,36 @@ ...@@ -164,6 +169,36 @@
#define QCA8K_PORT_LOOKUP_STATE GENMASK(18, 16) #define QCA8K_PORT_LOOKUP_STATE GENMASK(18, 16)
#define QCA8K_PORT_LOOKUP_LEARN BIT(20) #define QCA8K_PORT_LOOKUP_LEARN BIT(20)
#define QCA8K_REG_GLOBAL_FC_THRESH 0x800
#define QCA8K_GLOBAL_FC_GOL_XON_THRES(x) ((x) << 16)
#define QCA8K_GLOBAL_FC_GOL_XON_THRES_S GENMASK(24, 16)
#define QCA8K_GLOBAL_FC_GOL_XOFF_THRES(x) ((x) << 0)
#define QCA8K_GLOBAL_FC_GOL_XOFF_THRES_S GENMASK(8, 0)
#define QCA8K_REG_PORT_HOL_CTRL0(_i) (0x970 + (_i) * 0x8)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI0_BUF GENMASK(3, 0)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI0(x) ((x) << 0)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI1_BUF GENMASK(7, 4)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI1(x) ((x) << 4)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI2_BUF GENMASK(11, 8)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI2(x) ((x) << 8)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI3_BUF GENMASK(15, 12)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI3(x) ((x) << 12)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI4_BUF GENMASK(19, 16)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI4(x) ((x) << 16)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI5_BUF GENMASK(23, 20)
#define QCA8K_PORT_HOL_CTRL0_EG_PRI5(x) ((x) << 20)
#define QCA8K_PORT_HOL_CTRL0_EG_PORT_BUF GENMASK(29, 24)
#define QCA8K_PORT_HOL_CTRL0_EG_PORT(x) ((x) << 24)
#define QCA8K_REG_PORT_HOL_CTRL1(_i) (0x974 + (_i) * 0x8)
#define QCA8K_PORT_HOL_CTRL1_ING_BUF GENMASK(3, 0)
#define QCA8K_PORT_HOL_CTRL1_ING(x) ((x) << 0)
#define QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN BIT(6)
#define QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN BIT(7)
#define QCA8K_PORT_HOL_CTRL1_WRED_EN BIT(8)
#define QCA8K_PORT_HOL_CTRL1_EG_MIRROR_EN BIT(16)
/* Pkt edit registers */ /* Pkt edit registers */
#define QCA8K_EGRESS_VLAN(x) (0x0c70 + (4 * (x / 2))) #define QCA8K_EGRESS_VLAN(x) (0x0c70 + (4 * (x / 2)))
...@@ -211,7 +246,16 @@ struct ar8xxx_port_status { ...@@ -211,7 +246,16 @@ struct ar8xxx_port_status {
int enabled; int enabled;
}; };
struct qca8k_match_data {
u8 id;
};
struct qca8k_priv { struct qca8k_priv {
u8 switch_id;
u8 switch_revision;
u8 rgmii_tx_delay;
u8 rgmii_rx_delay;
bool legacy_phy_port_mapping;
struct regmap *regmap; struct regmap *regmap;
struct mii_bus *bus; struct mii_bus *bus;
struct ar8xxx_port_status port_sts[QCA8K_NUM_PORTS]; struct ar8xxx_port_status port_sts[QCA8K_NUM_PORTS];
......
...@@ -247,10 +247,11 @@ config NXP_TJA11XX_PHY ...@@ -247,10 +247,11 @@ config NXP_TJA11XX_PHY
Currently supports the NXP TJA1100 and TJA1101 PHY. Currently supports the NXP TJA1100 and TJA1101 PHY.
config AT803X_PHY config AT803X_PHY
tristate "Qualcomm Atheros AR803X PHYs" tristate "Qualcomm Atheros AR803X PHYs and QCA833x PHYs"
depends on REGULATOR depends on REGULATOR
help help
Currently supports the AR8030, AR8031, AR8033 and AR8035 model Currently supports the AR8030, AR8031, AR8033, AR8035 and internal
QCA8337(Internal qca8k PHY) model
config QSEMI_PHY config QSEMI_PHY
tristate "Quality Semiconductor PHYs" tristate "Quality Semiconductor PHYs"
......
...@@ -83,8 +83,8 @@ ...@@ -83,8 +83,8 @@
#define AT803X_MODE_CFG_MASK 0x0F #define AT803X_MODE_CFG_MASK 0x0F
#define AT803X_MODE_CFG_SGMII 0x01 #define AT803X_MODE_CFG_SGMII 0x01
#define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/ #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
#define AT803X_PSSR_MR_AN_COMPLETE 0x0200 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
#define AT803X_DEBUG_REG_0 0x00 #define AT803X_DEBUG_REG_0 0x00
#define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15) #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
...@@ -92,10 +92,16 @@ ...@@ -92,10 +92,16 @@
#define AT803X_DEBUG_REG_5 0x05 #define AT803X_DEBUG_REG_5 0x05
#define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8) #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
#define AT803X_DEBUG_REG_3C 0x3C
#define AT803X_DEBUG_REG_3D 0x3D
#define AT803X_DEBUG_REG_1F 0x1F #define AT803X_DEBUG_REG_1F 0x1F
#define AT803X_DEBUG_PLL_ON BIT(2) #define AT803X_DEBUG_PLL_ON BIT(2)
#define AT803X_DEBUG_RGMII_1V8 BIT(3) #define AT803X_DEBUG_RGMII_1V8 BIT(3)
#define MDIO_AZ_DEBUG 0x800D
/* AT803x supports either the XTAL input pad, an internal PLL or the /* AT803x supports either the XTAL input pad, an internal PLL or the
* DSP as clock reference for the clock output pad. The XTAL reference * DSP as clock reference for the clock output pad. The XTAL reference
* is only used for 25 MHz output, all other frequencies need the PLL. * is only used for 25 MHz output, all other frequencies need the PLL.
...@@ -128,33 +134,59 @@ ...@@ -128,33 +134,59 @@
#define AT803X_CLK_OUT_STRENGTH_HALF 1 #define AT803X_CLK_OUT_STRENGTH_HALF 1
#define AT803X_CLK_OUT_STRENGTH_QUARTER 2 #define AT803X_CLK_OUT_STRENGTH_QUARTER 2
#define AT803X_DEFAULT_DOWNSHIFT 5 #define AT803X_DEFAULT_DOWNSHIFT 5
#define AT803X_MIN_DOWNSHIFT 2 #define AT803X_MIN_DOWNSHIFT 2
#define AT803X_MAX_DOWNSHIFT 9 #define AT803X_MAX_DOWNSHIFT 9
#define AT803X_MMD3_SMARTEEE_CTL1 0x805b #define AT803X_MMD3_SMARTEEE_CTL1 0x805b
#define AT803X_MMD3_SMARTEEE_CTL2 0x805c #define AT803X_MMD3_SMARTEEE_CTL2 0x805c
#define AT803X_MMD3_SMARTEEE_CTL3 0x805d #define AT803X_MMD3_SMARTEEE_CTL3 0x805d
#define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8) #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8)
#define ATH9331_PHY_ID 0x004dd041 #define ATH9331_PHY_ID 0x004dd041
#define ATH8030_PHY_ID 0x004dd076 #define ATH8030_PHY_ID 0x004dd076
#define ATH8031_PHY_ID 0x004dd074 #define ATH8031_PHY_ID 0x004dd074
#define ATH8032_PHY_ID 0x004dd023 #define ATH8032_PHY_ID 0x004dd023
#define ATH8035_PHY_ID 0x004dd072 #define ATH8035_PHY_ID 0x004dd072
#define AT8030_PHY_ID_MASK 0xffffffef #define AT8030_PHY_ID_MASK 0xffffffef
#define AT803X_PAGE_FIBER 0 #define QCA8327_PHY_ID 0x004dd034
#define AT803X_PAGE_COPPER 1 #define QCA8337_PHY_ID 0x004dd036
#define QCA8K_PHY_ID_MASK 0xffffffff
#define QCA8K_DEVFLAGS_REVISION_MASK GENMASK(2, 0)
#define AT803X_PAGE_FIBER 0
#define AT803X_PAGE_COPPER 1
/* don't turn off internal PLL */
#define AT803X_KEEP_PLL_ENABLED BIT(0)
#define AT803X_DISABLE_SMARTEEE BIT(1)
MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver"); MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
MODULE_AUTHOR("Matus Ujhelyi"); MODULE_AUTHOR("Matus Ujhelyi");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
enum stat_access_type {
PHY,
MMD
};
struct at803x_hw_stat {
const char *string;
u8 reg;
u32 mask;
enum stat_access_type access_type;
};
static struct at803x_hw_stat at803x_hw_stats[] = {
{ "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
{ "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
{ "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
};
struct at803x_priv { struct at803x_priv {
int flags; int flags;
#define AT803X_KEEP_PLL_ENABLED BIT(0) /* don't turn off internal PLL */
#define AT803X_DISABLE_SMARTEEE BIT(1)
u16 clk_25m_reg; u16 clk_25m_reg;
u16 clk_25m_mask; u16 clk_25m_mask;
u8 smarteee_lpi_tw_1g; u8 smarteee_lpi_tw_1g;
...@@ -162,6 +194,7 @@ struct at803x_priv { ...@@ -162,6 +194,7 @@ struct at803x_priv {
struct regulator_dev *vddio_rdev; struct regulator_dev *vddio_rdev;
struct regulator_dev *vddh_rdev; struct regulator_dev *vddh_rdev;
struct regulator *vddio; struct regulator *vddio;
u64 stats[ARRAY_SIZE(at803x_hw_stats)];
}; };
struct at803x_context { struct at803x_context {
...@@ -173,6 +206,17 @@ struct at803x_context { ...@@ -173,6 +206,17 @@ struct at803x_context {
u16 led_control; u16 led_control;
}; };
static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
{
int ret;
ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
if (ret < 0)
return ret;
return phy_write(phydev, AT803X_DEBUG_DATA, data);
}
static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg) static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
{ {
int ret; int ret;
...@@ -335,6 +379,53 @@ static void at803x_get_wol(struct phy_device *phydev, ...@@ -335,6 +379,53 @@ static void at803x_get_wol(struct phy_device *phydev,
wol->wolopts |= WAKE_MAGIC; wol->wolopts |= WAKE_MAGIC;
} }
static int at803x_get_sset_count(struct phy_device *phydev)
{
return ARRAY_SIZE(at803x_hw_stats);
}
static void at803x_get_strings(struct phy_device *phydev, u8 *data)
{
int i;
for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
strscpy(data + i * ETH_GSTRING_LEN,
at803x_hw_stats[i].string, ETH_GSTRING_LEN);
}
}
static u64 at803x_get_stat(struct phy_device *phydev, int i)
{
struct at803x_hw_stat stat = at803x_hw_stats[i];
struct at803x_priv *priv = phydev->priv;
int val;
u64 ret;
if (stat.access_type == MMD)
val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
else
val = phy_read(phydev, stat.reg);
if (val < 0) {
ret = U64_MAX;
} else {
val = val & stat.mask;
priv->stats[i] += val;
ret = priv->stats[i];
}
return ret;
}
static void at803x_get_stats(struct phy_device *phydev,
struct ethtool_stats *stats, u64 *data)
{
int i;
for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
data[i] = at803x_get_stat(phydev, i);
}
static int at803x_suspend(struct phy_device *phydev) static int at803x_suspend(struct phy_device *phydev)
{ {
int value; int value;
...@@ -1170,6 +1261,34 @@ static int at803x_cable_test_start(struct phy_device *phydev) ...@@ -1170,6 +1261,34 @@ static int at803x_cable_test_start(struct phy_device *phydev)
return 0; return 0;
} }
static int qca83xx_config_init(struct phy_device *phydev)
{
u8 switch_revision;
switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
switch (switch_revision) {
case 1:
/* For 100M waveform */
at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_0, 0x02ea);
/* Turn on Gigabit clock */
at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x68a0);
break;
case 2:
phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
fallthrough;
case 4:
phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x6860);
at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_5, 0x2c46);
at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
break;
}
return 0;
}
static struct phy_driver at803x_driver[] = { static struct phy_driver at803x_driver[] = {
{ {
/* Qualcomm Atheros AR8035 */ /* Qualcomm Atheros AR8035 */
...@@ -1266,7 +1385,20 @@ static struct phy_driver at803x_driver[] = { ...@@ -1266,7 +1385,20 @@ static struct phy_driver at803x_driver[] = {
.read_status = at803x_read_status, .read_status = at803x_read_status,
.soft_reset = genphy_soft_reset, .soft_reset = genphy_soft_reset,
.config_aneg = at803x_config_aneg, .config_aneg = at803x_config_aneg,
} }; }, {
/* QCA8337 */
.phy_id = QCA8337_PHY_ID,
.phy_id_mask = QCA8K_PHY_ID_MASK,
.name = "QCA PHY 8337",
/* PHY_GBIT_FEATURES */
.probe = at803x_probe,
.flags = PHY_IS_INTERNAL,
.config_init = qca83xx_config_init,
.soft_reset = genphy_soft_reset,
.get_sset_count = at803x_get_sset_count,
.get_strings = at803x_get_strings,
.get_stats = at803x_get_stats,
}, };
module_phy_driver(at803x_driver); module_phy_driver(at803x_driver);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment