bcmgenet_wol.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #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. u32 cpu_mask_clear;
  113. int retries = 0;
  114. u32 reg;
  115. if (mode != GENET_POWER_WOL_MAGIC) {
  116. netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
  117. return -EINVAL;
  118. }
  119. /* disable RX */
  120. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  121. reg &= ~CMD_RX_EN;
  122. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  123. mdelay(10);
  124. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  125. reg |= MPD_EN;
  126. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  127. /* Do not leave UniMAC in MPD mode only */
  128. retries = bcmgenet_poll_wol_status(priv);
  129. if (retries < 0) {
  130. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  131. reg &= ~MPD_EN;
  132. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  133. return retries;
  134. }
  135. netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
  136. retries);
  137. /* Enable CRC forward */
  138. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  139. priv->crc_fwd_en = 1;
  140. reg |= CMD_CRC_FWD;
  141. /* Receiver must be enabled for WOL MP detection */
  142. reg |= CMD_RX_EN;
  143. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  144. if (priv->hw_params->flags & GENET_HAS_EXT) {
  145. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  146. reg &= ~EXT_ENERGY_DET_MASK;
  147. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  148. }
  149. /* Enable the MPD interrupt */
  150. cpu_mask_clear = UMAC_IRQ_MPD_R;
  151. bcmgenet_intrl2_0_writel(priv, cpu_mask_clear, INTRL2_CPU_MASK_CLEAR);
  152. return 0;
  153. }
  154. void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
  155. enum bcmgenet_power_mode mode)
  156. {
  157. u32 cpu_mask_set;
  158. u32 reg;
  159. if (mode != GENET_POWER_WOL_MAGIC) {
  160. netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
  161. return;
  162. }
  163. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  164. reg &= ~MPD_EN;
  165. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  166. /* Disable CRC Forward */
  167. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  168. reg &= ~CMD_CRC_FWD;
  169. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  170. priv->crc_fwd_en = 0;
  171. /* Stop monitoring magic packet IRQ */
  172. cpu_mask_set = UMAC_IRQ_MPD_R;
  173. /* Stop monitoring magic packet IRQ */
  174. bcmgenet_intrl2_0_writel(priv, cpu_mask_set, INTRL2_CPU_MASK_SET);
  175. }