blob: a41f82379369a32043b042478412bf858bc57f87 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0-only
Googleraf606d22022-10-26 21:40:12 -07002/*
3 * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
4 *
Googler9398cc32022-12-02 17:21:52 +08005 * Copyright (c) 2014-2017 Broadcom
Googleraf606d22022-10-26 21:40:12 -07006 */
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/version.h>
22#include <linux/platform_device.h>
23#include <net/arp.h>
24
25#include <linux/mii.h>
26#include <linux/ethtool.h>
27#include <linux/netdevice.h>
28#include <linux/inetdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
31#include <linux/in.h>
32#include <linux/ip.h>
33#include <linux/ipv6.h>
34#include <linux/phy.h>
35
36#include "bcmgenet.h"
37
38/* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
39 * Detection is supported through ethtool
40 */
41void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
42{
43 struct bcmgenet_priv *priv = netdev_priv(dev);
44
45 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
46 wol->wolopts = priv->wolopts;
47 memset(wol->sopass, 0, sizeof(wol->sopass));
48
Googler9398cc32022-12-02 17:21:52 +080049 if (wol->wolopts & WAKE_MAGICSECURE)
50 memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
Googleraf606d22022-10-26 21:40:12 -070051}
52
53/* ethtool function - set WOL (Wake on LAN) settings.
54 * Only for magic packet detection mode.
55 */
56int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
57{
58 struct bcmgenet_priv *priv = netdev_priv(dev);
59 struct device *kdev = &priv->pdev->dev;
60
61 if (!device_can_wakeup(kdev))
62 return -ENOTSUPP;
63
64 if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE))
65 return -EINVAL;
66
Googler9398cc32022-12-02 17:21:52 +080067 if (wol->wolopts & WAKE_MAGICSECURE)
68 memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
Googleraf606d22022-10-26 21:40:12 -070069
70 /* Flag the device and relevant IRQ as wakeup capable */
71 if (wol->wolopts) {
72 device_set_wakeup_enable(kdev, 1);
73 /* Avoid unbalanced enable_irq_wake calls */
74 if (priv->wol_irq_disabled)
75 enable_irq_wake(priv->wol_irq);
76 priv->wol_irq_disabled = false;
77 } else {
78 device_set_wakeup_enable(kdev, 0);
79 /* Avoid unbalanced disable_irq_wake calls */
80 if (!priv->wol_irq_disabled)
81 disable_irq_wake(priv->wol_irq);
82 priv->wol_irq_disabled = true;
83 }
84
85 priv->wolopts = wol->wolopts;
86
87 return 0;
88}
89
90static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
91{
92 struct net_device *dev = priv->dev;
93 int retries = 0;
94
95 while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
96 & RBUF_STATUS_WOL)) {
97 retries++;
98 if (retries > 5) {
99 netdev_crit(dev, "polling wol mode timeout\n");
100 return -ETIMEDOUT;
101 }
102 mdelay(1);
103 }
104
105 return retries;
106}
107
Googler9398cc32022-12-02 17:21:52 +0800108static void bcmgenet_set_mpd_password(struct bcmgenet_priv *priv)
109{
110 bcmgenet_umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
111 UMAC_MPD_PW_MS);
112 bcmgenet_umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
113 UMAC_MPD_PW_LS);
114}
115
Googleraf606d22022-10-26 21:40:12 -0700116int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
117 enum bcmgenet_power_mode mode)
118{
119 struct net_device *dev = priv->dev;
120 int retries = 0;
121 u32 reg;
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 /* disable RX */
129 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
130 reg &= ~CMD_RX_EN;
131 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
132 mdelay(10);
133
134 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
135 reg |= MPD_EN;
Googler9398cc32022-12-02 17:21:52 +0800136 if (priv->wolopts & WAKE_MAGICSECURE) {
137 bcmgenet_set_mpd_password(priv);
138 reg |= MPD_PW_EN;
139 }
Googleraf606d22022-10-26 21:40:12 -0700140 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
141
142 /* Do not leave UniMAC in MPD mode only */
143 retries = bcmgenet_poll_wol_status(priv);
144 if (retries < 0) {
145 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
Googler9398cc32022-12-02 17:21:52 +0800146 reg &= ~(MPD_EN | MPD_PW_EN);
Googleraf606d22022-10-26 21:40:12 -0700147 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
148 return retries;
149 }
150
151 netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
152 retries);
153
154 /* Enable CRC forward */
155 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
156 priv->crc_fwd_en = 1;
157 reg |= CMD_CRC_FWD;
158
159 /* Receiver must be enabled for WOL MP detection */
160 reg |= CMD_RX_EN;
161 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
162
Googlerb48fa912023-03-17 12:40:29 +0530163 if (priv->hw_params->flags & GENET_HAS_EXT) {
164 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
165 reg &= ~EXT_ENERGY_DET_MASK;
166 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
167 }
168
Googleraf606d22022-10-26 21:40:12 -0700169 return 0;
170}
171
172void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
173 enum bcmgenet_power_mode mode)
174{
175 u32 reg;
176
177 if (mode != GENET_POWER_WOL_MAGIC) {
178 netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
179 return;
180 }
181
182 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
Googler9398cc32022-12-02 17:21:52 +0800183 if (!(reg & MPD_EN))
184 return; /* already powered up so skip the rest */
185 reg &= ~(MPD_EN | MPD_PW_EN);
Googleraf606d22022-10-26 21:40:12 -0700186 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
187
188 /* Disable CRC Forward */
189 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
190 reg &= ~CMD_CRC_FWD;
191 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
192 priv->crc_fwd_en = 0;
Googleraf606d22022-10-26 21:40:12 -0700193}