1 /*
2  * Broadcom GENET (Gigabit Ethernet) Wake-on-LAN support
3  *
4  * Copyright (c) 2014-2017 Broadcom
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 	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]),
79 				     UMAC_MPD_PW_MS);
80 		bcmgenet_umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
81 				     UMAC_MPD_PW_LS);
82 		reg |= MPD_PW_EN;
83 	} else {
84 		reg &= ~MPD_PW_EN;
85 	}
86 	bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
87 
88 	/* Flag the device and relevant IRQ as wakeup capable */
89 	if (wol->wolopts) {
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;
95 	} else {
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;
101 	}
102 
103 	priv->wolopts = wol->wolopts;
104 
105 	return 0;
106 }
107 
108 static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
109 {
110 	struct net_device *dev = priv->dev;
111 	int retries = 0;
112 
113 	while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
114 		& RBUF_STATUS_WOL)) {
115 		retries++;
116 		if (retries > 5) {
117 			netdev_crit(dev, "polling wol mode timeout\n");
118 			return -ETIMEDOUT;
119 		}
120 		mdelay(1);
121 	}
122 
123 	return retries;
124 }
125 
126 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
127 				enum bcmgenet_power_mode mode)
128 {
129 	struct net_device *dev = priv->dev;
130 	int retries = 0;
131 	u32 reg;
132 
133 	if (mode != GENET_POWER_WOL_MAGIC) {
134 		netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
135 		return -EINVAL;
136 	}
137 
138 	/* disable RX */
139 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
140 	reg &= ~CMD_RX_EN;
141 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
142 	mdelay(10);
143 
144 	reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
145 	reg |= MPD_EN;
146 	bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
147 
148 	/* Do not leave UniMAC in MPD mode only */
149 	retries = bcmgenet_poll_wol_status(priv);
150 	if (retries < 0) {
151 		reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
152 		reg &= ~MPD_EN;
153 		bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
154 		return retries;
155 	}
156 
157 	netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
158 		  retries);
159 
160 	/* Enable CRC forward */
161 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
162 	priv->crc_fwd_en = 1;
163 	reg |= CMD_CRC_FWD;
164 
165 	/* Receiver must be enabled for WOL MP detection */
166 	reg |= CMD_RX_EN;
167 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
168 
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);
173 	}
174 
175 	return 0;
176 }
177 
178 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
179 			       enum bcmgenet_power_mode mode)
180 {
181 	u32 reg;
182 
183 	if (mode != GENET_POWER_WOL_MAGIC) {
184 		netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
185 		return;
186 	}
187 
188 	reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
189 	reg &= ~MPD_EN;
190 	bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
191 
192 	/* Disable CRC Forward */
193 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
194 	reg &= ~CMD_CRC_FWD;
195 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
196 	priv->crc_fwd_en = 0;
197 }
198