1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
5 * Copyright (c) 2014-2020 Broadcom
8 #define pr_fmt(fmt) "bcmgenet_wol: " fmt
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>
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
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>
32 #include <linux/ipv6.h>
33 #include <linux/phy.h>
37 /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
38 * Detection is supported through ethtool
40 void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
42 struct bcmgenet_priv *priv = netdev_priv(dev);
44 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
45 wol->wolopts = priv->wolopts;
46 memset(wol->sopass, 0, sizeof(wol->sopass));
48 if (wol->wolopts & WAKE_MAGICSECURE)
49 memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
52 /* ethtool function - set WOL (Wake on LAN) settings.
53 * Only for magic packet detection mode.
55 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
57 struct bcmgenet_priv *priv = netdev_priv(dev);
58 struct device *kdev = &priv->pdev->dev;
60 if (!device_can_wakeup(kdev))
63 if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER))
66 if (wol->wolopts & WAKE_MAGICSECURE)
67 memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
69 /* Flag the device and relevant IRQ as wakeup capable */
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;
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;
84 priv->wolopts = wol->wolopts;
89 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
91 struct net_device *dev = priv->dev;
94 while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
98 netdev_crit(dev, "polling wol mode timeout\n");
107 static void bcmgenet_set_mpd_password(struct bcmgenet_priv *priv)
109 bcmgenet_umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
111 bcmgenet_umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
115 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
116 enum bcmgenet_power_mode mode)
118 struct net_device *dev = priv->dev;
119 struct bcmgenet_rxnfc_rule *rule;
120 u32 reg, hfb_ctrl_reg, hfb_enable = 0;
123 if (mode != GENET_POWER_WOL_MAGIC) {
124 netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
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;
135 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
138 if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
139 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
141 if (priv->wolopts & WAKE_MAGICSECURE) {
142 bcmgenet_set_mpd_password(priv);
145 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
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);
157 /* Do not leave UniMAC in MPD mode only */
158 retries = bcmgenet_poll_wol_status(priv);
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);
167 netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
170 clk_prepare_enable(priv->clk_wol);
171 priv->wol_active = 1;
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);
180 /* Enable CRC forward */
181 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
182 priv->crc_fwd_en = 1;
185 /* Receiver must be enabled for WOL MP detection */
187 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
189 reg = UMAC_IRQ_MPD_R;
191 reg |= UMAC_IRQ_HFB_SM | UMAC_IRQ_HFB_MM;
193 bcmgenet_intrl2_0_writel(priv, reg, INTRL2_CPU_MASK_CLEAR);
198 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
199 enum bcmgenet_power_mode mode)
203 if (mode != GENET_POWER_WOL_MAGIC) {
204 netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
208 if (!priv->wol_active)
209 return; /* failed to suspend so skip the rest */
211 priv->wol_active = 0;
212 clk_disable_unprepare(priv->clk_wol);
213 priv->crc_fwd_en = 0;
215 /* Disable Magic Packet Detection */
216 if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
217 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
219 return; /* already reset so skip the rest */
220 reg &= ~(MPD_EN | MPD_PW_EN);
221 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
224 /* Disable WAKE_FILTER Detection */
225 if (priv->wolopts & WAKE_FILTER) {
226 reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
227 if (!(reg & RBUF_ACPI_EN))
228 return; /* already reset so skip the rest */
229 reg &= ~(RBUF_HFB_EN | RBUF_ACPI_EN);
230 bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
233 /* Disable CRC Forward */
234 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
236 bcmgenet_umac_writel(priv, reg, UMAC_CMD);