1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
5 * Copyright (c) 2014-2017 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/version.h>
22 #include <linux/platform_device.h>
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>
33 #include <linux/ipv6.h>
34 #include <linux/phy.h>
38 /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
39 * Detection is supported through ethtool
41 void bcmgenet_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
43 struct bcmgenet_priv
*priv
= netdev_priv(dev
);
46 wol
->supported
= WAKE_MAGIC
| WAKE_MAGICSECURE
;
47 wol
->wolopts
= priv
->wolopts
;
48 memset(wol
->sopass
, 0, sizeof(wol
->sopass
));
50 if (wol
->wolopts
& WAKE_MAGICSECURE
) {
51 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_PW_MS
);
52 put_unaligned_be16(reg
, &wol
->sopass
[0]);
53 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_PW_LS
);
54 put_unaligned_be32(reg
, &wol
->sopass
[2]);
58 /* ethtool function - set WOL (Wake on LAN) settings.
59 * Only for magic packet detection mode.
61 int bcmgenet_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
63 struct bcmgenet_priv
*priv
= netdev_priv(dev
);
64 struct device
*kdev
= &priv
->pdev
->dev
;
67 if (!device_can_wakeup(kdev
))
70 if (wol
->wolopts
& ~(WAKE_MAGIC
| WAKE_MAGICSECURE
))
73 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_CTRL
);
74 if (wol
->wolopts
& WAKE_MAGICSECURE
) {
75 bcmgenet_umac_writel(priv
, get_unaligned_be16(&wol
->sopass
[0]),
77 bcmgenet_umac_writel(priv
, get_unaligned_be32(&wol
->sopass
[2]),
83 bcmgenet_umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
85 /* Flag the device and relevant IRQ as wakeup capable */
87 device_set_wakeup_enable(kdev
, 1);
88 /* Avoid unbalanced enable_irq_wake calls */
89 if (priv
->wol_irq_disabled
)
90 enable_irq_wake(priv
->wol_irq
);
91 priv
->wol_irq_disabled
= false;
93 device_set_wakeup_enable(kdev
, 0);
94 /* Avoid unbalanced disable_irq_wake calls */
95 if (!priv
->wol_irq_disabled
)
96 disable_irq_wake(priv
->wol_irq
);
97 priv
->wol_irq_disabled
= true;
100 priv
->wolopts
= wol
->wolopts
;
105 static int bcmgenet_poll_wol_status(struct bcmgenet_priv
*priv
)
107 struct net_device
*dev
= priv
->dev
;
110 while (!(bcmgenet_rbuf_readl(priv
, RBUF_STATUS
)
111 & RBUF_STATUS_WOL
)) {
114 netdev_crit(dev
, "polling wol mode timeout\n");
123 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv
*priv
,
124 enum bcmgenet_power_mode mode
)
126 struct net_device
*dev
= priv
->dev
;
130 if (mode
!= GENET_POWER_WOL_MAGIC
) {
131 netif_err(priv
, wol
, dev
, "unsupported mode: %d\n", mode
);
135 /* Can't suspend with WoL if MAC is still in reset */
136 reg
= bcmgenet_umac_readl(priv
, UMAC_CMD
);
137 if (reg
& CMD_SW_RESET
)
138 reg
&= ~CMD_SW_RESET
;
142 bcmgenet_umac_writel(priv
, reg
, UMAC_CMD
);
145 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_CTRL
);
147 bcmgenet_umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
149 /* Do not leave UniMAC in MPD mode only */
150 retries
= bcmgenet_poll_wol_status(priv
);
152 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_CTRL
);
154 bcmgenet_umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
158 netif_dbg(priv
, wol
, dev
, "MPD WOL-ready status set after %d msec\n",
161 /* Enable CRC forward */
162 reg
= bcmgenet_umac_readl(priv
, UMAC_CMD
);
163 priv
->crc_fwd_en
= 1;
166 /* Receiver must be enabled for WOL MP detection */
168 bcmgenet_umac_writel(priv
, reg
, UMAC_CMD
);
170 if (priv
->hw_params
->flags
& GENET_HAS_EXT
) {
171 reg
= bcmgenet_ext_readl(priv
, EXT_EXT_PWR_MGMT
);
172 reg
&= ~EXT_ENERGY_DET_MASK
;
173 bcmgenet_ext_writel(priv
, reg
, EXT_EXT_PWR_MGMT
);
179 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv
*priv
,
180 enum bcmgenet_power_mode mode
)
184 if (mode
!= GENET_POWER_WOL_MAGIC
) {
185 netif_err(priv
, wol
, priv
->dev
, "invalid mode: %d\n", mode
);
189 reg
= bcmgenet_umac_readl(priv
, UMAC_MPD_CTRL
);
191 return; /* already powered up so skip the rest */
193 bcmgenet_umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
195 /* Disable CRC Forward */
196 reg
= bcmgenet_umac_readl(priv
, UMAC_CMD
);
198 bcmgenet_umac_writel(priv
, reg
, UMAC_CMD
);
199 priv
->crc_fwd_en
= 0;