Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp190174rdh; Wed, 25 Oct 2023 22:12:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEUpZQ/I3HJfvT6l1d5vc143D5/on8xQJKo+LJ6gVeAkKJYSdd7MXGrd/lRtQj+hwqq7wIm X-Received: by 2002:a25:5012:0:b0:d9a:ede4:7126 with SMTP id e18-20020a255012000000b00d9aede47126mr15449732ybb.44.1698297171561; Wed, 25 Oct 2023 22:12:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698297171; cv=none; d=google.com; s=arc-20160816; b=p/01r4I2rHafFSW30YAnxUIpZ6zCWlKfKLlOdG08kotKAU0mjLyc5NGfHCLUh6s/CD uMd6wqSU+SKnCZYj4OmvA19OgjVpAUmgJXXCUz6y0cMCIDU/H7q2//amtYLslzeHRxkP /YVUgtkxc8q5TCOQb/i8gjnNpm0BcYvf0zMSpOWGYsH8V/oIZQvzkDPGnZ71Bms91EHx +rNTeQqSJucPytllVL9UhjjHiYmyi3UaiRXeN8z+jqr5a13iu9mL9gbb/tg78zDHMDdm i5/zKP2oFc8HC32cifgFpcGEvydALBkZw6GZpOCglnR2LRUeOta/6PvTaz7/Jtv382pz RkkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=kqSes5kfRzGcovvn+UEZewcFcXSGVMAklQB0oCZq/5s=; fh=/pJ6KpVpsxFeEwEzwJ+ttbQ0e+DStiiQBZJIoLycD+A=; b=lXEsQZ/pdZ25czj+Ygam7EAjD3j693r78iDV7dtQffAgFC06m6k5A5DL4kBiTeHrVv eCsyqpyCsr5aJO6gR5cYXNXQL9R8n3ynt1I/un+zAzJsmdxAJFIFKDa35bK3akW4C5bc ym60xrlVa/Vo9MiZHh7cctghDQQQ7nF/zxxWLfNJ0uxXsXstmB7Fh4ZTxm1+J+xiRI71 sJtoLR02Ki5SEOzdLr1dP3Hr3hXU8Vn6WP8ot1dQFuQOioS5Wn0fUUxVIDZyjD9FIiW2 NitZMTo0weyyl/JXmIxxTAPSWtXEvdJ5aDg9CJl84nmE28AS3iII1IovRWxnZWH4uhA0 p0YA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id y72-20020a25324b000000b00da02d3b0106si7162522yby.167.2023.10.25.22.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 22:12:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 6869E809E710; Wed, 25 Oct 2023 22:11:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343564AbjJZFLk (ORCPT + 99 others); Thu, 26 Oct 2023 01:11:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233175AbjJZFLX (ORCPT ); Thu, 26 Oct 2023 01:11:23 -0400 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [IPv6:2a0a:edc0:2:b01:1d::104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF2E7187 for ; Wed, 25 Oct 2023 22:11:17 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qvsdy-0004YC-OS; Thu, 26 Oct 2023 07:10:54 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qvsdw-004L1X-PJ; Thu, 26 Oct 2023 07:10:52 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1qvsdw-009ilI-2E; Thu, 26 Oct 2023 07:10:52 +0200 From: Oleksij Rempel To: "David S. Miller" , Andrew Lunn , Eric Dumazet , Florian Fainelli , Jakub Kicinski , Paolo Abeni , Vladimir Oltean , Woojung Huh , Arun Ramadoss , Conor Dooley , Krzysztof Kozlowski , Rob Herring Cc: Oleksij Rempel , Vladimir Oltean , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, "Russell King (Oracle)" , devicetree@vger.kernel.org Subject: [PATCH net-next v8 5/5] net: dsa: microchip: Ensure Stable PME Pin State for Wake-on-LAN Date: Thu, 26 Oct 2023 07:10:51 +0200 Message-Id: <20231026051051.2316937-6-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231026051051.2316937-1-o.rempel@pengutronix.de> References: <20231026051051.2316937-1-o.rempel@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 25 Oct 2023 22:11:56 -0700 (PDT) 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 Reviewed-by: Vladimir Oltean --- drivers/net/dsa/microchip/ksz9477.c | 46 ++++++++++++++++++++++++++ drivers/net/dsa/microchip/ksz9477.h | 1 + drivers/net/dsa/microchip/ksz_common.c | 8 ++++- drivers/net/dsa/microchip/ksz_common.h | 1 + 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c index 441b4597ef27..f24181086e16 100644 --- a/drivers/net/dsa/microchip/ksz9477.c +++ b/drivers/net/dsa/microchip/ksz9477.c @@ -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; } diff --git a/drivers/net/dsa/microchip/ksz9477.h b/drivers/net/dsa/microchip/ksz9477.h index fa8d0318b437..ce1e656b800b 100644 --- a/drivers/net/dsa/microchip/ksz9477.h +++ b/drivers/net/dsa/microchip/ksz9477.h @@ -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); diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c index 3b48e42148a0..3fed406fb46a 100644 --- a/drivers/net/dsa/microchip/ksz_common.c +++ b/drivers/net/dsa/microchip/ksz_common.c @@ -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); diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h index 14b4828f80a1..b7e8a403a132 100644 --- a/drivers/net/dsa/microchip/ksz_common.h +++ b/drivers/net/dsa/microchip/ksz_common.h @@ -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); -- 2.39.2