xref: /OK3568_Linux_fs/kernel/drivers/power/reset/linkstation-poweroff.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * LinkStation power off restart driver
4*4882a593Smuzhiyun  * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/notifier.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/of_mdio.h>
11*4882a593Smuzhiyun #include <linux/of_platform.h>
12*4882a593Smuzhiyun #include <linux/reboot.h>
13*4882a593Smuzhiyun #include <linux/phy.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /* Defines from the eth phy Marvell driver */
16*4882a593Smuzhiyun #define MII_MARVELL_COPPER_PAGE		0
17*4882a593Smuzhiyun #define MII_MARVELL_LED_PAGE		3
18*4882a593Smuzhiyun #define MII_MARVELL_WOL_PAGE		17
19*4882a593Smuzhiyun #define MII_MARVELL_PHY_PAGE		22
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define MII_PHY_LED_CTRL		16
22*4882a593Smuzhiyun #define MII_88E1318S_PHY_LED_TCR	18
23*4882a593Smuzhiyun #define MII_88E1318S_PHY_WOL_CTRL	16
24*4882a593Smuzhiyun #define MII_M1011_IEVENT		19
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE		BIT(7)
27*4882a593Smuzhiyun #define MII_88E1318S_PHY_LED_TCR_FORCE_INT		BIT(15)
28*4882a593Smuzhiyun #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS	BIT(12)
29*4882a593Smuzhiyun #define LED2_FORCE_ON					(0x8 << 8)
30*4882a593Smuzhiyun #define LEDMASK						GENMASK(11,8)
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun static struct phy_device *phydev;
33*4882a593Smuzhiyun 
mvphy_reg_intn(u16 data)34*4882a593Smuzhiyun static void mvphy_reg_intn(u16 data)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	int rc = 0, saved_page;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
39*4882a593Smuzhiyun 	if (saved_page < 0)
40*4882a593Smuzhiyun 		goto err;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	/* Force manual LED2 control to let INTn work */
43*4882a593Smuzhiyun 	__phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	/* Set the LED[2]/INTn pin to the required state */
46*4882a593Smuzhiyun 	__phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
47*4882a593Smuzhiyun 		     MII_88E1318S_PHY_LED_TCR_FORCE_INT,
48*4882a593Smuzhiyun 		     MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (!data) {
51*4882a593Smuzhiyun 		/* Clear interrupts to ensure INTn won't be holded in high state */
52*4882a593Smuzhiyun 		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
53*4882a593Smuzhiyun 		__phy_read(phydev, MII_M1011_IEVENT);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 		/* If WOL was enabled and a magic packet was received before powering
56*4882a593Smuzhiyun 		 * off, we won't be able to wake up by sending another magic packet.
57*4882a593Smuzhiyun 		 * Clear WOL status.
58*4882a593Smuzhiyun 		 */
59*4882a593Smuzhiyun 		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
60*4882a593Smuzhiyun 		__phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
61*4882a593Smuzhiyun 			       MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun err:
64*4882a593Smuzhiyun 	rc = phy_restore_page(phydev, saved_page, rc);
65*4882a593Smuzhiyun 	if (rc < 0)
66*4882a593Smuzhiyun 		dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
linkstation_reboot_notifier(struct notifier_block * nb,unsigned long action,void * unused)69*4882a593Smuzhiyun static int linkstation_reboot_notifier(struct notifier_block *nb,
70*4882a593Smuzhiyun 				       unsigned long action, void *unused)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	if (action == SYS_RESTART)
73*4882a593Smuzhiyun 		mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return NOTIFY_DONE;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun static struct notifier_block linkstation_reboot_nb = {
79*4882a593Smuzhiyun 	.notifier_call = linkstation_reboot_notifier,
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun 
linkstation_poweroff(void)82*4882a593Smuzhiyun static void linkstation_poweroff(void)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	unregister_reboot_notifier(&linkstation_reboot_nb);
85*4882a593Smuzhiyun 	mvphy_reg_intn(0);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	kernel_restart("Power off");
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun static const struct of_device_id ls_poweroff_of_match[] = {
91*4882a593Smuzhiyun 	{ .compatible = "buffalo,ls421d" },
92*4882a593Smuzhiyun 	{ .compatible = "buffalo,ls421de" },
93*4882a593Smuzhiyun 	{ },
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
linkstation_poweroff_init(void)96*4882a593Smuzhiyun static int __init linkstation_poweroff_init(void)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct mii_bus *bus;
99*4882a593Smuzhiyun 	struct device_node *dn;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	dn = of_find_matching_node(NULL, ls_poweroff_of_match);
102*4882a593Smuzhiyun 	if (!dn)
103*4882a593Smuzhiyun 		return -ENODEV;
104*4882a593Smuzhiyun 	of_node_put(dn);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	dn = of_find_node_by_name(NULL, "mdio");
107*4882a593Smuzhiyun 	if (!dn)
108*4882a593Smuzhiyun 		return -ENODEV;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	bus = of_mdio_find_bus(dn);
111*4882a593Smuzhiyun 	of_node_put(dn);
112*4882a593Smuzhiyun 	if (!bus)
113*4882a593Smuzhiyun 		return -EPROBE_DEFER;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	phydev = phy_find_first(bus);
116*4882a593Smuzhiyun 	if (!phydev)
117*4882a593Smuzhiyun 		return -EPROBE_DEFER;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	register_reboot_notifier(&linkstation_reboot_nb);
120*4882a593Smuzhiyun 	pm_power_off = linkstation_poweroff;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
linkstation_poweroff_exit(void)125*4882a593Smuzhiyun static void __exit linkstation_poweroff_exit(void)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	pm_power_off = NULL;
128*4882a593Smuzhiyun 	unregister_reboot_notifier(&linkstation_reboot_nb);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun module_init(linkstation_poweroff_init);
132*4882a593Smuzhiyun module_exit(linkstation_poweroff_exit);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
135*4882a593Smuzhiyun MODULE_DESCRIPTION("LinkStation power off driver");
136*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
137