Skip to content

Commit 4d94282

Browse files
BryanW11731-MCHPdavem330
authored andcommitted
lan743x: Add power management support
Implement power management Supports suspend, resume, and Wake on LAN Signed-off-by: Bryan Whitehead <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6958460 commit 4d94282

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

drivers/net/ethernet/microchip/lan743x_ethtool.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,49 @@ static int lan743x_ethtool_get_sset_count(struct net_device *netdev, int sset)
415415
}
416416
}
417417

418+
#ifdef CONFIG_PM
419+
static void lan743x_ethtool_get_wol(struct net_device *netdev,
420+
struct ethtool_wolinfo *wol)
421+
{
422+
struct lan743x_adapter *adapter = netdev_priv(netdev);
423+
424+
wol->supported = 0;
425+
wol->wolopts = 0;
426+
phy_ethtool_get_wol(netdev->phydev, wol);
427+
428+
wol->supported |= WAKE_BCAST | WAKE_UCAST | WAKE_MCAST |
429+
WAKE_MAGIC | WAKE_PHY | WAKE_ARP;
430+
431+
wol->wolopts |= adapter->wolopts;
432+
}
433+
434+
static int lan743x_ethtool_set_wol(struct net_device *netdev,
435+
struct ethtool_wolinfo *wol)
436+
{
437+
struct lan743x_adapter *adapter = netdev_priv(netdev);
438+
439+
adapter->wolopts = 0;
440+
if (wol->wolopts & WAKE_UCAST)
441+
adapter->wolopts |= WAKE_UCAST;
442+
if (wol->wolopts & WAKE_MCAST)
443+
adapter->wolopts |= WAKE_MCAST;
444+
if (wol->wolopts & WAKE_BCAST)
445+
adapter->wolopts |= WAKE_BCAST;
446+
if (wol->wolopts & WAKE_MAGIC)
447+
adapter->wolopts |= WAKE_MAGIC;
448+
if (wol->wolopts & WAKE_PHY)
449+
adapter->wolopts |= WAKE_PHY;
450+
if (wol->wolopts & WAKE_ARP)
451+
adapter->wolopts |= WAKE_ARP;
452+
453+
device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
454+
455+
phy_ethtool_set_wol(netdev->phydev, wol);
456+
457+
return 0;
458+
}
459+
#endif /* CONFIG_PM */
460+
418461
const struct ethtool_ops lan743x_ethtool_ops = {
419462
.get_drvinfo = lan743x_ethtool_get_drvinfo,
420463
.get_msglevel = lan743x_ethtool_get_msglevel,
@@ -429,4 +472,8 @@ const struct ethtool_ops lan743x_ethtool_ops = {
429472
.get_sset_count = lan743x_ethtool_get_sset_count,
430473
.get_link_ksettings = phy_ethtool_get_link_ksettings,
431474
.set_link_ksettings = phy_ethtool_set_link_ksettings,
475+
#ifdef CONFIG_PM
476+
.get_wol = lan743x_ethtool_get_wol,
477+
.set_wol = lan743x_ethtool_set_wol,
478+
#endif
432479
};

drivers/net/ethernet/microchip/lan743x_main.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/phy.h>
1212
#include <linux/rtnetlink.h>
1313
#include <linux/iopoll.h>
14+
#include <linux/crc16.h>
1415
#include "lan743x_main.h"
1516
#include "lan743x_ethtool.h"
1617

@@ -2749,10 +2750,182 @@ static void lan743x_pcidev_shutdown(struct pci_dev *pdev)
27492750
lan743x_netdev_close(netdev);
27502751
rtnl_unlock();
27512752

2753+
#ifdef CONFIG_PM
2754+
pci_save_state(pdev);
2755+
#endif
2756+
27522757
/* clean up lan743x portion */
27532758
lan743x_hardware_cleanup(adapter);
27542759
}
27552760

2761+
#ifdef CONFIG_PM
2762+
static u16 lan743x_pm_wakeframe_crc16(const u8 *buf, int len)
2763+
{
2764+
return bitrev16(crc16(0xFFFF, buf, len));
2765+
}
2766+
2767+
static void lan743x_pm_set_wol(struct lan743x_adapter *adapter)
2768+
{
2769+
const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
2770+
const u8 ipv6_multicast[3] = { 0x33, 0x33 };
2771+
const u8 arp_type[2] = { 0x08, 0x06 };
2772+
int mask_index;
2773+
u32 pmtctl;
2774+
u32 wucsr;
2775+
u32 macrx;
2776+
u16 crc;
2777+
2778+
for (mask_index = 0; mask_index < MAC_NUM_OF_WUF_CFG; mask_index++)
2779+
lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 0);
2780+
2781+
/* clear wake settings */
2782+
pmtctl = lan743x_csr_read(adapter, PMT_CTL);
2783+
pmtctl |= PMT_CTL_WUPS_MASK_;
2784+
pmtctl &= ~(PMT_CTL_GPIO_WAKEUP_EN_ | PMT_CTL_EEE_WAKEUP_EN_ |
2785+
PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_ |
2786+
PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ | PMT_CTL_ETH_PHY_WAKE_EN_);
2787+
2788+
macrx = lan743x_csr_read(adapter, MAC_RX);
2789+
2790+
wucsr = 0;
2791+
mask_index = 0;
2792+
2793+
pmtctl |= PMT_CTL_ETH_PHY_D3_COLD_OVR_ | PMT_CTL_ETH_PHY_D3_OVR_;
2794+
2795+
if (adapter->wolopts & WAKE_PHY) {
2796+
pmtctl |= PMT_CTL_ETH_PHY_EDPD_PLL_CTL_;
2797+
pmtctl |= PMT_CTL_ETH_PHY_WAKE_EN_;
2798+
}
2799+
if (adapter->wolopts & WAKE_MAGIC) {
2800+
wucsr |= MAC_WUCSR_MPEN_;
2801+
macrx |= MAC_RX_RXEN_;
2802+
pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2803+
}
2804+
if (adapter->wolopts & WAKE_UCAST) {
2805+
wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_PFDA_EN_;
2806+
macrx |= MAC_RX_RXEN_;
2807+
pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2808+
pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2809+
}
2810+
if (adapter->wolopts & WAKE_BCAST) {
2811+
wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_BCST_EN_;
2812+
macrx |= MAC_RX_RXEN_;
2813+
pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2814+
pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2815+
}
2816+
if (adapter->wolopts & WAKE_MCAST) {
2817+
/* IPv4 multicast */
2818+
crc = lan743x_pm_wakeframe_crc16(ipv4_multicast, 3);
2819+
lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2820+
MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2821+
(0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2822+
(crc & MAC_WUF_CFG_CRC16_MASK_));
2823+
lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 7);
2824+
lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2825+
lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2826+
lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2827+
mask_index++;
2828+
2829+
/* IPv6 multicast */
2830+
crc = lan743x_pm_wakeframe_crc16(ipv6_multicast, 2);
2831+
lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2832+
MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2833+
(0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2834+
(crc & MAC_WUF_CFG_CRC16_MASK_));
2835+
lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 3);
2836+
lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2837+
lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2838+
lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2839+
mask_index++;
2840+
2841+
wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2842+
macrx |= MAC_RX_RXEN_;
2843+
pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2844+
pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2845+
}
2846+
if (adapter->wolopts & WAKE_ARP) {
2847+
/* set MAC_WUF_CFG & WUF_MASK
2848+
* for packettype (offset 12,13) = ARP (0x0806)
2849+
*/
2850+
crc = lan743x_pm_wakeframe_crc16(arp_type, 2);
2851+
lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2852+
MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_ALL_ |
2853+
(0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2854+
(crc & MAC_WUF_CFG_CRC16_MASK_));
2855+
lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 0x3000);
2856+
lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2857+
lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2858+
lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2859+
mask_index++;
2860+
2861+
wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2862+
macrx |= MAC_RX_RXEN_;
2863+
pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2864+
pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2865+
}
2866+
2867+
lan743x_csr_write(adapter, MAC_WUCSR, wucsr);
2868+
lan743x_csr_write(adapter, PMT_CTL, pmtctl);
2869+
lan743x_csr_write(adapter, MAC_RX, macrx);
2870+
}
2871+
2872+
static int lan743x_pm_suspend(struct device *dev)
2873+
{
2874+
struct pci_dev *pdev = to_pci_dev(dev);
2875+
struct net_device *netdev = pci_get_drvdata(pdev);
2876+
struct lan743x_adapter *adapter = netdev_priv(netdev);
2877+
int ret;
2878+
2879+
lan743x_pcidev_shutdown(pdev);
2880+
2881+
/* clear all wakes */
2882+
lan743x_csr_write(adapter, MAC_WUCSR, 0);
2883+
lan743x_csr_write(adapter, MAC_WUCSR2, 0);
2884+
lan743x_csr_write(adapter, MAC_WK_SRC, 0xFFFFFFFF);
2885+
2886+
if (adapter->wolopts)
2887+
lan743x_pm_set_wol(adapter);
2888+
2889+
/* Host sets PME_En, put D3hot */
2890+
ret = pci_prepare_to_sleep(pdev);
2891+
2892+
return 0;
2893+
}
2894+
2895+
static int lan743x_pm_resume(struct device *dev)
2896+
{
2897+
struct pci_dev *pdev = to_pci_dev(dev);
2898+
struct net_device *netdev = pci_get_drvdata(pdev);
2899+
struct lan743x_adapter *adapter = netdev_priv(netdev);
2900+
int ret;
2901+
2902+
pci_set_power_state(pdev, PCI_D0);
2903+
pci_restore_state(pdev);
2904+
pci_save_state(pdev);
2905+
2906+
ret = lan743x_hardware_init(adapter, pdev);
2907+
if (ret) {
2908+
netif_err(adapter, probe, adapter->netdev,
2909+
"lan743x_hardware_init returned %d\n", ret);
2910+
}
2911+
2912+
/* open netdev when netdev is at running state while resume.
2913+
* For instance, it is true when system wakesup after pm-suspend
2914+
* However, it is false when system wakes up after suspend GUI menu
2915+
*/
2916+
if (netif_running(netdev))
2917+
lan743x_netdev_open(netdev);
2918+
2919+
netif_device_attach(netdev);
2920+
2921+
return 0;
2922+
}
2923+
2924+
const struct dev_pm_ops lan743x_pm_ops = {
2925+
SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend, lan743x_pm_resume)
2926+
};
2927+
#endif /*CONFIG_PM */
2928+
27562929
static const struct pci_device_id lan743x_pcidev_tbl[] = {
27572930
{ PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7430) },
27582931
{ 0, }
@@ -2763,6 +2936,9 @@ static struct pci_driver lan743x_pcidev_driver = {
27632936
.id_table = lan743x_pcidev_tbl,
27642937
.probe = lan743x_pcidev_probe,
27652938
.remove = lan743x_pcidev_remove,
2939+
#ifdef CONFIG_PM
2940+
.driver.pm = &lan743x_pm_ops,
2941+
#endif
27662942
.shutdown = lan743x_pcidev_shutdown,
27672943
};
27682944

drivers/net/ethernet/microchip/lan743x_main.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@
2424
#define HW_CFG_LRST_ BIT(1)
2525

2626
#define PMT_CTL (0x014)
27+
#define PMT_CTL_ETH_PHY_D3_COLD_OVR_ BIT(27)
28+
#define PMT_CTL_MAC_D3_RX_CLK_OVR_ BIT(25)
29+
#define PMT_CTL_ETH_PHY_EDPD_PLL_CTL_ BIT(24)
30+
#define PMT_CTL_ETH_PHY_D3_OVR_ BIT(23)
31+
#define PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ BIT(18)
32+
#define PMT_CTL_GPIO_WAKEUP_EN_ BIT(15)
33+
#define PMT_CTL_EEE_WAKEUP_EN_ BIT(13)
2734
#define PMT_CTL_READY_ BIT(7)
2835
#define PMT_CTL_ETH_PHY_RST_ BIT(4)
36+
#define PMT_CTL_WOL_EN_ BIT(3)
37+
#define PMT_CTL_ETH_PHY_WAKE_EN_ BIT(2)
38+
#define PMT_CTL_WUPS_MASK_ (0x00000003)
2939

3040
#define DP_SEL (0x024)
3141
#define DP_SEL_DPRDY_ BIT(31)
@@ -107,6 +117,38 @@
107117

108118
#define MAC_MII_DATA (0x124)
109119

120+
#define MAC_WUCSR (0x140)
121+
#define MAC_WUCSR_RFE_WAKE_EN_ BIT(14)
122+
#define MAC_WUCSR_PFDA_EN_ BIT(3)
123+
#define MAC_WUCSR_WAKE_EN_ BIT(2)
124+
#define MAC_WUCSR_MPEN_ BIT(1)
125+
#define MAC_WUCSR_BCST_EN_ BIT(0)
126+
127+
#define MAC_WK_SRC (0x144)
128+
129+
#define MAC_WUF_CFG0 (0x150)
130+
#define MAC_NUM_OF_WUF_CFG (32)
131+
#define MAC_WUF_CFG_BEGIN (MAC_WUF_CFG0)
132+
#define MAC_WUF_CFG(index) (MAC_WUF_CFG_BEGIN + (4 * (index)))
133+
#define MAC_WUF_CFG_EN_ BIT(31)
134+
#define MAC_WUF_CFG_TYPE_MCAST_ (0x02000000)
135+
#define MAC_WUF_CFG_TYPE_ALL_ (0x01000000)
136+
#define MAC_WUF_CFG_OFFSET_SHIFT_ (16)
137+
#define MAC_WUF_CFG_CRC16_MASK_ (0x0000FFFF)
138+
139+
#define MAC_WUF_MASK0_0 (0x200)
140+
#define MAC_WUF_MASK0_1 (0x204)
141+
#define MAC_WUF_MASK0_2 (0x208)
142+
#define MAC_WUF_MASK0_3 (0x20C)
143+
#define MAC_WUF_MASK0_BEGIN (MAC_WUF_MASK0_0)
144+
#define MAC_WUF_MASK1_BEGIN (MAC_WUF_MASK0_1)
145+
#define MAC_WUF_MASK2_BEGIN (MAC_WUF_MASK0_2)
146+
#define MAC_WUF_MASK3_BEGIN (MAC_WUF_MASK0_3)
147+
#define MAC_WUF_MASK0(index) (MAC_WUF_MASK0_BEGIN + (0x10 * (index)))
148+
#define MAC_WUF_MASK1(index) (MAC_WUF_MASK1_BEGIN + (0x10 * (index)))
149+
#define MAC_WUF_MASK2(index) (MAC_WUF_MASK2_BEGIN + (0x10 * (index)))
150+
#define MAC_WUF_MASK3(index) (MAC_WUF_MASK3_BEGIN + (0x10 * (index)))
151+
110152
/* offset 0x400 - 0x500, x may range from 0 to 32, for a total of 33 entries */
111153
#define RFE_ADDR_FILT_HI(x) (0x400 + (8 * (x)))
112154
#define RFE_ADDR_FILT_HI_VALID_ BIT(31)
@@ -121,6 +163,8 @@
121163
#define RFE_CTL_MCAST_HASH_ BIT(3)
122164
#define RFE_CTL_DA_PERFECT_ BIT(1)
123165

166+
#define MAC_WUCSR2 (0x600)
167+
124168
#define INT_STS (0x780)
125169
#define INT_BIT_DMA_RX_(channel) BIT(24 + (channel))
126170
#define INT_BIT_ALL_RX_ (0x0F000000)
@@ -534,6 +578,9 @@ struct lan743x_adapter {
534578
struct net_device *netdev;
535579
struct mii_bus *mdiobus;
536580
int msg_enable;
581+
#ifdef CONFIG_PM
582+
u32 wolopts;
583+
#endif
537584
struct pci_dev *pdev;
538585
struct lan743x_csr csr;
539586
struct lan743x_intr intr;

0 commit comments

Comments
 (0)