xref: /openbmc/linux/drivers/net/ethernet/broadcom/genet/bcmgenet_wol.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c51de7f3SFlorian Fainelli /*
3c51de7f3SFlorian Fainelli  * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
4c51de7f3SFlorian Fainelli  *
500d1a1bcSDoug Berger  * Copyright (c) 2014-2024 Broadcom
6c51de7f3SFlorian Fainelli  */
7c51de7f3SFlorian Fainelli 
8c51de7f3SFlorian Fainelli #define pr_fmt(fmt)				"bcmgenet_wol: " fmt
9c51de7f3SFlorian Fainelli 
10c51de7f3SFlorian Fainelli #include <linux/kernel.h>
11c51de7f3SFlorian Fainelli #include <linux/module.h>
12c51de7f3SFlorian Fainelli #include <linux/sched.h>
13c51de7f3SFlorian Fainelli #include <linux/types.h>
14c51de7f3SFlorian Fainelli #include <linux/interrupt.h>
15c51de7f3SFlorian Fainelli #include <linux/string.h>
16c51de7f3SFlorian Fainelli #include <linux/init.h>
17c51de7f3SFlorian Fainelli #include <linux/errno.h>
18c51de7f3SFlorian Fainelli #include <linux/delay.h>
19c51de7f3SFlorian Fainelli #include <linux/pm.h>
20c51de7f3SFlorian Fainelli #include <linux/clk.h>
21c51de7f3SFlorian Fainelli #include <linux/platform_device.h>
22c51de7f3SFlorian Fainelli #include <net/arp.h>
23c51de7f3SFlorian Fainelli 
24c51de7f3SFlorian Fainelli #include <linux/mii.h>
25c51de7f3SFlorian Fainelli #include <linux/ethtool.h>
26c51de7f3SFlorian Fainelli #include <linux/netdevice.h>
27c51de7f3SFlorian Fainelli #include <linux/inetdevice.h>
28c51de7f3SFlorian Fainelli #include <linux/etherdevice.h>
29c51de7f3SFlorian Fainelli #include <linux/skbuff.h>
30c51de7f3SFlorian Fainelli #include <linux/in.h>
31c51de7f3SFlorian Fainelli #include <linux/ip.h>
32c51de7f3SFlorian Fainelli #include <linux/ipv6.h>
33c51de7f3SFlorian Fainelli #include <linux/phy.h>
34c51de7f3SFlorian Fainelli 
35c51de7f3SFlorian Fainelli #include "bcmgenet.h"
36c51de7f3SFlorian Fainelli 
37c51de7f3SFlorian Fainelli /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
38c51de7f3SFlorian Fainelli  * Detection is supported through ethtool
39c51de7f3SFlorian Fainelli  */
bcmgenet_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)40c51de7f3SFlorian Fainelli void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
41c51de7f3SFlorian Fainelli {
42c51de7f3SFlorian Fainelli 	struct bcmgenet_priv *priv = netdev_priv(dev);
4300b022f8SJeremy Linton 	struct device *kdev = &priv->pdev->dev;
44*fcb2be1bSFlorian Fainelli 	u32 phy_wolopts = 0;
4500b022f8SJeremy Linton 
46*fcb2be1bSFlorian Fainelli 	if (dev->phydev) {
477e400ff3SFlorian Fainelli 		phy_ethtool_get_wol(dev->phydev, wol);
48*fcb2be1bSFlorian Fainelli 		phy_wolopts = wol->wolopts;
49*fcb2be1bSFlorian Fainelli 	}
507e400ff3SFlorian Fainelli 
5173780807SFlorian Fainelli 	/* MAC is not wake-up capable, return what the PHY does */
5273780807SFlorian Fainelli 	if (!device_can_wakeup(kdev))
5300b022f8SJeremy Linton 		return;
54c51de7f3SFlorian Fainelli 
5573780807SFlorian Fainelli 	/* Overlay MAC capabilities with that of the PHY queried before */
5673780807SFlorian Fainelli 	wol->supported |= WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
57*fcb2be1bSFlorian Fainelli 	wol->wolopts |= priv->wolopts;
58c51de7f3SFlorian Fainelli 
59*fcb2be1bSFlorian Fainelli 	/* Return the PHY configured magic password */
60*fcb2be1bSFlorian Fainelli 	if (phy_wolopts & WAKE_MAGICSECURE)
61*fcb2be1bSFlorian Fainelli 		return;
62*fcb2be1bSFlorian Fainelli 
63*fcb2be1bSFlorian Fainelli 	/* Otherwise the MAC one */
64*fcb2be1bSFlorian Fainelli 	memset(wol->sopass, 0, sizeof(wol->sopass));
656f768905SDoug Berger 	if (wol->wolopts & WAKE_MAGICSECURE)
666f768905SDoug Berger 		memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
67c51de7f3SFlorian Fainelli }
68c51de7f3SFlorian Fainelli 
69c51de7f3SFlorian Fainelli /* ethtool function - set WOL (Wake on LAN) settings.
70c51de7f3SFlorian Fainelli  * Only for magic packet detection mode.
71c51de7f3SFlorian Fainelli  */
bcmgenet_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)72c51de7f3SFlorian Fainelli int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
73c51de7f3SFlorian Fainelli {
74c51de7f3SFlorian Fainelli 	struct bcmgenet_priv *priv = netdev_priv(dev);
75c51de7f3SFlorian Fainelli 	struct device *kdev = &priv->pdev->dev;
767e400ff3SFlorian Fainelli 	int ret;
777e400ff3SFlorian Fainelli 
787e400ff3SFlorian Fainelli 	/* Try Wake-on-LAN from the PHY first */
797e400ff3SFlorian Fainelli 	if (dev->phydev) {
807e400ff3SFlorian Fainelli 		ret = phy_ethtool_set_wol(dev->phydev, wol);
81*fcb2be1bSFlorian Fainelli 		if (ret != -EOPNOTSUPP && wol->wolopts)
827e400ff3SFlorian Fainelli 			return ret;
837e400ff3SFlorian Fainelli 	}
84c51de7f3SFlorian Fainelli 
85c51de7f3SFlorian Fainelli 	if (!device_can_wakeup(kdev))
86c51de7f3SFlorian Fainelli 		return -ENOTSUPP;
87c51de7f3SFlorian Fainelli 
88f50932ccSDoug Berger 	if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER))
89c51de7f3SFlorian Fainelli 		return -EINVAL;
90c51de7f3SFlorian Fainelli 
916f768905SDoug Berger 	if (wol->wolopts & WAKE_MAGICSECURE)
926f768905SDoug Berger 		memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
93c51de7f3SFlorian Fainelli 
94c51de7f3SFlorian Fainelli 	/* Flag the device and relevant IRQ as wakeup capable */
95c51de7f3SFlorian Fainelli 	if (wol->wolopts) {
96c51de7f3SFlorian Fainelli 		device_set_wakeup_enable(kdev, 1);
97083731a8SFlorian Fainelli 		/* Avoid unbalanced enable_irq_wake calls */
983fcdf2dfSFlorian Fainelli 		if (priv->wol_irq_disabled) {
99c51de7f3SFlorian Fainelli 			enable_irq_wake(priv->wol_irq);
1003fcdf2dfSFlorian Fainelli 			enable_irq_wake(priv->irq0);
1013fcdf2dfSFlorian Fainelli 		}
102c51de7f3SFlorian Fainelli 		priv->wol_irq_disabled = false;
103c51de7f3SFlorian Fainelli 	} else {
104c51de7f3SFlorian Fainelli 		device_set_wakeup_enable(kdev, 0);
105c51de7f3SFlorian Fainelli 		/* Avoid unbalanced disable_irq_wake calls */
1063fcdf2dfSFlorian Fainelli 		if (!priv->wol_irq_disabled) {
107c51de7f3SFlorian Fainelli 			disable_irq_wake(priv->wol_irq);
1083fcdf2dfSFlorian Fainelli 			disable_irq_wake(priv->irq0);
1093fcdf2dfSFlorian Fainelli 		}
110c51de7f3SFlorian Fainelli 		priv->wol_irq_disabled = true;
111c51de7f3SFlorian Fainelli 	}
112c51de7f3SFlorian Fainelli 
113c51de7f3SFlorian Fainelli 	priv->wolopts = wol->wolopts;
114c51de7f3SFlorian Fainelli 
115c51de7f3SFlorian Fainelli 	return 0;
116c51de7f3SFlorian Fainelli }
117c51de7f3SFlorian Fainelli 
bcmgenet_poll_wol_status(struct bcmgenet_priv * priv)118c51de7f3SFlorian Fainelli static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
119c51de7f3SFlorian Fainelli {
120c51de7f3SFlorian Fainelli 	struct net_device *dev = priv->dev;
121c51de7f3SFlorian Fainelli 	int retries = 0;
122c51de7f3SFlorian Fainelli 
123c51de7f3SFlorian Fainelli 	while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
124c51de7f3SFlorian Fainelli 		& RBUF_STATUS_WOL)) {
125c51de7f3SFlorian Fainelli 		retries++;
126c51de7f3SFlorian Fainelli 		if (retries > 5) {
127c51de7f3SFlorian Fainelli 			netdev_crit(dev, "polling wol mode timeout\n");
128c51de7f3SFlorian Fainelli 			return -ETIMEDOUT;
129c51de7f3SFlorian Fainelli 		}
130c51de7f3SFlorian Fainelli 		mdelay(1);
131c51de7f3SFlorian Fainelli 	}
132c51de7f3SFlorian Fainelli 
133c51de7f3SFlorian Fainelli 	return retries;
134c51de7f3SFlorian Fainelli }
135c51de7f3SFlorian Fainelli 
bcmgenet_set_mpd_password(struct bcmgenet_priv * priv)1366f768905SDoug Berger static void bcmgenet_set_mpd_password(struct bcmgenet_priv *priv)
1376f768905SDoug Berger {
1386f768905SDoug Berger 	bcmgenet_umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
1396f768905SDoug Berger 			     UMAC_MPD_PW_MS);
1406f768905SDoug Berger 	bcmgenet_umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
1416f768905SDoug Berger 			     UMAC_MPD_PW_LS);
1426f768905SDoug Berger }
1436f768905SDoug Berger 
bcmgenet_wol_power_down_cfg(struct bcmgenet_priv * priv,enum bcmgenet_power_mode mode)144c51de7f3SFlorian Fainelli int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
145c51de7f3SFlorian Fainelli 				enum bcmgenet_power_mode mode)
146c51de7f3SFlorian Fainelli {
147c51de7f3SFlorian Fainelli 	struct net_device *dev = priv->dev;
148f50932ccSDoug Berger 	struct bcmgenet_rxnfc_rule *rule;
149f50932ccSDoug Berger 	u32 reg, hfb_ctrl_reg, hfb_enable = 0;
150c51de7f3SFlorian Fainelli 	int retries = 0;
151c51de7f3SFlorian Fainelli 
152c51de7f3SFlorian Fainelli 	if (mode != GENET_POWER_WOL_MAGIC) {
153c51de7f3SFlorian Fainelli 		netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
154c51de7f3SFlorian Fainelli 		return -EINVAL;
155c51de7f3SFlorian Fainelli 	}
156c51de7f3SFlorian Fainelli 
15788f6c8bfSDoug Berger 	/* Can't suspend with WoL if MAC is still in reset */
15800d1a1bcSDoug Berger 	spin_lock_bh(&priv->reg_lock);
159c51de7f3SFlorian Fainelli 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
16088f6c8bfSDoug Berger 	if (reg & CMD_SW_RESET)
16188f6c8bfSDoug Berger 		reg &= ~CMD_SW_RESET;
16288f6c8bfSDoug Berger 
16388f6c8bfSDoug Berger 	/* disable RX */
164c51de7f3SFlorian Fainelli 	reg &= ~CMD_RX_EN;
165c51de7f3SFlorian Fainelli 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
16600d1a1bcSDoug Berger 	spin_unlock_bh(&priv->reg_lock);
167c51de7f3SFlorian Fainelli 	mdelay(10);
168c51de7f3SFlorian Fainelli 
169f50932ccSDoug Berger 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
170c51de7f3SFlorian Fainelli 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
171c51de7f3SFlorian Fainelli 		reg |= MPD_EN;
1726f768905SDoug Berger 		if (priv->wolopts & WAKE_MAGICSECURE) {
1736f768905SDoug Berger 			bcmgenet_set_mpd_password(priv);
1746f768905SDoug Berger 			reg |= MPD_PW_EN;
1756f768905SDoug Berger 		}
176c51de7f3SFlorian Fainelli 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
177f50932ccSDoug Berger 	}
178f50932ccSDoug Berger 
179f50932ccSDoug Berger 	hfb_ctrl_reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
180f50932ccSDoug Berger 	if (priv->wolopts & WAKE_FILTER) {
181f50932ccSDoug Berger 		list_for_each_entry(rule, &priv->rxnfc_list, list)
182f50932ccSDoug Berger 			if (rule->fs.ring_cookie == RX_CLS_FLOW_WAKE)
183f50932ccSDoug Berger 				hfb_enable |= (1 << rule->fs.location);
184f50932ccSDoug Berger 		reg = (hfb_ctrl_reg & ~RBUF_HFB_EN) | RBUF_ACPI_EN;
185f50932ccSDoug Berger 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
186f50932ccSDoug Berger 	}
187c51de7f3SFlorian Fainelli 
188c51de7f3SFlorian Fainelli 	/* Do not leave UniMAC in MPD mode only */
189c51de7f3SFlorian Fainelli 	retries = bcmgenet_poll_wol_status(priv);
190c51de7f3SFlorian Fainelli 	if (retries < 0) {
191c51de7f3SFlorian Fainelli 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
1926f768905SDoug Berger 		reg &= ~(MPD_EN | MPD_PW_EN);
193c51de7f3SFlorian Fainelli 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
194f50932ccSDoug Berger 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
195c51de7f3SFlorian Fainelli 		return retries;
196c51de7f3SFlorian Fainelli 	}
197c51de7f3SFlorian Fainelli 
198c51de7f3SFlorian Fainelli 	netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
199c51de7f3SFlorian Fainelli 		  retries);
200c51de7f3SFlorian Fainelli 
2011a1d5106SDoug Berger 	clk_prepare_enable(priv->clk_wol);
2021a1d5106SDoug Berger 	priv->wol_active = 1;
2031a1d5106SDoug Berger 
204f50932ccSDoug Berger 	if (hfb_enable) {
205f50932ccSDoug Berger 		bcmgenet_hfb_reg_writel(priv, hfb_enable,
206f50932ccSDoug Berger 					HFB_FLT_ENABLE_V3PLUS + 4);
207f50932ccSDoug Berger 		hfb_ctrl_reg = RBUF_HFB_EN | RBUF_ACPI_EN;
208f50932ccSDoug Berger 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
209f50932ccSDoug Berger 	}
210f50932ccSDoug Berger 
211c51de7f3SFlorian Fainelli 	/* Enable CRC forward */
21200d1a1bcSDoug Berger 	spin_lock_bh(&priv->reg_lock);
213c51de7f3SFlorian Fainelli 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
214c51de7f3SFlorian Fainelli 	priv->crc_fwd_en = 1;
215c51de7f3SFlorian Fainelli 	reg |= CMD_CRC_FWD;
216c51de7f3SFlorian Fainelli 
217c51de7f3SFlorian Fainelli 	/* Receiver must be enabled for WOL MP detection */
218c51de7f3SFlorian Fainelli 	reg |= CMD_RX_EN;
219c51de7f3SFlorian Fainelli 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
22000d1a1bcSDoug Berger 	spin_unlock_bh(&priv->reg_lock);
221c51de7f3SFlorian Fainelli 
222eb236c29SDoug Berger 	reg = UMAC_IRQ_MPD_R;
223eb236c29SDoug Berger 	if (hfb_enable)
224eb236c29SDoug Berger 		reg |=  UMAC_IRQ_HFB_SM | UMAC_IRQ_HFB_MM;
225eb236c29SDoug Berger 
226eb236c29SDoug Berger 	bcmgenet_intrl2_0_writel(priv, reg, INTRL2_CPU_MASK_CLEAR);
227eb236c29SDoug Berger 
228c51de7f3SFlorian Fainelli 	return 0;
229c51de7f3SFlorian Fainelli }
230c51de7f3SFlorian Fainelli 
bcmgenet_wol_power_up_cfg(struct bcmgenet_priv * priv,enum bcmgenet_power_mode mode)231c51de7f3SFlorian Fainelli void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
232c51de7f3SFlorian Fainelli 			       enum bcmgenet_power_mode mode)
233c51de7f3SFlorian Fainelli {
234c51de7f3SFlorian Fainelli 	u32 reg;
235c51de7f3SFlorian Fainelli 
236c51de7f3SFlorian Fainelli 	if (mode != GENET_POWER_WOL_MAGIC) {
237c51de7f3SFlorian Fainelli 		netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
238c51de7f3SFlorian Fainelli 		return;
239c51de7f3SFlorian Fainelli 	}
240c51de7f3SFlorian Fainelli 
2411a1d5106SDoug Berger 	if (!priv->wol_active)
2421a1d5106SDoug Berger 		return;	/* failed to suspend so skip the rest */
2431a1d5106SDoug Berger 
2441a1d5106SDoug Berger 	priv->wol_active = 0;
2451a1d5106SDoug Berger 	clk_disable_unprepare(priv->clk_wol);
2462f11f0dfSDoug Berger 	priv->crc_fwd_en = 0;
2471a1d5106SDoug Berger 
2481a1d5106SDoug Berger 	/* Disable Magic Packet Detection */
2492f11f0dfSDoug Berger 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
250c51de7f3SFlorian Fainelli 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
2512f11f0dfSDoug Berger 		if (!(reg & MPD_EN))
2522f11f0dfSDoug Berger 			return;	/* already reset so skip the rest */
2536f768905SDoug Berger 		reg &= ~(MPD_EN | MPD_PW_EN);
254c51de7f3SFlorian Fainelli 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
2552f11f0dfSDoug Berger 	}
256c51de7f3SFlorian Fainelli 
257f50932ccSDoug Berger 	/* Disable WAKE_FILTER Detection */
2583d653adbSDoug Berger 	if (priv->wolopts & WAKE_FILTER) {
259f50932ccSDoug Berger 		reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
2603d653adbSDoug Berger 		if (!(reg & RBUF_ACPI_EN))
2613d653adbSDoug Berger 			return;	/* already reset so skip the rest */
262f50932ccSDoug Berger 		reg &= ~(RBUF_HFB_EN | RBUF_ACPI_EN);
263f50932ccSDoug Berger 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
2643d653adbSDoug Berger 	}
265f50932ccSDoug Berger 
266c51de7f3SFlorian Fainelli 	/* Disable CRC Forward */
26700d1a1bcSDoug Berger 	spin_lock_bh(&priv->reg_lock);
268c51de7f3SFlorian Fainelli 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
269c51de7f3SFlorian Fainelli 	reg &= ~CMD_CRC_FWD;
270c51de7f3SFlorian Fainelli 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
27100d1a1bcSDoug Berger 	spin_unlock_bh(&priv->reg_lock);
272c51de7f3SFlorian Fainelli }
273