xref: /openbmc/linux/drivers/power/reset/linkstation-poweroff.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1a7f79f99SDaniel González Cabanelas // SPDX-License-Identifier: GPL-2.0
2a7f79f99SDaniel González Cabanelas /*
3a7f79f99SDaniel González Cabanelas  * LinkStation power off restart driver
4a7f79f99SDaniel González Cabanelas  * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
5a7f79f99SDaniel González Cabanelas  */
6a7f79f99SDaniel González Cabanelas 
7a7f79f99SDaniel González Cabanelas #include <linux/module.h>
8a7f79f99SDaniel González Cabanelas #include <linux/notifier.h>
9a7f79f99SDaniel González Cabanelas #include <linux/of.h>
10a7f79f99SDaniel González Cabanelas #include <linux/of_mdio.h>
11a7f79f99SDaniel González Cabanelas #include <linux/of_platform.h>
12a7f79f99SDaniel González Cabanelas #include <linux/reboot.h>
13a7f79f99SDaniel González Cabanelas #include <linux/phy.h>
14a7f79f99SDaniel González Cabanelas 
15a7f79f99SDaniel González Cabanelas /* Defines from the eth phy Marvell driver */
16a7f79f99SDaniel González Cabanelas #define MII_MARVELL_COPPER_PAGE		0
17a7f79f99SDaniel González Cabanelas #define MII_MARVELL_LED_PAGE		3
18a7f79f99SDaniel González Cabanelas #define MII_MARVELL_WOL_PAGE		17
19a7f79f99SDaniel González Cabanelas #define MII_MARVELL_PHY_PAGE		22
20a7f79f99SDaniel González Cabanelas 
21a7f79f99SDaniel González Cabanelas #define MII_PHY_LED_CTRL		16
22*0c77ec3dSPawel Dembicki #define MII_PHY_LED_POL_CTRL		17
23a7f79f99SDaniel González Cabanelas #define MII_88E1318S_PHY_LED_TCR	18
24a7f79f99SDaniel González Cabanelas #define MII_88E1318S_PHY_WOL_CTRL	16
25a7f79f99SDaniel González Cabanelas #define MII_M1011_IEVENT		19
26a7f79f99SDaniel González Cabanelas 
27a7f79f99SDaniel González Cabanelas #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE		BIT(7)
28a7f79f99SDaniel González Cabanelas #define MII_88E1318S_PHY_LED_TCR_FORCE_INT		BIT(15)
29a7f79f99SDaniel González Cabanelas #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS	BIT(12)
30a7f79f99SDaniel González Cabanelas #define LED2_FORCE_ON					(0x8 << 8)
31a7f79f99SDaniel González Cabanelas #define LEDMASK						GENMASK(11,8)
32a7f79f99SDaniel González Cabanelas 
33*0c77ec3dSPawel Dembicki #define MII_88E1318S_PHY_LED_POL_LED2		BIT(4)
34*0c77ec3dSPawel Dembicki 
35e2f471efSPawel Dembicki struct power_off_cfg {
36e2f471efSPawel Dembicki 	char *mdio_node_name;
37e2f471efSPawel Dembicki 	void (*phy_set_reg)(bool restart);
38e2f471efSPawel Dembicki };
39a7f79f99SDaniel González Cabanelas 
40e2f471efSPawel Dembicki static struct phy_device *phydev;
41e2f471efSPawel Dembicki static const struct power_off_cfg *cfg;
42e2f471efSPawel Dembicki 
linkstation_mvphy_reg_intn(bool restart)43e2f471efSPawel Dembicki static void linkstation_mvphy_reg_intn(bool restart)
44a7f79f99SDaniel González Cabanelas {
45a7f79f99SDaniel González Cabanelas 	int rc = 0, saved_page;
46e2f471efSPawel Dembicki 	u16 data = 0;
47e2f471efSPawel Dembicki 
48e2f471efSPawel Dembicki 	if (restart)
49e2f471efSPawel Dembicki 		data = MII_88E1318S_PHY_LED_TCR_FORCE_INT;
50a7f79f99SDaniel González Cabanelas 
51a7f79f99SDaniel González Cabanelas 	saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
52a7f79f99SDaniel González Cabanelas 	if (saved_page < 0)
53a7f79f99SDaniel González Cabanelas 		goto err;
54a7f79f99SDaniel González Cabanelas 
55a7f79f99SDaniel González Cabanelas 	/* Force manual LED2 control to let INTn work */
56a7f79f99SDaniel González Cabanelas 	__phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
57a7f79f99SDaniel González Cabanelas 
58a7f79f99SDaniel González Cabanelas 	/* Set the LED[2]/INTn pin to the required state */
59a7f79f99SDaniel González Cabanelas 	__phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
60a7f79f99SDaniel González Cabanelas 		     MII_88E1318S_PHY_LED_TCR_FORCE_INT,
61a7f79f99SDaniel González Cabanelas 		     MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
62a7f79f99SDaniel González Cabanelas 
63a7f79f99SDaniel González Cabanelas 	if (!data) {
64a7f79f99SDaniel González Cabanelas 		/* Clear interrupts to ensure INTn won't be holded in high state */
65a7f79f99SDaniel González Cabanelas 		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
66a7f79f99SDaniel González Cabanelas 		__phy_read(phydev, MII_M1011_IEVENT);
67a7f79f99SDaniel González Cabanelas 
68a7f79f99SDaniel González Cabanelas 		/* If WOL was enabled and a magic packet was received before powering
69a7f79f99SDaniel González Cabanelas 		 * off, we won't be able to wake up by sending another magic packet.
70a7f79f99SDaniel González Cabanelas 		 * Clear WOL status.
71a7f79f99SDaniel González Cabanelas 		 */
72a7f79f99SDaniel González Cabanelas 		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
73a7f79f99SDaniel González Cabanelas 		__phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
74a7f79f99SDaniel González Cabanelas 			       MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
75a7f79f99SDaniel González Cabanelas 	}
76a7f79f99SDaniel González Cabanelas err:
77a7f79f99SDaniel González Cabanelas 	rc = phy_restore_page(phydev, saved_page, rc);
78a7f79f99SDaniel González Cabanelas 	if (rc < 0)
79a7f79f99SDaniel González Cabanelas 		dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
80a7f79f99SDaniel González Cabanelas }
81a7f79f99SDaniel González Cabanelas 
readynas_mvphy_set_reg(bool restart)82*0c77ec3dSPawel Dembicki static void readynas_mvphy_set_reg(bool restart)
83*0c77ec3dSPawel Dembicki {
84*0c77ec3dSPawel Dembicki 	int rc = 0, saved_page;
85*0c77ec3dSPawel Dembicki 	u16 data = 0;
86*0c77ec3dSPawel Dembicki 
87*0c77ec3dSPawel Dembicki 	if (restart)
88*0c77ec3dSPawel Dembicki 		data = MII_88E1318S_PHY_LED_POL_LED2;
89*0c77ec3dSPawel Dembicki 
90*0c77ec3dSPawel Dembicki 	saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
91*0c77ec3dSPawel Dembicki 	if (saved_page < 0)
92*0c77ec3dSPawel Dembicki 		goto err;
93*0c77ec3dSPawel Dembicki 
94*0c77ec3dSPawel Dembicki 	/* Set the LED[2].0 Polarity bit to the required state */
95*0c77ec3dSPawel Dembicki 	__phy_modify(phydev, MII_PHY_LED_POL_CTRL,
96*0c77ec3dSPawel Dembicki 		     MII_88E1318S_PHY_LED_POL_LED2, data);
97*0c77ec3dSPawel Dembicki 
98*0c77ec3dSPawel Dembicki 	if (!data) {
99*0c77ec3dSPawel Dembicki 		/* If WOL was enabled and a magic packet was received before powering
100*0c77ec3dSPawel Dembicki 		 * off, we won't be able to wake up by sending another magic packet.
101*0c77ec3dSPawel Dembicki 		 * Clear WOL status.
102*0c77ec3dSPawel Dembicki 		 */
103*0c77ec3dSPawel Dembicki 		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
104*0c77ec3dSPawel Dembicki 		__phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
105*0c77ec3dSPawel Dembicki 			       MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
106*0c77ec3dSPawel Dembicki 	}
107*0c77ec3dSPawel Dembicki err:
108*0c77ec3dSPawel Dembicki 	rc = phy_restore_page(phydev, saved_page, rc);
109*0c77ec3dSPawel Dembicki 	if (rc < 0)
110*0c77ec3dSPawel Dembicki 		dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
111*0c77ec3dSPawel Dembicki }
112*0c77ec3dSPawel Dembicki 
113e2f471efSPawel Dembicki static const struct power_off_cfg linkstation_power_off_cfg = {
114e2f471efSPawel Dembicki 	.mdio_node_name = "mdio",
115e2f471efSPawel Dembicki 	.phy_set_reg = linkstation_mvphy_reg_intn,
116e2f471efSPawel Dembicki };
117e2f471efSPawel Dembicki 
118*0c77ec3dSPawel Dembicki static const struct power_off_cfg readynas_power_off_cfg = {
119*0c77ec3dSPawel Dembicki 	.mdio_node_name = "mdio-bus",
120*0c77ec3dSPawel Dembicki 	.phy_set_reg = readynas_mvphy_set_reg,
121*0c77ec3dSPawel Dembicki };
122*0c77ec3dSPawel Dembicki 
linkstation_reboot_notifier(struct notifier_block * nb,unsigned long action,void * unused)123a7f79f99SDaniel González Cabanelas static int linkstation_reboot_notifier(struct notifier_block *nb,
124a7f79f99SDaniel González Cabanelas 				       unsigned long action, void *unused)
125a7f79f99SDaniel González Cabanelas {
126a7f79f99SDaniel González Cabanelas 	if (action == SYS_RESTART)
127e2f471efSPawel Dembicki 		cfg->phy_set_reg(true);
128a7f79f99SDaniel González Cabanelas 
129a7f79f99SDaniel González Cabanelas 	return NOTIFY_DONE;
130a7f79f99SDaniel González Cabanelas }
131a7f79f99SDaniel González Cabanelas 
132a7f79f99SDaniel González Cabanelas static struct notifier_block linkstation_reboot_nb = {
133a7f79f99SDaniel González Cabanelas 	.notifier_call = linkstation_reboot_notifier,
134a7f79f99SDaniel González Cabanelas };
135a7f79f99SDaniel González Cabanelas 
linkstation_poweroff(void)136a7f79f99SDaniel González Cabanelas static void linkstation_poweroff(void)
137a7f79f99SDaniel González Cabanelas {
138a7f79f99SDaniel González Cabanelas 	unregister_reboot_notifier(&linkstation_reboot_nb);
139e2f471efSPawel Dembicki 	cfg->phy_set_reg(false);
140a7f79f99SDaniel González Cabanelas 
141a7f79f99SDaniel González Cabanelas 	kernel_restart("Power off");
142a7f79f99SDaniel González Cabanelas }
143a7f79f99SDaniel González Cabanelas 
144a7f79f99SDaniel González Cabanelas static const struct of_device_id ls_poweroff_of_match[] = {
145e2f471efSPawel Dembicki 	{ .compatible = "buffalo,ls421d",
146e2f471efSPawel Dembicki 	  .data = &linkstation_power_off_cfg,
147e2f471efSPawel Dembicki 	},
148e2f471efSPawel Dembicki 	{ .compatible = "buffalo,ls421de",
149e2f471efSPawel Dembicki 	  .data = &linkstation_power_off_cfg,
150e2f471efSPawel Dembicki 	},
151*0c77ec3dSPawel Dembicki 	{ .compatible = "netgear,readynas-duo-v2",
152*0c77ec3dSPawel Dembicki 	  .data = &readynas_power_off_cfg,
153*0c77ec3dSPawel Dembicki 	},
154a7f79f99SDaniel González Cabanelas 	{ },
155a7f79f99SDaniel González Cabanelas };
156a7f79f99SDaniel González Cabanelas 
linkstation_poweroff_init(void)157a7f79f99SDaniel González Cabanelas static int __init linkstation_poweroff_init(void)
158a7f79f99SDaniel González Cabanelas {
159a7f79f99SDaniel González Cabanelas 	struct mii_bus *bus;
160a7f79f99SDaniel González Cabanelas 	struct device_node *dn;
161e2f471efSPawel Dembicki 	const struct of_device_id *match;
162a7f79f99SDaniel González Cabanelas 
163a7f79f99SDaniel González Cabanelas 	dn = of_find_matching_node(NULL, ls_poweroff_of_match);
164a7f79f99SDaniel González Cabanelas 	if (!dn)
165a7f79f99SDaniel González Cabanelas 		return -ENODEV;
166a7f79f99SDaniel González Cabanelas 	of_node_put(dn);
167a7f79f99SDaniel González Cabanelas 
168e2f471efSPawel Dembicki 	match = of_match_node(ls_poweroff_of_match, dn);
169e2f471efSPawel Dembicki 	cfg = match->data;
170e2f471efSPawel Dembicki 
171e2f471efSPawel Dembicki 	dn = of_find_node_by_name(NULL, cfg->mdio_node_name);
172a7f79f99SDaniel González Cabanelas 	if (!dn)
173a7f79f99SDaniel González Cabanelas 		return -ENODEV;
174a7f79f99SDaniel González Cabanelas 
175a7f79f99SDaniel González Cabanelas 	bus = of_mdio_find_bus(dn);
176a7f79f99SDaniel González Cabanelas 	of_node_put(dn);
177a7f79f99SDaniel González Cabanelas 	if (!bus)
178a7f79f99SDaniel González Cabanelas 		return -EPROBE_DEFER;
179a7f79f99SDaniel González Cabanelas 
180a7f79f99SDaniel González Cabanelas 	phydev = phy_find_first(bus);
1811027a42cSDaniel González Cabanelas 	put_device(&bus->dev);
182a7f79f99SDaniel González Cabanelas 	if (!phydev)
183a7f79f99SDaniel González Cabanelas 		return -EPROBE_DEFER;
184a7f79f99SDaniel González Cabanelas 
185a7f79f99SDaniel González Cabanelas 	register_reboot_notifier(&linkstation_reboot_nb);
186a7f79f99SDaniel González Cabanelas 	pm_power_off = linkstation_poweroff;
187a7f79f99SDaniel González Cabanelas 
188a7f79f99SDaniel González Cabanelas 	return 0;
189a7f79f99SDaniel González Cabanelas }
190a7f79f99SDaniel González Cabanelas 
linkstation_poweroff_exit(void)191a7f79f99SDaniel González Cabanelas static void __exit linkstation_poweroff_exit(void)
192a7f79f99SDaniel González Cabanelas {
193a7f79f99SDaniel González Cabanelas 	pm_power_off = NULL;
194a7f79f99SDaniel González Cabanelas 	unregister_reboot_notifier(&linkstation_reboot_nb);
195a7f79f99SDaniel González Cabanelas }
196a7f79f99SDaniel González Cabanelas 
197a7f79f99SDaniel González Cabanelas module_init(linkstation_poweroff_init);
198a7f79f99SDaniel González Cabanelas module_exit(linkstation_poweroff_exit);
199a7f79f99SDaniel González Cabanelas 
200a7f79f99SDaniel González Cabanelas MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
201a7f79f99SDaniel González Cabanelas MODULE_DESCRIPTION("LinkStation power off driver");
202a7f79f99SDaniel González Cabanelas MODULE_LICENSE("GPL v2");
203