Commit fd250fed authored by Pieter Van Trappen's avatar Pieter Van Trappen Committed by Jakub Kicinski

net: dsa: microchip: generalize KSZ9477 WoL functions at ksz_common

Generalize KSZ9477 WoL functions at ksz_common. Move dedicated registers
and generic masks to existing structures & defines for that purpose.

Introduction of PME (port) read/write helper functions, which happen
to be the generic read/write for KSZ9477 but not for the incoming
KSZ87xx patch.
Signed-off-by: default avatarPieter Van Trappen <pieter.van.trappen@cern.ch>
Acked-by: default avatarArun Ramadoss <arun.ramadoss@microchip.com>
Link: https://patch.msgid.link/20240813142750.772781-4-vtpieter@gmail.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent f3ac6198
...@@ -1023,6 +1023,7 @@ void ksz9477_port_queue_split(struct ksz_device *dev, int port) ...@@ -1023,6 +1023,7 @@ void ksz9477_port_queue_split(struct ksz_device *dev, int port)
void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port) void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
{ {
const u16 *regs = dev->info->regs;
struct dsa_switch *ds = dev->ds; struct dsa_switch *ds = dev->ds;
u16 data16; u16 data16;
u8 member; u8 member;
...@@ -1067,12 +1068,12 @@ void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port) ...@@ -1067,12 +1068,12 @@ void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
ksz9477_port_acl_init(dev, port); ksz9477_port_acl_init(dev, port);
/* clear pending wake flags */ /* clear pending wake flags */
ksz9477_handle_wake_reason(dev, port); ksz_handle_wake_reason(dev, port);
/* Disable all WoL options by default. Otherwise /* Disable all WoL options by default. Otherwise
* ksz_switch_macaddr_get/put logic will not work properly. * ksz_switch_macaddr_get/put logic will not work properly.
*/ */
ksz_pwrite8(dev, port, REG_PORT_PME_CTRL, 0); ksz_pwrite8(dev, port, regs[REG_PORT_PME_CTRL], 0);
} }
void ksz9477_config_cpu_port(struct dsa_switch *ds) void ksz9477_config_cpu_port(struct dsa_switch *ds)
...@@ -1169,6 +1170,7 @@ int ksz9477_enable_stp_addr(struct ksz_device *dev) ...@@ -1169,6 +1170,7 @@ int ksz9477_enable_stp_addr(struct ksz_device *dev)
int ksz9477_setup(struct dsa_switch *ds) int ksz9477_setup(struct dsa_switch *ds)
{ {
struct ksz_device *dev = ds->priv; struct ksz_device *dev = ds->priv;
const u16 *regs = dev->info->regs;
int ret = 0; int ret = 0;
ds->mtu_enforcement_ingress = true; ds->mtu_enforcement_ingress = true;
...@@ -1199,13 +1201,11 @@ int ksz9477_setup(struct dsa_switch *ds) ...@@ -1199,13 +1201,11 @@ int ksz9477_setup(struct dsa_switch *ds)
/* enable global MIB counter freeze function */ /* enable global MIB counter freeze function */
ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true); ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
/* Make sure PME (WoL) is not enabled. If requested, it will be /* Make sure PME (WoL) is not enabled. If requested, it will
* enabled by ksz9477_wol_pre_shutdown(). Otherwise, some PMICs do not * be enabled by ksz_wol_pre_shutdown(). Otherwise, some PMICs
* like PME events changes before shutdown. * do not like PME events changes before shutdown.
*/ */
ksz_write8(dev, REG_SW_PME_CTRL, 0); return ksz_write8(dev, regs[REG_SW_PME_CTRL], 0);
return 0;
} }
u32 ksz9477_get_port_addr(int port, int offset) u32 ksz9477_get_port_addr(int port, int offset)
......
...@@ -348,9 +348,9 @@ static const struct ksz_dev_ops ksz9477_dev_ops = { ...@@ -348,9 +348,9 @@ static const struct ksz_dev_ops ksz9477_dev_ops = {
.mdb_add = ksz9477_mdb_add, .mdb_add = ksz9477_mdb_add,
.mdb_del = ksz9477_mdb_del, .mdb_del = ksz9477_mdb_del,
.change_mtu = ksz9477_change_mtu, .change_mtu = ksz9477_change_mtu,
.get_wol = ksz9477_get_wol, .pme_write8 = ksz_write8,
.set_wol = ksz9477_set_wol, .pme_pread8 = ksz_pread8,
.wol_pre_shutdown = ksz9477_wol_pre_shutdown, .pme_pwrite8 = ksz_pwrite8,
.config_cpu_port = ksz9477_config_cpu_port, .config_cpu_port = ksz9477_config_cpu_port,
.tc_cbs_set_cinc = ksz9477_tc_cbs_set_cinc, .tc_cbs_set_cinc = ksz9477_tc_cbs_set_cinc,
.enable_stp_addr = ksz9477_enable_stp_addr, .enable_stp_addr = ksz9477_enable_stp_addr,
...@@ -539,6 +539,9 @@ static const u16 ksz9477_regs[] = { ...@@ -539,6 +539,9 @@ static const u16 ksz9477_regs[] = {
[S_MULTICAST_CTRL] = 0x0331, [S_MULTICAST_CTRL] = 0x0331,
[P_XMII_CTRL_0] = 0x0300, [P_XMII_CTRL_0] = 0x0300,
[P_XMII_CTRL_1] = 0x0301, [P_XMII_CTRL_1] = 0x0301,
[REG_SW_PME_CTRL] = 0x0006,
[REG_PORT_PME_STATUS] = 0x0013,
[REG_PORT_PME_CTRL] = 0x0017,
}; };
static const u32 ksz9477_masks[] = { static const u32 ksz9477_masks[] = {
...@@ -3742,17 +3745,8 @@ static int ksz_setup_tc(struct dsa_switch *ds, int port, ...@@ -3742,17 +3745,8 @@ static int ksz_setup_tc(struct dsa_switch *ds, int port,
} }
} }
static void ksz_get_wol(struct dsa_switch *ds, int port,
struct ethtool_wolinfo *wol)
{
struct ksz_device *dev = ds->priv;
if (dev->dev_ops->get_wol)
dev->dev_ops->get_wol(dev, port, wol);
}
/** /**
* ksz9477_handle_wake_reason - Handle wake reason on a specified port. * ksz_handle_wake_reason - Handle wake reason on a specified port.
* @dev: The device structure. * @dev: The device structure.
* @port: The port number. * @port: The port number.
* *
...@@ -3764,12 +3758,15 @@ static void ksz_get_wol(struct dsa_switch *ds, int port, ...@@ -3764,12 +3758,15 @@ static void ksz_get_wol(struct dsa_switch *ds, int port,
* *
* Return: 0 on success, or an error code on failure. * Return: 0 on success, or an error code on failure.
*/ */
int ksz9477_handle_wake_reason(struct ksz_device *dev, int port) int ksz_handle_wake_reason(struct ksz_device *dev, int port)
{ {
const struct ksz_dev_ops *ops = dev->dev_ops;
const u16 *regs = dev->info->regs;
u8 pme_status; u8 pme_status;
int ret; int ret;
ret = ksz_pread8(dev, port, REG_PORT_PME_STATUS, &pme_status); ret = ops->pme_pread8(dev, port, regs[REG_PORT_PME_STATUS],
&pme_status);
if (ret) if (ret)
return ret; return ret;
...@@ -3781,25 +3778,31 @@ int ksz9477_handle_wake_reason(struct ksz_device *dev, int port) ...@@ -3781,25 +3778,31 @@ int ksz9477_handle_wake_reason(struct ksz_device *dev, int port)
pme_status & PME_WOL_LINKUP ? " \"Link Up\"" : "", pme_status & PME_WOL_LINKUP ? " \"Link Up\"" : "",
pme_status & PME_WOL_ENERGY ? " \"Energy detect\"" : ""); pme_status & PME_WOL_ENERGY ? " \"Energy detect\"" : "");
return ksz_pwrite8(dev, port, REG_PORT_PME_STATUS, pme_status); return ops->pme_pwrite8(dev, port, regs[REG_PORT_PME_STATUS],
pme_status);
} }
/** /**
* ksz9477_get_wol - Get Wake-on-LAN settings for a specified port. * ksz_get_wol - Get Wake-on-LAN settings for a specified port.
* @dev: The device structure. * @ds: The dsa_switch structure.
* @port: The port number. * @port: The port number.
* @wol: Pointer to ethtool Wake-on-LAN settings structure. * @wol: Pointer to ethtool Wake-on-LAN settings structure.
* *
* This function checks the PME Pin Control Register to see if PME Pin Output * This function checks the device PME wakeup_source flag and chip_id.
* Enable is set, indicating PME is enabled. If enabled, it sets the supported * If enabled and supported, it sets the supported and active WoL
* and active WoL flags. * flags.
*/ */
void ksz9477_get_wol(struct ksz_device *dev, int port, static void ksz_get_wol(struct dsa_switch *ds, int port,
struct ethtool_wolinfo *wol) struct ethtool_wolinfo *wol)
{ {
struct ksz_device *dev = ds->priv;
const u16 *regs = dev->info->regs;
u8 pme_ctrl; u8 pme_ctrl;
int ret; int ret;
if (!is_ksz9477(dev))
return;
if (!dev->wakeup_source) if (!dev->wakeup_source)
return; return;
...@@ -3812,7 +3815,8 @@ void ksz9477_get_wol(struct ksz_device *dev, int port, ...@@ -3812,7 +3815,8 @@ void ksz9477_get_wol(struct ksz_device *dev, int port,
if (ksz_is_port_mac_global_usable(dev->ds, port)) if (ksz_is_port_mac_global_usable(dev->ds, port))
wol->supported |= WAKE_MAGIC; wol->supported |= WAKE_MAGIC;
ret = ksz_pread8(dev, port, REG_PORT_PME_CTRL, &pme_ctrl); ret = dev->dev_ops->pme_pread8(dev, port, regs[REG_PORT_PME_CTRL],
&pme_ctrl);
if (ret) if (ret)
return; return;
...@@ -3822,35 +3826,26 @@ void ksz9477_get_wol(struct ksz_device *dev, int port, ...@@ -3822,35 +3826,26 @@ void ksz9477_get_wol(struct ksz_device *dev, int port,
wol->wolopts |= WAKE_PHY; wol->wolopts |= WAKE_PHY;
} }
static int ksz_set_wol(struct dsa_switch *ds, int port,
struct ethtool_wolinfo *wol)
{
struct ksz_device *dev = ds->priv;
if (dev->dev_ops->set_wol)
return dev->dev_ops->set_wol(dev, port, wol);
return -EOPNOTSUPP;
}
/** /**
* ksz9477_set_wol - Set Wake-on-LAN settings for a specified port. * ksz_set_wol - Set Wake-on-LAN settings for a specified port.
* @dev: The device structure. * @ds: The dsa_switch structure.
* @port: The port number. * @port: The port number.
* @wol: Pointer to ethtool Wake-on-LAN settings structure. * @wol: Pointer to ethtool Wake-on-LAN settings structure.
* *
* This function configures Wake-on-LAN (WoL) settings for a specified port. * This function configures Wake-on-LAN (WoL) settings for a specified
* It validates the provided WoL options, checks if PME is enabled via the * port. It validates the provided WoL options, checks if PME is
* switch's PME Pin Control Register, clears any previous wake reasons, * enabled and supported, clears any previous wake reasons, and sets
* and sets the Magic Packet flag in the port's PME control register if * the Magic Packet flag in the port's PME control register if
* specified. * specified.
* *
* Return: 0 on success, or other error codes on failure. * Return: 0 on success, or other error codes on failure.
*/ */
int ksz9477_set_wol(struct ksz_device *dev, int port, static int ksz_set_wol(struct dsa_switch *ds, int port,
struct ethtool_wolinfo *wol) struct ethtool_wolinfo *wol)
{ {
u8 pme_ctrl = 0, pme_ctrl_old = 0; u8 pme_ctrl = 0, pme_ctrl_old = 0;
struct ksz_device *dev = ds->priv;
const u16 *regs = dev->info->regs;
bool magic_switched_off; bool magic_switched_off;
bool magic_switched_on; bool magic_switched_on;
int ret; int ret;
...@@ -3858,10 +3853,13 @@ int ksz9477_set_wol(struct ksz_device *dev, int port, ...@@ -3858,10 +3853,13 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC)) if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
return -EINVAL; return -EINVAL;
if (!is_ksz9477(dev))
return -EOPNOTSUPP;
if (!dev->wakeup_source) if (!dev->wakeup_source)
return -EOPNOTSUPP; return -EOPNOTSUPP;
ret = ksz9477_handle_wake_reason(dev, port); ret = ksz_handle_wake_reason(dev, port);
if (ret) if (ret)
return ret; return ret;
...@@ -3870,7 +3868,8 @@ int ksz9477_set_wol(struct ksz_device *dev, int port, ...@@ -3870,7 +3868,8 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
if (wol->wolopts & WAKE_PHY) if (wol->wolopts & WAKE_PHY)
pme_ctrl |= PME_WOL_LINKUP | PME_WOL_ENERGY; pme_ctrl |= PME_WOL_LINKUP | PME_WOL_ENERGY;
ret = ksz_pread8(dev, port, REG_PORT_PME_CTRL, &pme_ctrl_old); ret = dev->dev_ops->pme_pread8(dev, port, regs[REG_PORT_PME_CTRL],
&pme_ctrl_old);
if (ret) if (ret)
return ret; return ret;
...@@ -3893,7 +3892,8 @@ int ksz9477_set_wol(struct ksz_device *dev, int port, ...@@ -3893,7 +3892,8 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
ksz_switch_macaddr_put(dev->ds); ksz_switch_macaddr_put(dev->ds);
} }
ret = ksz_pwrite8(dev, port, REG_PORT_PME_CTRL, pme_ctrl); ret = dev->dev_ops->pme_pwrite8(dev, port, regs[REG_PORT_PME_CTRL],
pme_ctrl);
if (ret) { if (ret) {
if (magic_switched_on) if (magic_switched_on)
ksz_switch_macaddr_put(dev->ds); ksz_switch_macaddr_put(dev->ds);
...@@ -3904,7 +3904,7 @@ int ksz9477_set_wol(struct ksz_device *dev, int port, ...@@ -3904,7 +3904,7 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
} }
/** /**
* ksz9477_wol_pre_shutdown - Prepares the switch device for shutdown while * ksz_wol_pre_shutdown - Prepares the switch device for shutdown while
* considering Wake-on-LAN (WoL) settings. * considering Wake-on-LAN (WoL) settings.
* @dev: The switch device structure. * @dev: The switch device structure.
* @wol_enabled: Pointer to a boolean which will be set to true if WoL is * @wol_enabled: Pointer to a boolean which will be set to true if WoL is
...@@ -3915,32 +3915,38 @@ int ksz9477_set_wol(struct ksz_device *dev, int port, ...@@ -3915,32 +3915,38 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
* the wol_enabled flag accordingly to reflect whether WoL is active on any * the wol_enabled flag accordingly to reflect whether WoL is active on any
* port. * port.
*/ */
void ksz9477_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled) static void ksz_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled)
{ {
const struct ksz_dev_ops *ops = dev->dev_ops;
const u16 *regs = dev->info->regs;
struct dsa_port *dp; struct dsa_port *dp;
int ret; int ret;
*wol_enabled = false; *wol_enabled = false;
if (!is_ksz9477(dev))
return;
if (!dev->wakeup_source) if (!dev->wakeup_source)
return; return;
dsa_switch_for_each_user_port(dp, dev->ds) { dsa_switch_for_each_user_port(dp, dev->ds) {
u8 pme_ctrl = 0; u8 pme_ctrl = 0;
ret = ksz_pread8(dev, dp->index, REG_PORT_PME_CTRL, &pme_ctrl); ret = ops->pme_pread8(dev, dp->index,
regs[REG_PORT_PME_CTRL], &pme_ctrl);
if (!ret && pme_ctrl) if (!ret && pme_ctrl)
*wol_enabled = true; *wol_enabled = true;
/* make sure there are no pending wake events which would /* make sure there are no pending wake events which would
* prevent the device from going to sleep/shutdown. * prevent the device from going to sleep/shutdown.
*/ */
ksz9477_handle_wake_reason(dev, dp->index); ksz_handle_wake_reason(dev, dp->index);
} }
/* Now we are save to enable PME pin. */ /* Now we are save to enable PME pin. */
if (*wol_enabled) if (*wol_enabled)
ksz_write8(dev, REG_SW_PME_CTRL, PME_ENABLE); ops->pme_write8(dev, regs[REG_SW_PME_CTRL], PME_ENABLE);
} }
static int ksz_port_set_mac_address(struct dsa_switch *ds, int port, static int ksz_port_set_mac_address(struct dsa_switch *ds, int port,
...@@ -4253,8 +4259,7 @@ void ksz_switch_shutdown(struct ksz_device *dev) ...@@ -4253,8 +4259,7 @@ void ksz_switch_shutdown(struct ksz_device *dev)
{ {
bool wol_enabled = false; bool wol_enabled = false;
if (dev->dev_ops->wol_pre_shutdown) ksz_wol_pre_shutdown(dev, &wol_enabled);
dev->dev_ops->wol_pre_shutdown(dev, &wol_enabled);
if (dev->dev_ops->reset && !wol_enabled) if (dev->dev_ops->reset && !wol_enabled)
dev->dev_ops->reset(dev); dev->dev_ops->reset(dev);
......
...@@ -235,6 +235,9 @@ enum ksz_regs { ...@@ -235,6 +235,9 @@ enum ksz_regs {
S_MULTICAST_CTRL, S_MULTICAST_CTRL,
P_XMII_CTRL_0, P_XMII_CTRL_0,
P_XMII_CTRL_1, P_XMII_CTRL_1,
REG_SW_PME_CTRL,
REG_PORT_PME_STATUS,
REG_PORT_PME_CTRL,
}; };
enum ksz_masks { enum ksz_masks {
...@@ -354,6 +357,11 @@ struct ksz_dev_ops { ...@@ -354,6 +357,11 @@ struct ksz_dev_ops {
void (*get_caps)(struct ksz_device *dev, int port, void (*get_caps)(struct ksz_device *dev, int port,
struct phylink_config *config); struct phylink_config *config);
int (*change_mtu)(struct ksz_device *dev, int port, int mtu); int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
int (*pme_write8)(struct ksz_device *dev, u32 reg, u8 value);
int (*pme_pread8)(struct ksz_device *dev, int port, int offset,
u8 *data);
int (*pme_pwrite8)(struct ksz_device *dev, int port, int offset,
u8 data);
void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze); void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
void (*port_init_cnt)(struct ksz_device *dev, int port); void (*port_init_cnt)(struct ksz_device *dev, int port);
void (*phylink_mac_link_up)(struct ksz_device *dev, int port, void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
...@@ -363,11 +371,6 @@ struct ksz_dev_ops { ...@@ -363,11 +371,6 @@ struct ksz_dev_ops {
int duplex, bool tx_pause, bool rx_pause); int duplex, bool tx_pause, bool rx_pause);
void (*setup_rgmii_delay)(struct ksz_device *dev, int port); void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
int (*tc_cbs_set_cinc)(struct ksz_device *dev, int port, u32 val); int (*tc_cbs_set_cinc)(struct ksz_device *dev, int port, u32 val);
void (*get_wol)(struct ksz_device *dev, int port,
struct ethtool_wolinfo *wol);
int (*set_wol)(struct ksz_device *dev, int port,
struct ethtool_wolinfo *wol);
void (*wol_pre_shutdown)(struct ksz_device *dev, bool *wol_enabled);
void (*config_cpu_port)(struct dsa_switch *ds); void (*config_cpu_port)(struct dsa_switch *ds);
int (*enable_stp_addr)(struct ksz_device *dev); int (*enable_stp_addr)(struct ksz_device *dev);
int (*reset)(struct ksz_device *dev); int (*reset)(struct ksz_device *dev);
...@@ -391,12 +394,7 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, ...@@ -391,12 +394,7 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
struct netlink_ext_ack *extack); struct netlink_ext_ack *extack);
void ksz_switch_macaddr_put(struct dsa_switch *ds); void ksz_switch_macaddr_put(struct dsa_switch *ds);
void ksz_switch_shutdown(struct ksz_device *dev); void ksz_switch_shutdown(struct ksz_device *dev);
int ksz9477_handle_wake_reason(struct ksz_device *dev, int port); int ksz_handle_wake_reason(struct ksz_device *dev, int port);
void ksz9477_get_wol(struct ksz_device *dev, int port,
struct ethtool_wolinfo *wol);
int ksz9477_set_wol(struct ksz_device *dev, int port,
struct ethtool_wolinfo *wol);
void ksz9477_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled);
/* Common register access functions */ /* Common register access functions */
static inline struct regmap *ksz_regmap_8(struct ksz_device *dev) static inline struct regmap *ksz_regmap_8(struct ksz_device *dev)
...@@ -635,6 +633,11 @@ static inline bool is_ksz8(struct ksz_device *dev) ...@@ -635,6 +633,11 @@ static inline bool is_ksz8(struct ksz_device *dev)
return ksz_is_ksz87xx(dev) || ksz_is_ksz88x3(dev); return ksz_is_ksz87xx(dev) || ksz_is_ksz88x3(dev);
} }
static inline bool is_ksz9477(struct ksz_device *dev)
{
return dev->chip_id == KSZ9477_CHIP_ID;
}
static inline int is_lan937x(struct ksz_device *dev) static inline int is_lan937x(struct ksz_device *dev)
{ {
return dev->chip_id == LAN9370_CHIP_ID || return dev->chip_id == LAN9370_CHIP_ID ||
...@@ -702,15 +705,10 @@ static inline bool is_lan937x_tx_phy(struct ksz_device *dev, int port) ...@@ -702,15 +705,10 @@ static inline bool is_lan937x_tx_phy(struct ksz_device *dev, int port)
#define P_MII_SEL_M 0x3 #define P_MII_SEL_M 0x3
/* KSZ9477, KSZ8795 Wake-on-LAN (WoL) masks */ /* KSZ9477, KSZ8795 Wake-on-LAN (WoL) masks */
#define REG_PORT_PME_STATUS 0x0013
#define REG_PORT_PME_CTRL 0x0017
#define PME_WOL_MAGICPKT BIT(2) #define PME_WOL_MAGICPKT BIT(2)
#define PME_WOL_LINKUP BIT(1) #define PME_WOL_LINKUP BIT(1)
#define PME_WOL_ENERGY BIT(0) #define PME_WOL_ENERGY BIT(0)
#define REG_SW_PME_CTRL 0x0006
#define PME_ENABLE BIT(1) #define PME_ENABLE BIT(1)
#define PME_POLARITY BIT(0) #define PME_POLARITY BIT(0)
......
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