1 --- a/drivers/power/reset/Kconfig
2 +++ b/drivers/power/reset/Kconfig
3 @@ -99,6 +99,17 @@ config POWER_RESET_HISI
5 Reboot support for Hisilicon boards.
7 +config POWER_RESET_LINKSTATION
8 + tristate "Buffalo LinkStation power-off driver"
9 + depends on ARCH_MVEBU || COMPILE_TEST
10 + depends on OF_MDIO && PHYLIB
12 + This driver supports turning off some Buffalo LinkStations by
13 + setting an output pin at the ethernet PHY to the correct state.
14 + It also makes the device compatible with the WoL function.
16 + Say Y here if you have a Buffalo LinkStation LS421D/E.
18 config POWER_RESET_MSM
19 bool "Qualcomm MSM power-off driver"
21 --- a/drivers/power/reset/Makefile
22 +++ b/drivers/power/reset/Makefile
23 @@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF
24 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
25 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
26 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
27 +obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
28 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
29 obj-$(CONFIG_POWER_RESET_QCOM_PON) += qcom-pon.o
30 obj-$(CONFIG_POWER_RESET_OCELOT_RESET) += ocelot-reset.o
32 +++ b/drivers/power/reset/linkstation-poweroff.c
34 +// SPDX-License-Identifier: GPL-2.0
36 + * LinkStation power off restart driver
37 + * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
40 +#include <linux/module.h>
41 +#include <linux/notifier.h>
42 +#include <linux/of.h>
43 +#include <linux/of_mdio.h>
44 +#include <linux/of_platform.h>
45 +#include <linux/reboot.h>
46 +#include <linux/phy.h>
48 +/* Defines from the eth phy Marvell driver */
49 +#define MII_MARVELL_COPPER_PAGE 0
50 +#define MII_MARVELL_LED_PAGE 3
51 +#define MII_MARVELL_WOL_PAGE 17
52 +#define MII_MARVELL_PHY_PAGE 22
54 +#define MII_PHY_LED_CTRL 16
55 +#define MII_88E1318S_PHY_LED_TCR 18
56 +#define MII_88E1318S_PHY_WOL_CTRL 16
57 +#define MII_M1011_IEVENT 19
59 +#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
60 +#define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
61 +#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
62 +#define LED2_FORCE_ON (0x8 << 8)
63 +#define LEDMASK GENMASK(11,8)
65 +static struct phy_device *phydev;
67 +static void mvphy_reg_intn(u16 data)
69 + int rc = 0, saved_page;
71 + saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
75 + /* Force manual LED2 control to let INTn work */
76 + __phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
78 + /* Set the LED[2]/INTn pin to the required state */
79 + __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
80 + MII_88E1318S_PHY_LED_TCR_FORCE_INT,
81 + MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
84 + /* Clear interrupts to ensure INTn won't be holded in high state */
85 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
86 + __phy_read(phydev, MII_M1011_IEVENT);
88 + /* If WOL was enabled and a magic packet was received before powering
89 + * off, we won't be able to wake up by sending another magic packet.
92 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
93 + __phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
94 + MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
97 + rc = phy_restore_page(phydev, saved_page, rc);
99 + dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
102 + /* Slow down the PHY to save energy */
103 + rc= phy_speed_down(phydev, false);
105 + dev_err(&phydev->mdio.dev, "PHY speed down failed, %d\n", rc);
109 +static int linkstation_reboot_notifier(struct notifier_block *nb,
110 + unsigned long action, void *unused)
112 + if (action == SYS_RESTART)
113 + mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
115 + return NOTIFY_DONE;
118 +static struct notifier_block linkstation_reboot_nb = {
119 + .notifier_call = linkstation_reboot_notifier,
122 +static void linkstation_poweroff(void)
124 + unregister_reboot_notifier(&linkstation_reboot_nb);
127 + kernel_restart("Power off");
130 +static const struct of_device_id ls_poweroff_of_match[] = {
131 + { .compatible = "buffalo,ls421d" },
132 + { .compatible = "buffalo,ls421de" },
136 +static int __init linkstation_poweroff_init(void)
138 + struct mii_bus *bus;
139 + struct device_node *dn;
141 + dn = of_find_matching_node(NULL, ls_poweroff_of_match);
146 + dn = of_find_node_by_name(NULL, "mdio");
150 + bus = of_mdio_find_bus(dn);
153 + return -EPROBE_DEFER;
155 + phydev = phy_find_first(bus);
157 + return -EPROBE_DEFER;
159 + register_reboot_notifier(&linkstation_reboot_nb);
160 + pm_power_off = linkstation_poweroff;
162 + pr_info("LinkStation power off driver registered\n");
166 +static void __exit linkstation_poweroff_exit(void)
168 + pm_power_off = NULL;
169 + unregister_reboot_notifier(&linkstation_reboot_nb);
172 +module_init(linkstation_poweroff_init);
173 +module_exit(linkstation_poweroff_exit);
175 +MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
176 +MODULE_DESCRIPTION("LinkStation power off driver");
177 +MODULE_LICENSE("GPL v2");