1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
4  *
5  * Copyright (c) 2014-2024 Broadcom
6  */
7 
8 #define pr_fmt(fmt)				"bcmgenet_wol: " fmt
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/string.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
22 #include <net/arp.h>
23 
24 #include <linux/mii.h>
25 #include <linux/ethtool.h>
26 #include <linux/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/ipv6.h>
33 #include <linux/phy.h>
34 
35 #include "bcmgenet.h"
36 
37 /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
38  * Detection is supported through ethtool
39  */
40 void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
41 {
42 	struct bcmgenet_priv *priv = netdev_priv(dev);
43 	struct device *kdev = &priv->pdev->dev;
44 
45 	if (dev->phydev) {
46 		phy_ethtool_get_wol(dev->phydev, wol);
47 		if (wol->supported)
48 			return;
49 	}
50 
51 	if (!device_can_wakeup(kdev)) {
52 		wol->supported = 0;
53 		wol->wolopts = 0;
54 		return;
55 	}
56 
57 	wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
58 	wol->wolopts = priv->wolopts;
59 	memset(wol->sopass, 0, sizeof(wol->sopass));
60 
61 	if (wol->wolopts & WAKE_MAGICSECURE)
62 		memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
63 }
64 
65 /* ethtool function - set WOL (Wake on LAN) settings.
66  * Only for magic packet detection mode.
67  */
68 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
69 {
70 	struct bcmgenet_priv *priv = netdev_priv(dev);
71 	struct device *kdev = &priv->pdev->dev;
72 	int ret;
73 
74 	/* Try Wake-on-LAN from the PHY first */
75 	if (dev->phydev) {
76 		ret = phy_ethtool_set_wol(dev->phydev, wol);
77 		if (ret != -EOPNOTSUPP)
78 			return ret;
79 	}
80 
81 	if (!device_can_wakeup(kdev))
82 		return -ENOTSUPP;
83 
84 	if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER))
85 		return -EINVAL;
86 
87 	if (wol->wolopts & WAKE_MAGICSECURE)
88 		memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
89 
90 	/* Flag the device and relevant IRQ as wakeup capable */
91 	if (wol->wolopts) {
92 		device_set_wakeup_enable(kdev, 1);
93 		/* Avoid unbalanced enable_irq_wake calls */
94 		if (priv->wol_irq_disabled) {
95 			enable_irq_wake(priv->wol_irq);
96 			enable_irq_wake(priv->irq0);
97 		}
98 		priv->wol_irq_disabled = false;
99 	} else {
100 		device_set_wakeup_enable(kdev, 0);
101 		/* Avoid unbalanced disable_irq_wake calls */
102 		if (!priv->wol_irq_disabled) {
103 			disable_irq_wake(priv->wol_irq);
104 			disable_irq_wake(priv->irq0);
105 		}
106 		priv->wol_irq_disabled = true;
107 	}
108 
109 	priv->wolopts = wol->wolopts;
110 
111 	return 0;
112 }
113 
114 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
115 {
116 	struct net_device *dev = priv->dev;
117 	int retries = 0;
118 
119 	while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
120 		& RBUF_STATUS_WOL)) {
121 		retries++;
122 		if (retries > 5) {
123 			netdev_crit(dev, "polling wol mode timeout\n");
124 			return -ETIMEDOUT;
125 		}
126 		mdelay(1);
127 	}
128 
129 	return retries;
130 }
131 
132 static void bcmgenet_set_mpd_password(struct bcmgenet_priv *priv)
133 {
134 	bcmgenet_umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
135 			     UMAC_MPD_PW_MS);
136 	bcmgenet_umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
137 			     UMAC_MPD_PW_LS);
138 }
139 
140 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
141 				enum bcmgenet_power_mode mode)
142 {
143 	struct net_device *dev = priv->dev;
144 	struct bcmgenet_rxnfc_rule *rule;
145 	u32 reg, hfb_ctrl_reg, hfb_enable = 0;
146 	int retries = 0;
147 
148 	if (mode != GENET_POWER_WOL_MAGIC) {
149 		netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
150 		return -EINVAL;
151 	}
152 
153 	/* Can't suspend with WoL if MAC is still in reset */
154 	spin_lock_bh(&priv->reg_lock);
155 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
156 	if (reg & CMD_SW_RESET)
157 		reg &= ~CMD_SW_RESET;
158 
159 	/* disable RX */
160 	reg &= ~CMD_RX_EN;
161 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
162 	spin_unlock_bh(&priv->reg_lock);
163 	mdelay(10);
164 
165 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
166 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
167 		reg |= MPD_EN;
168 		if (priv->wolopts & WAKE_MAGICSECURE) {
169 			bcmgenet_set_mpd_password(priv);
170 			reg |= MPD_PW_EN;
171 		}
172 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
173 	}
174 
175 	hfb_ctrl_reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
176 	if (priv->wolopts & WAKE_FILTER) {
177 		list_for_each_entry(rule, &priv->rxnfc_list, list)
178 			if (rule->fs.ring_cookie == RX_CLS_FLOW_WAKE)
179 				hfb_enable |= (1 << rule->fs.location);
180 		reg = (hfb_ctrl_reg & ~RBUF_HFB_EN) | RBUF_ACPI_EN;
181 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
182 	}
183 
184 	/* Do not leave UniMAC in MPD mode only */
185 	retries = bcmgenet_poll_wol_status(priv);
186 	if (retries < 0) {
187 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
188 		reg &= ~(MPD_EN | MPD_PW_EN);
189 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
190 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
191 		return retries;
192 	}
193 
194 	netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
195 		  retries);
196 
197 	clk_prepare_enable(priv->clk_wol);
198 	priv->wol_active = 1;
199 
200 	if (hfb_enable) {
201 		bcmgenet_hfb_reg_writel(priv, hfb_enable,
202 					HFB_FLT_ENABLE_V3PLUS + 4);
203 		hfb_ctrl_reg = RBUF_HFB_EN | RBUF_ACPI_EN;
204 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
205 	}
206 
207 	/* Enable CRC forward */
208 	spin_lock_bh(&priv->reg_lock);
209 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
210 	priv->crc_fwd_en = 1;
211 	reg |= CMD_CRC_FWD;
212 
213 	/* Receiver must be enabled for WOL MP detection */
214 	reg |= CMD_RX_EN;
215 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
216 	spin_unlock_bh(&priv->reg_lock);
217 
218 	reg = UMAC_IRQ_MPD_R;
219 	if (hfb_enable)
220 		reg |=  UMAC_IRQ_HFB_SM | UMAC_IRQ_HFB_MM;
221 
222 	bcmgenet_intrl2_0_writel(priv, reg, INTRL2_CPU_MASK_CLEAR);
223 
224 	return 0;
225 }
226 
227 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
228 			       enum bcmgenet_power_mode mode)
229 {
230 	u32 reg;
231 
232 	if (mode != GENET_POWER_WOL_MAGIC) {
233 		netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
234 		return;
235 	}
236 
237 	if (!priv->wol_active)
238 		return;	/* failed to suspend so skip the rest */
239 
240 	priv->wol_active = 0;
241 	clk_disable_unprepare(priv->clk_wol);
242 	priv->crc_fwd_en = 0;
243 
244 	/* Disable Magic Packet Detection */
245 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
246 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
247 		if (!(reg & MPD_EN))
248 			return;	/* already reset so skip the rest */
249 		reg &= ~(MPD_EN | MPD_PW_EN);
250 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
251 	}
252 
253 	/* Disable WAKE_FILTER Detection */
254 	if (priv->wolopts & WAKE_FILTER) {
255 		reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
256 		if (!(reg & RBUF_ACPI_EN))
257 			return;	/* already reset so skip the rest */
258 		reg &= ~(RBUF_HFB_EN | RBUF_ACPI_EN);
259 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
260 	}
261 
262 	/* Disable CRC Forward */
263 	spin_lock_bh(&priv->reg_lock);
264 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
265 	reg &= ~CMD_CRC_FWD;
266 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
267 	spin_unlock_bh(&priv->reg_lock);
268 }
269