bcmgenet_wol.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #define pr_fmt(fmt) "bcmgenet_wol: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/delay.h>
  20. #include <linux/pm.h>
  21. #include <linux/clk.h>
  22. #include <linux/version.h>
  23. #include <linux/platform_device.h>
  24. #include <net/arp.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>
  31. #include <linux/in.h>
  32. #include <linux/ip.h>
  33. #include <linux/ipv6.h>
  34. #include <linux/phy.h>
  35. #include "bcmgenet.h"
  36. /* ethtool function - get WOL (Wake on LAN) settings, Only Magic Packet
  37. * Detection is supported through ethtool
  38. */
  39. void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  40. {
  41. struct bcmgenet_priv *priv = netdev_priv(dev);
  42. u32 reg;
  43. wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
  44. wol->wolopts = priv->wolopts;
  45. memset(wol->sopass, 0, sizeof(wol->sopass));
  46. if (wol->wolopts & WAKE_MAGICSECURE) {
  47. reg = bcmgenet_umac_readl(priv, UMAC_MPD_PW_MS);
  48. put_unaligned_be16(reg, &wol->sopass[0]);
  49. reg = bcmgenet_umac_readl(priv, UMAC_MPD_PW_LS);
  50. put_unaligned_be32(reg, &wol->sopass[2]);
  51. }
  52. }
  53. /* ethtool function - set WOL (Wake on LAN) settings.
  54. * Only for magic packet detection mode.
  55. */
  56. int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  57. {
  58. struct bcmgenet_priv *priv = netdev_priv(dev);
  59. struct device *kdev = &priv->pdev->dev;
  60. u32 reg;
  61. if (!device_can_wakeup(kdev))
  62. return -ENOTSUPP;
  63. if (wol->wolopts & ~(WAKE_MAGIC | WAKE_MAGICSECURE))
  64. return -EINVAL;
  65. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  66. if (wol->wolopts & WAKE_MAGICSECURE) {
  67. bcmgenet_umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
  68. UMAC_MPD_PW_MS);
  69. bcmgenet_umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
  70. UMAC_MPD_PW_LS);
  71. reg |= MPD_PW_EN;
  72. } else {
  73. reg &= ~MPD_PW_EN;
  74. }
  75. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  76. /* Flag the device and relevant IRQ as wakeup capable */
  77. if (wol->wolopts) {
  78. device_set_wakeup_enable(kdev, 1);
  79. /* Avoid unbalanced enable_irq_wake calls */
  80. if (priv->wol_irq_disabled)
  81. enable_irq_wake(priv->wol_irq);
  82. priv->wol_irq_disabled = false;
  83. } else {
  84. device_set_wakeup_enable(kdev, 0);
  85. /* Avoid unbalanced disable_irq_wake calls */
  86. if (!priv->wol_irq_disabled)
  87. disable_irq_wake(priv->wol_irq);
  88. priv->wol_irq_disabled = true;
  89. }
  90. priv->wolopts = wol->wolopts;
  91. return 0;
  92. }
  93. static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
  94. {
  95. struct net_device *dev = priv->dev;
  96. int retries = 0;
  97. while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
  98. & RBUF_STATUS_WOL)) {
  99. retries++;
  100. if (retries > 5) {
  101. netdev_crit(dev, "polling wol mode timeout\n");
  102. return -ETIMEDOUT;
  103. }
  104. mdelay(1);
  105. }
  106. return retries;
  107. }
  108. int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
  109. enum bcmgenet_power_mode mode)
  110. {
  111. struct net_device *dev = priv->dev;
  112. int retries = 0;
  113. u32 reg;
  114. if (mode != GENET_POWER_WOL_MAGIC) {
  115. netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
  116. return -EINVAL;
  117. }
  118. /* disable RX */
  119. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  120. reg &= ~CMD_RX_EN;
  121. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  122. mdelay(10);
  123. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  124. reg |= MPD_EN;
  125. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  126. /* Do not leave UniMAC in MPD mode only */
  127. retries = bcmgenet_poll_wol_status(priv);
  128. if (retries < 0) {
  129. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  130. reg &= ~MPD_EN;
  131. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  132. return retries;
  133. }
  134. netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
  135. retries);
  136. /* Enable CRC forward */
  137. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  138. priv->crc_fwd_en = 1;
  139. reg |= CMD_CRC_FWD;
  140. /* Receiver must be enabled for WOL MP detection */
  141. reg |= CMD_RX_EN;
  142. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  143. if (priv->hw_params->flags & GENET_HAS_EXT) {
  144. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  145. reg &= ~EXT_ENERGY_DET_MASK;
  146. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  147. }
  148. return 0;
  149. }
  150. void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
  151. enum bcmgenet_power_mode mode)
  152. {
  153. u32 reg;
  154. if (mode != GENET_POWER_WOL_MAGIC) {
  155. netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
  156. return;
  157. }
  158. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  159. reg &= ~MPD_EN;
  160. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  161. /* Disable CRC Forward */
  162. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  163. reg &= ~CMD_CRC_FWD;
  164. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  165. priv->crc_fwd_en = 0;
  166. }