2 * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
4 * Copyright (c) 2014-2017 Broadcom
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) "bcmgenet_wol: " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/types.h>
17 #include <linux/interrupt.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
23 #include <linux/clk.h>
24 #include <linux/version.h>
25 #include <linux/platform_device.h>
28 #include <linux/mii.h>
29 #include <linux/ethtool.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
36 #include <linux/ipv6.h>
37 #include <linux/phy.h>
41 /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
42 * Detection is supported through ethtool
44 void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
46 struct bcmgenet_priv *priv = netdev_priv(dev);
49 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
50 wol->wolopts = priv->wolopts;
51 memset(wol->sopass, 0, sizeof(wol->sopass));
53 if (wol->wolopts & WAKE_MAGICSECURE) {
54 reg = bcmgenet_umac_readl(priv, UMAC_MPD_PW_MS);
55 put_unaligned_be16(reg, &wol->sopass[0]);
56 reg = bcmgenet_umac_readl(priv, UMAC_MPD_PW_LS);
57 put_unaligned_be32(reg, &wol->sopass[2]);
61 /* ethtool function - set WOL (Wake on LAN) settings.
62 * Only for magic packet detection mode.
64 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
66 struct bcmgenet_priv *priv = netdev_priv(dev);
67 struct device *kdev = &priv->pdev->dev;
70 if (!device_can_wakeup(kdev))
73 if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE))
76 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
77 if (wol->wolopts & WAKE_MAGICSECURE) {
78 bcmgenet_umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
80 bcmgenet_umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
86 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
88 /* Flag the device and relevant IRQ as wakeup capable */
90 device_set_wakeup_enable(kdev, 1);
91 /* Avoid unbalanced enable_irq_wake calls */
92 if (priv->wol_irq_disabled)
93 enable_irq_wake(priv->wol_irq);
94 priv->wol_irq_disabled = false;
96 device_set_wakeup_enable(kdev, 0);
97 /* Avoid unbalanced disable_irq_wake calls */
98 if (!priv->wol_irq_disabled)
99 disable_irq_wake(priv->wol_irq);
100 priv->wol_irq_disabled = true;
103 priv->wolopts = wol->wolopts;
108 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
110 struct net_device *dev = priv->dev;
113 while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
114 & RBUF_STATUS_WOL)) {
117 netdev_crit(dev, "polling wol mode timeout\n");
126 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
127 enum bcmgenet_power_mode mode)
129 struct net_device *dev = priv->dev;
133 if (mode != GENET_POWER_WOL_MAGIC) {
134 netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
139 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
141 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
144 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
146 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
148 /* Do not leave UniMAC in MPD mode only */
149 retries = bcmgenet_poll_wol_status(priv);
151 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
153 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
157 netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
160 /* Enable CRC forward */
161 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
162 priv->crc_fwd_en = 1;
165 /* Receiver must be enabled for WOL MP detection */
167 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
169 if (priv->hw_params->flags & GENET_HAS_EXT) {
170 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
171 reg &= ~EXT_ENERGY_DET_MASK;
172 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
178 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
179 enum bcmgenet_power_mode mode)
183 if (mode != GENET_POWER_WOL_MAGIC) {
184 netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
188 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
190 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
192 /* Disable CRC Forward */
193 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
195 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
196 priv->crc_fwd_en = 0;