7bf48c34058115943aed47a7a099ba74f599fc26
[openwrt/staging/mkresin.git] / target / linux / generic / backport-5.4 / 025-power-reset-add-driver-for-LinkStation-power-off.patch
1 From a7f79f99541eff4e6bcae0014eb08d3019337565 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Daniel=20Gonz=C3=A1lez=20Cabanelas?= <dgcbueu@gmail.com>
3 Date: Wed, 15 Jul 2020 15:35:14 +0200
4 Subject: [PATCH] power: reset: add driver for LinkStation power off
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Some Buffalo LinkStations perform the power off operation, at restart
10 time, depending on the state of an output pin (LED2/INTn) at the ethernet
11 PHY. This pin is also used to wake the machine when a WoL packet is
12 received by the PHY.
13
14 The driver is required by the Buffalo LinkStation LS421DE (ARM MVEBU),
15 and other models. Without it, the board remains forever halted if a
16 power off command is executed, unless the PSU is disconnected and
17 connected again.
18
19 Add the driver to provide the power off function and also make the WoL
20 feature to be available.
21
22 Signed-off-by: Daniel González Cabanelas <dgcbueu@gmail.com>
23 Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
24 ---
25 drivers/power/reset/Kconfig | 11 ++
26 drivers/power/reset/Makefile | 1 +
27 drivers/power/reset/linkstation-poweroff.c | 136 +++++++++++++++++++++
28 3 files changed, 148 insertions(+)
29 create mode 100644 drivers/power/reset/linkstation-poweroff.c
30
31 --- a/drivers/power/reset/Kconfig
32 +++ b/drivers/power/reset/Kconfig
33 @@ -99,6 +99,17 @@ config POWER_RESET_HISI
34 help
35 Reboot support for Hisilicon boards.
36
37 +config POWER_RESET_LINKSTATION
38 + tristate "Buffalo LinkStation power-off driver"
39 + depends on ARCH_MVEBU || COMPILE_TEST
40 + depends on OF_MDIO && PHYLIB
41 + help
42 + This driver supports turning off some Buffalo LinkStations by
43 + setting an output pin at the ethernet PHY to the correct state.
44 + It also makes the device compatible with the WoL function.
45 +
46 + Say Y here if you have a Buffalo LinkStation LS421D/E.
47 +
48 config POWER_RESET_MSM
49 bool "Qualcomm MSM power-off driver"
50 depends on ARCH_QCOM
51 --- a/drivers/power/reset/Makefile
52 +++ b/drivers/power/reset/Makefile
53 @@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF
54 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
55 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
56 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
57 +obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
58 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
59 obj-$(CONFIG_POWER_RESET_QCOM_PON) += qcom-pon.o
60 obj-$(CONFIG_POWER_RESET_OCELOT_RESET) += ocelot-reset.o
61 --- /dev/null
62 +++ b/drivers/power/reset/linkstation-poweroff.c
63 @@ -0,0 +1,136 @@
64 +// SPDX-License-Identifier: GPL-2.0
65 +/*
66 + * LinkStation power off restart driver
67 + * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
68 + */
69 +
70 +#include <linux/module.h>
71 +#include <linux/notifier.h>
72 +#include <linux/of.h>
73 +#include <linux/of_mdio.h>
74 +#include <linux/of_platform.h>
75 +#include <linux/reboot.h>
76 +#include <linux/phy.h>
77 +
78 +/* Defines from the eth phy Marvell driver */
79 +#define MII_MARVELL_COPPER_PAGE 0
80 +#define MII_MARVELL_LED_PAGE 3
81 +#define MII_MARVELL_WOL_PAGE 17
82 +#define MII_MARVELL_PHY_PAGE 22
83 +
84 +#define MII_PHY_LED_CTRL 16
85 +#define MII_88E1318S_PHY_LED_TCR 18
86 +#define MII_88E1318S_PHY_WOL_CTRL 16
87 +#define MII_M1011_IEVENT 19
88 +
89 +#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
90 +#define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
91 +#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
92 +#define LED2_FORCE_ON (0x8 << 8)
93 +#define LEDMASK GENMASK(11,8)
94 +
95 +static struct phy_device *phydev;
96 +
97 +static void mvphy_reg_intn(u16 data)
98 +{
99 + int rc = 0, saved_page;
100 +
101 + saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
102 + if (saved_page < 0)
103 + goto err;
104 +
105 + /* Force manual LED2 control to let INTn work */
106 + __phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
107 +
108 + /* Set the LED[2]/INTn pin to the required state */
109 + __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
110 + MII_88E1318S_PHY_LED_TCR_FORCE_INT,
111 + MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
112 +
113 + if (!data) {
114 + /* Clear interrupts to ensure INTn won't be holded in high state */
115 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
116 + __phy_read(phydev, MII_M1011_IEVENT);
117 +
118 + /* If WOL was enabled and a magic packet was received before powering
119 + * off, we won't be able to wake up by sending another magic packet.
120 + * Clear WOL status.
121 + */
122 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
123 + __phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
124 + MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
125 + }
126 +err:
127 + rc = phy_restore_page(phydev, saved_page, rc);
128 + if (rc < 0)
129 + dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
130 +}
131 +
132 +static int linkstation_reboot_notifier(struct notifier_block *nb,
133 + unsigned long action, void *unused)
134 +{
135 + if (action == SYS_RESTART)
136 + mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
137 +
138 + return NOTIFY_DONE;
139 +}
140 +
141 +static struct notifier_block linkstation_reboot_nb = {
142 + .notifier_call = linkstation_reboot_notifier,
143 +};
144 +
145 +static void linkstation_poweroff(void)
146 +{
147 + unregister_reboot_notifier(&linkstation_reboot_nb);
148 + mvphy_reg_intn(0);
149 +
150 + kernel_restart("Power off");
151 +}
152 +
153 +static const struct of_device_id ls_poweroff_of_match[] = {
154 + { .compatible = "buffalo,ls421d" },
155 + { .compatible = "buffalo,ls421de" },
156 + { },
157 +};
158 +
159 +static int __init linkstation_poweroff_init(void)
160 +{
161 + struct mii_bus *bus;
162 + struct device_node *dn;
163 +
164 + dn = of_find_matching_node(NULL, ls_poweroff_of_match);
165 + if (!dn)
166 + return -ENODEV;
167 + of_node_put(dn);
168 +
169 + dn = of_find_node_by_name(NULL, "mdio");
170 + if (!dn)
171 + return -ENODEV;
172 +
173 + bus = of_mdio_find_bus(dn);
174 + of_node_put(dn);
175 + if (!bus)
176 + return -EPROBE_DEFER;
177 +
178 + phydev = phy_find_first(bus);
179 + if (!phydev)
180 + return -EPROBE_DEFER;
181 +
182 + register_reboot_notifier(&linkstation_reboot_nb);
183 + pm_power_off = linkstation_poweroff;
184 +
185 + return 0;
186 +}
187 +
188 +static void __exit linkstation_poweroff_exit(void)
189 +{
190 + pm_power_off = NULL;
191 + unregister_reboot_notifier(&linkstation_reboot_nb);
192 +}
193 +
194 +module_init(linkstation_poweroff_init);
195 +module_exit(linkstation_poweroff_exit);
196 +
197 +MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
198 +MODULE_DESCRIPTION("LinkStation power off driver");
199 +MODULE_LICENSE("GPL v2");