linux/drivers/net/ethernet/broadcom/genet/bcmgenet_wol.c
<<
>>
Prefs
   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        reg = UMAC_IRQ_MPD_R;
 190        if (hfb_enable)
 191                reg |=  UMAC_IRQ_HFB_SM | UMAC_IRQ_HFB_MM;
 192
 193        bcmgenet_intrl2_0_writel(priv, reg, INTRL2_CPU_MASK_CLEAR);
 194
 195        return 0;
 196}
 197
 198void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
 199                               enum bcmgenet_power_mode mode)
 200{
 201        u32 reg;
 202
 203        if (mode != GENET_POWER_WOL_MAGIC) {
 204                netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
 205                return;
 206        }
 207
 208        if (!priv->wol_active)
 209                return; /* failed to suspend so skip the rest */
 210
 211        priv->wol_active = 0;
 212        clk_disable_unprepare(priv->clk_wol);
 213        priv->crc_fwd_en = 0;
 214
 215        /* Disable Magic Packet Detection */
 216        if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE)) {
 217                reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
 218                if (!(reg & MPD_EN))
 219                        return; /* already reset so skip the rest */
 220                reg &= ~(MPD_EN | MPD_PW_EN);
 221                bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
 222        }
 223
 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);
 231        }
 232
 233        /* Disable CRC Forward */
 234        reg = bcmgenet_umac_readl(priv, UMAC_CMD);
 235        reg &= ~CMD_CRC_FWD;
 236        bcmgenet_umac_writel(priv, reg, UMAC_CMD);
 237}
 238