bcmgenet_wol.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (wol->wolopts & WAKE_MAGICSECURE) {
  66. bcmgenet_umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
  67. UMAC_MPD_PW_MS);
  68. bcmgenet_umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
  69. UMAC_MPD_PW_LS);
  70. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  71. reg |= MPD_PW_EN;
  72. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  73. }
  74. /* Flag the device and relevant IRQ as wakeup capable */
  75. if (wol->wolopts) {
  76. device_set_wakeup_enable(kdev, 1);
  77. /* Avoid unbalanced enable_irq_wake calls */
  78. if (priv->wol_irq_disabled)
  79. enable_irq_wake(priv->wol_irq);
  80. priv->wol_irq_disabled = false;
  81. } else {
  82. device_set_wakeup_enable(kdev, 0);
  83. /* Avoid unbalanced disable_irq_wake calls */
  84. if (!priv->wol_irq_disabled)
  85. disable_irq_wake(priv->wol_irq);
  86. priv->wol_irq_disabled = true;
  87. }
  88. priv->wolopts = wol->wolopts;
  89. return 0;
  90. }
  91. static int bcmgenet_poll_wol_status(struct bcmgenet_priv *priv)
  92. {
  93. struct net_device *dev = priv->dev;
  94. int retries = 0;
  95. while (!(bcmgenet_rbuf_readl(priv, RBUF_STATUS)
  96. & RBUF_STATUS_WOL)) {
  97. retries++;
  98. if (retries > 5) {
  99. netdev_crit(dev, "polling wol mode timeout\n");
  100. return -ETIMEDOUT;
  101. }
  102. mdelay(1);
  103. }
  104. return retries;
  105. }
  106. int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
  107. enum bcmgenet_power_mode mode)
  108. {
  109. struct net_device *dev = priv->dev;
  110. u32 cpu_mask_clear;
  111. int retries = 0;
  112. u32 reg;
  113. if (mode != GENET_POWER_WOL_MAGIC) {
  114. netif_err(priv, wol, dev, "unsupported mode: %d\n", mode);
  115. return -EINVAL;
  116. }
  117. /* disable RX */
  118. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  119. reg &= ~CMD_RX_EN;
  120. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  121. mdelay(10);
  122. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  123. reg |= MPD_EN;
  124. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  125. /* Do not leave UniMAC in MPD mode only */
  126. retries = bcmgenet_poll_wol_status(priv);
  127. if (retries < 0) {
  128. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  129. reg &= ~MPD_EN;
  130. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  131. return retries;
  132. }
  133. netif_dbg(priv, wol, dev, "MPD WOL-ready status set after %d msec\n",
  134. retries);
  135. /* Enable CRC forward */
  136. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  137. priv->crc_fwd_en = 1;
  138. reg |= CMD_CRC_FWD;
  139. /* Receiver must be enabled for WOL MP detection */
  140. reg |= CMD_RX_EN;
  141. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  142. if (priv->hw_params->flags & GENET_HAS_EXT) {
  143. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  144. reg &= ~EXT_ENERGY_DET_MASK;
  145. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  146. }
  147. /* Enable the MPD interrupt */
  148. cpu_mask_clear = UMAC_IRQ_MPD_R;
  149. bcmgenet_intrl2_0_writel(priv, cpu_mask_clear, INTRL2_CPU_MASK_CLEAR);
  150. return 0;
  151. }
  152. void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
  153. enum bcmgenet_power_mode mode)
  154. {
  155. u32 cpu_mask_set;
  156. u32 reg;
  157. if (mode != GENET_POWER_WOL_MAGIC) {
  158. netif_err(priv, wol, priv->dev, "invalid mode: %d\n", mode);
  159. return;
  160. }
  161. reg = bcmgenet_umac_readl(priv, UMAC_MPD_CTRL);
  162. reg &= ~MPD_EN;
  163. bcmgenet_umac_writel(priv, reg, UMAC_MPD_CTRL);
  164. /* Disable CRC Forward */
  165. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  166. reg &= ~CMD_CRC_FWD;
  167. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  168. priv->crc_fwd_en = 0;
  169. /* Stop monitoring magic packet IRQ */
  170. cpu_mask_set = UMAC_IRQ_MPD_R;
  171. /* Stop monitoring magic packet IRQ */
  172. bcmgenet_intrl2_0_writel(priv, cpu_mask_set, INTRL2_CPU_MASK_SET);
  173. }