net: dsa: microchip: Ensure Stable PME Pin State for Wake-on-LAN
authorOleksij Rempel <o.rempel@pengutronix.de>
Thu, 26 Oct 2023 05:10:51 +0000 (07:10 +0200)
committerJakub Kicinski <kuba@kernel.org>
Fri, 27 Oct 2023 21:43:53 +0000 (14:43 -0700)
Ensures a stable PME (Power Management Event) pin state by disabling PME
on system start and enabling it on shutdown only if WoL (Wake-on-LAN) is
configured. This is needed to avoid issues with some PMICs (Power
Management ICs).

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://lore.kernel.org/r/20231026051051.2316937-6-o.rempel@pengutronix.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/dsa/microchip/ksz9477.c
drivers/net/dsa/microchip/ksz9477.h
drivers/net/dsa/microchip/ksz_common.c
drivers/net/dsa/microchip/ksz_common.h

index 441b4597ef272093034cf349ed0c0cb0ab452f0d..f24181086e165415771974f76f02d7b6d693b1f7 100644 (file)
@@ -197,6 +197,46 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
        return 0;
 }
 
+/**
+ * ksz9477_wol_pre_shutdown - Prepares the switch device for shutdown while
+ *                            considering Wake-on-LAN (WoL) settings.
+ * @dev: The switch device structure.
+ * @wol_enabled: Pointer to a boolean which will be set to true if WoL is
+ *               enabled on any port.
+ *
+ * This function prepares the switch device for a safe shutdown while taking
+ * into account the Wake-on-LAN (WoL) settings on the user ports. It updates
+ * the wol_enabled flag accordingly to reflect whether WoL is active on any
+ * port.
+ */
+void ksz9477_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled)
+{
+       struct dsa_port *dp;
+       int ret;
+
+       *wol_enabled = false;
+
+       if (!dev->wakeup_source)
+               return;
+
+       dsa_switch_for_each_user_port(dp, dev->ds) {
+               u8 pme_ctrl = 0;
+
+               ret = ksz_pread8(dev, dp->index, REG_PORT_PME_CTRL, &pme_ctrl);
+               if (!ret && pme_ctrl)
+                       *wol_enabled = true;
+
+               /* make sure there are no pending wake events which would
+                * prevent the device from going to sleep/shutdown.
+                */
+               ksz9477_handle_wake_reason(dev, dp->index);
+       }
+
+       /* Now we are save to enable PME pin. */
+       if (*wol_enabled)
+               ksz_write8(dev, REG_SW_PME_CTRL, PME_ENABLE);
+}
+
 static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
 {
        unsigned int val;
@@ -1277,6 +1317,12 @@ int ksz9477_setup(struct dsa_switch *ds)
        /* enable global MIB counter freeze function */
        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
+        * enabled by ksz9477_wol_pre_shutdown(). Otherwise, some PMICs do not
+        * like PME events changes before shutdown.
+        */
+       ksz_write8(dev, REG_SW_PME_CTRL, 0);
+
        return 0;
 }
 
index fa8d0318b437085fe3c1006c63c45c03d864e669..ce1e656b800b157cde165c034b2a731af6a69111 100644 (file)
@@ -62,6 +62,7 @@ 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);
 
 int ksz9477_port_acl_init(struct ksz_device *dev, int port);
 void ksz9477_port_acl_free(struct ksz_device *dev, int port);
index 3b48e42148a09806bf9234b494a4f756ece77505..3fed406fb46ae6361c5d1819470b750083e60ef6 100644 (file)
@@ -321,6 +321,7 @@ static const struct ksz_dev_ops ksz9477_dev_ops = {
        .phylink_mac_link_up = ksz9477_phylink_mac_link_up,
        .get_wol = ksz9477_get_wol,
        .set_wol = ksz9477_set_wol,
+       .wol_pre_shutdown = ksz9477_wol_pre_shutdown,
        .config_cpu_port = ksz9477_config_cpu_port,
        .tc_cbs_set_cinc = ksz9477_tc_cbs_set_cinc,
        .enable_stp_addr = ksz9477_enable_stp_addr,
@@ -3857,7 +3858,12 @@ EXPORT_SYMBOL(ksz_switch_alloc);
  */
 void ksz_switch_shutdown(struct ksz_device *dev)
 {
-       if (dev->dev_ops->reset)
+       bool wol_enabled = false;
+
+       if (dev->dev_ops->wol_pre_shutdown)
+               dev->dev_ops->wol_pre_shutdown(dev, &wol_enabled);
+
+       if (dev->dev_ops->reset && !wol_enabled)
                dev->dev_ops->reset(dev);
 
        dsa_switch_shutdown(dev->ds);
index 14b4828f80a1ce94f4076ed69816b9d9f0fe1a0a..b7e8a403a132fdc5b3fc757732472df038728e15 100644 (file)
@@ -378,6 +378,7 @@ struct ksz_dev_ops {
                        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);
        int (*enable_stp_addr)(struct ksz_device *dev);
        int (*reset)(struct ksz_device *dev);