1 // SPDX-License-Identifier: GPL-2.0
3 * LinkStation power off restart driver
4 * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
7 #include <linux/module.h>
8 #include <linux/notifier.h>
10 #include <linux/of_mdio.h>
11 #include <linux/of_platform.h>
12 #include <linux/reboot.h>
13 #include <linux/phy.h>
15 /* Defines from the eth phy Marvell driver */
16 #define MII_MARVELL_COPPER_PAGE 0
17 #define MII_MARVELL_LED_PAGE 3
18 #define MII_MARVELL_WOL_PAGE 17
19 #define MII_MARVELL_PHY_PAGE 22
21 #define MII_PHY_LED_CTRL 16
22 #define MII_PHY_LED_POL_CTRL 17
23 #define MII_88E1318S_PHY_LED_TCR 18
24 #define MII_88E1318S_PHY_WOL_CTRL 16
25 #define MII_M1011_IEVENT 19
27 #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
28 #define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
29 #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
30 #define LED2_FORCE_ON (0x8 << 8)
31 #define LEDMASK GENMASK(11,8)
33 #define MII_88E1318S_PHY_LED_POL_LED2 BIT(4)
35 struct power_off_cfg
{
37 void (*phy_set_reg
)(bool restart
);
40 static struct phy_device
*phydev
;
41 static const struct power_off_cfg
*cfg
;
43 static void linkstation_mvphy_reg_intn(bool restart
)
45 int rc
= 0, saved_page
;
49 data
= MII_88E1318S_PHY_LED_TCR_FORCE_INT
;
51 saved_page
= phy_select_page(phydev
, MII_MARVELL_LED_PAGE
);
55 /* Force manual LED2 control to let INTn work */
56 __phy_modify(phydev
, MII_PHY_LED_CTRL
, LEDMASK
, LED2_FORCE_ON
);
58 /* Set the LED[2]/INTn pin to the required state */
59 __phy_modify(phydev
, MII_88E1318S_PHY_LED_TCR
,
60 MII_88E1318S_PHY_LED_TCR_FORCE_INT
,
61 MII_88E1318S_PHY_LED_TCR_INTn_ENABLE
| data
);
64 /* Clear interrupts to ensure INTn won't be holded in high state */
65 __phy_write(phydev
, MII_MARVELL_PHY_PAGE
, MII_MARVELL_COPPER_PAGE
);
66 __phy_read(phydev
, MII_M1011_IEVENT
);
68 /* If WOL was enabled and a magic packet was received before powering
69 * off, we won't be able to wake up by sending another magic packet.
72 __phy_write(phydev
, MII_MARVELL_PHY_PAGE
, MII_MARVELL_WOL_PAGE
);
73 __phy_set_bits(phydev
, MII_88E1318S_PHY_WOL_CTRL
,
74 MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS
);
77 rc
= phy_restore_page(phydev
, saved_page
, rc
);
79 dev_err(&phydev
->mdio
.dev
, "Write register failed, %d\n", rc
);
82 static void readynas_mvphy_set_reg(bool restart
)
84 int rc
= 0, saved_page
;
88 data
= MII_88E1318S_PHY_LED_POL_LED2
;
90 saved_page
= phy_select_page(phydev
, MII_MARVELL_LED_PAGE
);
94 /* Set the LED[2].0 Polarity bit to the required state */
95 __phy_modify(phydev
, MII_PHY_LED_POL_CTRL
,
96 MII_88E1318S_PHY_LED_POL_LED2
, data
);
99 /* If WOL was enabled and a magic packet was received before powering
100 * off, we won't be able to wake up by sending another magic packet.
103 __phy_write(phydev
, MII_MARVELL_PHY_PAGE
, MII_MARVELL_WOL_PAGE
);
104 __phy_set_bits(phydev
, MII_88E1318S_PHY_WOL_CTRL
,
105 MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS
);
108 rc
= phy_restore_page(phydev
, saved_page
, rc
);
110 dev_err(&phydev
->mdio
.dev
, "Write register failed, %d\n", rc
);
113 static const struct power_off_cfg linkstation_power_off_cfg
= {
114 .mdio_node_name
= "mdio",
115 .phy_set_reg
= linkstation_mvphy_reg_intn
,
118 static const struct power_off_cfg readynas_power_off_cfg
= {
119 .mdio_node_name
= "mdio-bus",
120 .phy_set_reg
= readynas_mvphy_set_reg
,
123 static int linkstation_reboot_notifier(struct notifier_block
*nb
,
124 unsigned long action
, void *unused
)
126 if (action
== SYS_RESTART
)
127 cfg
->phy_set_reg(true);
132 static struct notifier_block linkstation_reboot_nb
= {
133 .notifier_call
= linkstation_reboot_notifier
,
136 static void linkstation_poweroff(void)
138 unregister_reboot_notifier(&linkstation_reboot_nb
);
139 cfg
->phy_set_reg(false);
141 kernel_restart("Power off");
144 static const struct of_device_id ls_poweroff_of_match
[] = {
145 { .compatible
= "buffalo,ls421d",
146 .data
= &linkstation_power_off_cfg
,
148 { .compatible
= "buffalo,ls421de",
149 .data
= &linkstation_power_off_cfg
,
151 { .compatible
= "netgear,readynas-duo-v2",
152 .data
= &readynas_power_off_cfg
,
157 static int __init
linkstation_poweroff_init(void)
160 struct device_node
*dn
;
161 const struct of_device_id
*match
;
163 dn
= of_find_matching_node(NULL
, ls_poweroff_of_match
);
168 match
= of_match_node(ls_poweroff_of_match
, dn
);
171 dn
= of_find_node_by_name(NULL
, cfg
->mdio_node_name
);
175 bus
= of_mdio_find_bus(dn
);
178 return -EPROBE_DEFER
;
180 phydev
= phy_find_first(bus
);
181 put_device(&bus
->dev
);
183 return -EPROBE_DEFER
;
185 register_reboot_notifier(&linkstation_reboot_nb
);
186 pm_power_off
= linkstation_poweroff
;
191 static void __exit
linkstation_poweroff_exit(void)
194 unregister_reboot_notifier(&linkstation_reboot_nb
);
197 module_init(linkstation_poweroff_init
);
198 module_exit(linkstation_poweroff_exit
);
200 MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
201 MODULE_DESCRIPTION("LinkStation power off driver");
202 MODULE_LICENSE("GPL v2");