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