1 /* 2 * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support 3 * 4 * Copyright (c) 2014 Broadcom Corporation 5 * 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. 9 */ 10 11 #define pr_fmt(fmt) "bcmgenet_wol: " fmt 12 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> 22 #include <linux/pm.h> 23 #include <linux/clk.h> 24 #include <linux/version.h> 25 #include <linux/platform_device.h> 26 #include <net/arp.h> 27 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> 34 #include <linux/in.h> 35 #include <linux/ip.h> 36 #include <linux/ipv6.h> 37 #include <linux/phy.h> 38 39 #include "bcmgenet.h" 40 41 /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet 42 * Detection is supported through ethtool 43 */ 44 void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 45 { 46 struct bcmgenet_priv *priv = netdev_priv(dev); 47 u32 reg; 48 49 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE; 50 wol->wolopts = priv->wolopts; 51 memset(wol->sopass, 0, sizeof(wol->sopass)); 52 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]); 58 } 59 } 60 61 /* ethtool function - set WOL (Wake on LAN) settings. 62 * Only for magic packet detection mode. 63 */ 64 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 65 { 66 struct bcmgenet_priv *priv = netdev_priv(dev); 67 struct device *kdev = &priv->pdev->dev; 68 u32 reg; 69 70 if (!device_can_wakeup(kdev)) 71 return -ENOTSUPP; 72 73 if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE)) 74 return -EINVAL; 75 76 if (wol->wolopts & WAKE_MAGICSECURE) { 77 bcmgenet_umac_writel(priv, get_unaligned_be16(&wol->sopass[0]), 78 UMAC_MPD_PW_MS); 79 bcmgenet_umac_writel(priv, get_unaligned_be32(&wol->sopass[2]), 80 UMAC_MPD_PW_LS); 81 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL); 82 reg |= MPD_PW_EN; 83 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL); 84 } 85 86 /* Flag the device and relevant IRQ as wakeup capable */ 87 if (wol->wolopts) { 88 device_set_wakeup_enable(kdev, 1); 89 /* Avoid unbalanced enable_irq_wake calls */ 90 if (priv->wol_irq_disabled) 91 enable_irq_wake(priv->wol_irq); 92 priv->wol_irq_disabled = false; 93 } else { 94 device_set_wakeup_enable(kdev, 0); 95 /* Avoid unbalanced disable_irq_wake calls */ 96 if (!priv->wol_irq_disabled) 97 disable_irq_wake(priv->wol_irq); 98 priv->wol_irq_disabled = true; 99 } 100 101 priv->wolopts = wol->wolopts; 102 103 return 0; 104 } 105 106 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv) 107 { 108 struct net_device *dev = priv->dev; 109 int retries = 0; 110 111 while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS) 112 & RBUF_STATUS_WOL)) { 113 retries++; 114 if (retries > 5) { 115 netdev_crit(dev, "polling wol mode timeout\n"); 116 return -ETIMEDOUT; 117 } 118 mdelay(1); 119 } 120 121 return retries; 122 } 123 124 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv, 125 enum bcmgenet_power_mode mode) 126 { 127 struct net_device *dev = priv->dev; 128 u32 cpu_mask_clear; 129 int retries = 0; 130 u32 reg; 131 132 if (mode != GENET_POWER_WOL_MAGIC) { 133 netif_err(priv, wol, dev, "unsupported mode: %d\n", mode); 134 return -EINVAL; 135 } 136 137 /* disable RX */ 138 reg = bcmgenet_umac_readl(priv, UMAC_CMD); 139 reg &= ~CMD_RX_EN; 140 bcmgenet_umac_writel(priv, reg, UMAC_CMD); 141 mdelay(10); 142 143 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL); 144 reg |= MPD_EN; 145 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL); 146 147 /* Do not leave UniMAC in MPD mode only */ 148 retries = bcmgenet_poll_wol_status(priv); 149 if (retries < 0) { 150 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL); 151 reg &= ~MPD_EN; 152 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL); 153 return retries; 154 } 155 156 netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n", 157 retries); 158 159 /* Enable CRC forward */ 160 reg = bcmgenet_umac_readl(priv, UMAC_CMD); 161 priv->crc_fwd_en = 1; 162 reg |= CMD_CRC_FWD; 163 164 /* Receiver must be enabled for WOL MP detection */ 165 reg |= CMD_RX_EN; 166 bcmgenet_umac_writel(priv, reg, UMAC_CMD); 167 168 if (priv->hw_params->flags & GENET_HAS_EXT) { 169 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT); 170 reg &= ~EXT_ENERGY_DET_MASK; 171 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT); 172 } 173 174 /* Enable the MPD interrupt */ 175 cpu_mask_clear = UMAC_IRQ_MPD_R; 176 177 bcmgenet_intrl2_0_writel(priv, cpu_mask_clear, INTRL2_CPU_MASK_CLEAR); 178 179 return 0; 180 } 181 182 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv, 183 enum bcmgenet_power_mode mode) 184 { 185 u32 cpu_mask_set; 186 u32 reg; 187 188 if (mode != GENET_POWER_WOL_MAGIC) { 189 netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode); 190 return; 191 } 192 193 reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL); 194 reg &= ~MPD_EN; 195 bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL); 196 197 /* Disable CRC Forward */ 198 reg = bcmgenet_umac_readl(priv, UMAC_CMD); 199 reg &= ~CMD_CRC_FWD; 200 bcmgenet_umac_writel(priv, reg, UMAC_CMD); 201 priv->crc_fwd_en = 0; 202 203 /* Stop monitoring magic packet IRQ */ 204 cpu_mask_set = UMAC_IRQ_MPD_R; 205 206 /* Stop monitoring magic packet IRQ */ 207 bcmgenet_intrl2_0_writel(priv, cpu_mask_set, INTRL2_CPU_MASK_SET); 208 } 209