1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
4  *
5  * Copyright (c) 2014-2020 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 (!device_can_wakeup(kdev)) {
46 		wol->supported = 0;
47 		wol->wolopts = 0;
48 		return;
49 	}
50 
51 	wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
52 	wol->wolopts = priv->wolopts;
53 	memset(wol->sopass, 0, sizeof(wol->sopass));
54 
55 	if (wol->wolopts & WAKE_MAGICSECURE)
56 		memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
57 }
58 
59 /* ethtool function - set WOL (Wake on LAN) settings.
60  * Only for magic packet detection mode.
61  */
62 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
63 {
64 	struct bcmgenet_priv *priv = netdev_priv(dev);
65 	struct device *kdev = &priv->pdev->dev;
66 
67 	if (!device_can_wakeup(kdev))
68 		return -ENOTSUPP;
69 
70 	if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER))
71 		return -EINVAL;
72 
73 	if (wol->wolopts & WAKE_MAGICSECURE)
74 		memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
75 
76 	/* Flag the device and relevant IRQ as wakeup capable */
77 	if (wol->wolopts) {
78 		device_set_wakeup_enable(kdev, 1);
79 		/* Avoid unbalanced enable_irq_wake calls */
80 		if (priv->wol_irq_disabled) {
81 			enable_irq_wake(priv->wol_irq);
82 			enable_irq_wake(priv->irq0);
83 		}
84 		priv->wol_irq_disabled = false;
85 	} else {
86 		device_set_wakeup_enable(kdev, 0);
87 		/* Avoid unbalanced disable_irq_wake calls */
88 		if (!priv->wol_irq_disabled) {
89 			disable_irq_wake(priv->wol_irq);
90 			disable_irq_wake(priv->irq0);
91 		}
92 		priv->wol_irq_disabled = true;
93 	}
94 
95 	priv->wolopts = wol->wolopts;
96 
97 	return 0;
98 }
99 
100 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
101 {
102 	struct net_device *dev = priv->dev;
103 	int retries = 0;
104 
105 	while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
106 		& RBUF_STATUS_WOL)) {
107 		retries++;
108 		if (retries > 5) {
109 			netdev_crit(dev, "polling wol mode timeout\n");
110 			return -ETIMEDOUT;
111 		}
112 		mdelay(1);
113 	}
114 
115 	return retries;
116 }
117 
118 static void bcmgenet_set_mpd_password(struct bcmgenet_priv *priv)
119 {
120 	bcmgenet_umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
121 			     UMAC_MPD_PW_MS);
122 	bcmgenet_umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
123 			     UMAC_MPD_PW_LS);
124 }
125 
126 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
127 				enum bcmgenet_power_mode mode)
128 {
129 	struct net_device *dev = priv->dev;
130 	struct bcmgenet_rxnfc_rule *rule;
131 	u32 reg, hfb_ctrl_reg, hfb_enable = 0;
132 	int retries = 0;
133 
134 	if (mode != GENET_POWER_WOL_MAGIC) {
135 		netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
136 		return -EINVAL;
137 	}
138 
139 	/* Can't suspend with WoL if MAC is still in reset */
140 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
141 	if (reg & CMD_SW_RESET)
142 		reg &= ~CMD_SW_RESET;
143 
144 	/* disable RX */
145 	reg &= ~CMD_RX_EN;
146 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
147 	mdelay(10);
148 
149 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
150 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
151 		reg |= MPD_EN;
152 		if (priv->wolopts & WAKE_MAGICSECURE) {
153 			bcmgenet_set_mpd_password(priv);
154 			reg |= MPD_PW_EN;
155 		}
156 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
157 	}
158 
159 	hfb_ctrl_reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
160 	if (priv->wolopts & WAKE_FILTER) {
161 		list_for_each_entry(rule, &priv->rxnfc_list, list)
162 			if (rule->fs.ring_cookie == RX_CLS_FLOW_WAKE)
163 				hfb_enable |= (1 << rule->fs.location);
164 		reg = (hfb_ctrl_reg & ~RBUF_HFB_EN) | RBUF_ACPI_EN;
165 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
166 	}
167 
168 	/* Do not leave UniMAC in MPD mode only */
169 	retries = bcmgenet_poll_wol_status(priv);
170 	if (retries < 0) {
171 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
172 		reg &= ~(MPD_EN | MPD_PW_EN);
173 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
174 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
175 		return retries;
176 	}
177 
178 	netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
179 		  retries);
180 
181 	clk_prepare_enable(priv->clk_wol);
182 	priv->wol_active = 1;
183 
184 	if (hfb_enable) {
185 		bcmgenet_hfb_reg_writel(priv, hfb_enable,
186 					HFB_FLT_ENABLE_V3PLUS + 4);
187 		hfb_ctrl_reg = RBUF_HFB_EN | RBUF_ACPI_EN;
188 		bcmgenet_hfb_reg_writel(priv, hfb_ctrl_reg, HFB_CTRL);
189 	}
190 
191 	/* Enable CRC forward */
192 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
193 	priv->crc_fwd_en = 1;
194 	reg |= CMD_CRC_FWD;
195 
196 	/* Receiver must be enabled for WOL MP detection */
197 	reg |= CMD_RX_EN;
198 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
199 
200 	reg = UMAC_IRQ_MPD_R;
201 	if (hfb_enable)
202 		reg |=  UMAC_IRQ_HFB_SM | UMAC_IRQ_HFB_MM;
203 
204 	bcmgenet_intrl2_0_writel(priv, reg, INTRL2_CPU_MASK_CLEAR);
205 
206 	return 0;
207 }
208 
209 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
210 			       enum bcmgenet_power_mode mode)
211 {
212 	u32 reg;
213 
214 	if (mode != GENET_POWER_WOL_MAGIC) {
215 		netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
216 		return;
217 	}
218 
219 	if (!priv->wol_active)
220 		return;	/* failed to suspend so skip the rest */
221 
222 	priv->wol_active = 0;
223 	clk_disable_unprepare(priv->clk_wol);
224 	priv->crc_fwd_en = 0;
225 
226 	/* Disable Magic Packet Detection */
227 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
228 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
229 		if (!(reg & MPD_EN))
230 			return;	/* already reset so skip the rest */
231 		reg &= ~(MPD_EN | MPD_PW_EN);
232 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
233 	}
234 
235 	/* Disable WAKE_FILTER Detection */
236 	if (priv->wolopts & WAKE_FILTER) {
237 		reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
238 		if (!(reg & RBUF_ACPI_EN))
239 			return;	/* already reset so skip the rest */
240 		reg &= ~(RBUF_HFB_EN | RBUF_ACPI_EN);
241 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
242 	}
243 
244 	/* Disable CRC Forward */
245 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
246 	reg &= ~CMD_CRC_FWD;
247 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
248 }
249