4ba06cc3dd2a0ff69115d230a98b0a122258e223
[openwrt/staging/xback.git] /
1 From 7840e4d6f48a75413470935ebdc4bab4fc0c035e Mon Sep 17 00:00:00 2001
2 From: Daniel Braunwarth <daniel.braunwarth@kuka.com>
3 Date: Tue, 29 Apr 2025 13:33:37 +0200
4 Subject: [PATCH] net: phy: realtek: Add support for WOL magic packet on
5 RTL8211F
6
7 The RTL8211F supports multiple WOL modes. This patch adds support for
8 magic packets.
9
10 The PHY notifies the system via the INTB/PMEB pin when a WOL event
11 occurs.
12
13 Signed-off-by: Daniel Braunwarth <daniel.braunwarth@kuka.com>
14 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
15 Link: https://patch.msgid.link/20250429-realtek_wol-v2-1-8f84def1ef2c@kuka.com
16 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
17 ---
18 drivers/net/phy/realtek/realtek_main.c | 69 ++++++++++++++++++++++++++
19 1 file changed, 69 insertions(+)
20
21 --- a/drivers/net/phy/realtek/realtek_main.c
22 +++ b/drivers/net/phy/realtek/realtek_main.c
23 @@ -10,6 +10,7 @@
24 #include <linux/bitops.h>
25 #include <linux/of.h>
26 #include <linux/phy.h>
27 +#include <linux/netdevice.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/clk.h>
31 @@ -38,6 +39,24 @@
32
33 #define RTL8211F_INSR 0x1d
34
35 +/* RTL8211F WOL interrupt configuration */
36 +#define RTL8211F_INTBCR_PAGE 0xd40
37 +#define RTL8211F_INTBCR 0x16
38 +#define RTL8211F_INTBCR_INTB_PMEB BIT(5)
39 +
40 +/* RTL8211F WOL settings */
41 +#define RTL8211F_WOL_SETTINGS_PAGE 0xd8a
42 +#define RTL8211F_WOL_SETTINGS_EVENTS 16
43 +#define RTL8211F_WOL_EVENT_MAGIC BIT(12)
44 +#define RTL8211F_WOL_SETTINGS_STATUS 17
45 +#define RTL8211F_WOL_STATUS_RESET (BIT(15) | 0x1fff)
46 +
47 +/* RTL8211F Unique phyiscal and multicast address (WOL) */
48 +#define RTL8211F_PHYSICAL_ADDR_PAGE 0xd8c
49 +#define RTL8211F_PHYSICAL_ADDR_WORD0 16
50 +#define RTL8211F_PHYSICAL_ADDR_WORD1 17
51 +#define RTL8211F_PHYSICAL_ADDR_WORD2 18
52 +
53 #define RTL8211F_LEDCR 0x10
54 #define RTL8211F_LEDCR_MODE BIT(15)
55 #define RTL8211F_LEDCR_ACT_TXRX BIT(4)
56 @@ -123,6 +142,7 @@ struct rtl821x_priv {
57 u16 phycr2;
58 bool has_phycr2;
59 struct clk *clk;
60 + u32 saved_wolopts;
61 };
62
63 static int rtl821x_read_page(struct phy_device *phydev)
64 @@ -354,6 +374,53 @@ static irqreturn_t rtl8211f_handle_inter
65 return IRQ_HANDLED;
66 }
67
68 +static void rtl8211f_get_wol(struct phy_device *dev, struct ethtool_wolinfo *wol)
69 +{
70 + wol->supported = WAKE_MAGIC;
71 + if (phy_read_paged(dev, RTL8211F_WOL_SETTINGS_PAGE, RTL8211F_WOL_SETTINGS_EVENTS)
72 + & RTL8211F_WOL_EVENT_MAGIC)
73 + wol->wolopts = WAKE_MAGIC;
74 +}
75 +
76 +static int rtl8211f_set_wol(struct phy_device *dev, struct ethtool_wolinfo *wol)
77 +{
78 + const u8 *mac_addr = dev->attached_dev->dev_addr;
79 + int oldpage;
80 +
81 + oldpage = phy_save_page(dev);
82 + if (oldpage < 0)
83 + goto err;
84 +
85 + if (wol->wolopts & WAKE_MAGIC) {
86 + /* Store the device address for the magic packet */
87 + rtl821x_write_page(dev, RTL8211F_PHYSICAL_ADDR_PAGE);
88 + __phy_write(dev, RTL8211F_PHYSICAL_ADDR_WORD0, mac_addr[1] << 8 | (mac_addr[0]));
89 + __phy_write(dev, RTL8211F_PHYSICAL_ADDR_WORD1, mac_addr[3] << 8 | (mac_addr[2]));
90 + __phy_write(dev, RTL8211F_PHYSICAL_ADDR_WORD2, mac_addr[5] << 8 | (mac_addr[4]));
91 +
92 + /* Enable magic packet matching and reset WOL status */
93 + rtl821x_write_page(dev, RTL8211F_WOL_SETTINGS_PAGE);
94 + __phy_write(dev, RTL8211F_WOL_SETTINGS_EVENTS, RTL8211F_WOL_EVENT_MAGIC);
95 + __phy_write(dev, RTL8211F_WOL_SETTINGS_STATUS, RTL8211F_WOL_STATUS_RESET);
96 +
97 + /* Enable the WOL interrupt */
98 + rtl821x_write_page(dev, RTL8211F_INTBCR_PAGE);
99 + __phy_set_bits(dev, RTL8211F_INTBCR, RTL8211F_INTBCR_INTB_PMEB);
100 + } else {
101 + /* Disable the WOL interrupt */
102 + rtl821x_write_page(dev, RTL8211F_INTBCR_PAGE);
103 + __phy_clear_bits(dev, RTL8211F_INTBCR, RTL8211F_INTBCR_INTB_PMEB);
104 +
105 + /* Disable magic packet matching and reset WOL status */
106 + rtl821x_write_page(dev, RTL8211F_WOL_SETTINGS_PAGE);
107 + __phy_write(dev, RTL8211F_WOL_SETTINGS_EVENTS, 0);
108 + __phy_write(dev, RTL8211F_WOL_SETTINGS_STATUS, RTL8211F_WOL_STATUS_RESET);
109 + }
110 +
111 +err:
112 + return phy_restore_page(dev, oldpage, 0);
113 +}
114 +
115 static int rtl8211_config_aneg(struct phy_device *phydev)
116 {
117 int ret;
118 @@ -1400,6 +1467,8 @@ static struct phy_driver realtek_drvs[]
119 .read_status = rtlgen_read_status,
120 .config_intr = &rtl8211f_config_intr,
121 .handle_interrupt = rtl8211f_handle_interrupt,
122 + .set_wol = rtl8211f_set_wol,
123 + .get_wol = rtl8211f_get_wol,
124 .suspend = rtl821x_suspend,
125 .resume = rtl821x_resume,
126 .read_page = rtl821x_read_page,