Skip to contentSkip to conversation

power: reset: add driver for LinkStation power off

PATCH Superseded Daniel González Cabanelas 2 participants 4 messages Jun 13, 2020 – Jun 19, 2020 System reset/shutdown drivers

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 power: reset: add driver for LinkStation power off

Some Buffalo LinkStations perform the power off operation, at restart time, depending on the state of an output pin (LED2/INTn) at the ethernet PHY. This pin is also used to wake the machine when a WoL packet is received by the PHY.

The driver is required by the Buffalo LinkStation LS421DE (ARM MVEBU), and other models. Without it, the board remains forever halted if a power off command is executed, unless the PSU is disconnected and connected again.

Add the driver to provide the power off function and also make the WoL feature to be available.

---
Changes in v3:
- Code for PHY autodectection added, "phy-handle,intn" deleted.
Changes in v2:
- The driver is now compatible with the WoL function, the LED2 pin output is now used as INTn. Added the required code to make INTn work properly.
- Code for PHY autodectection deleted, "phy-handle,intn" is now mandatory.
- Replace a dev_info with a dev_dbg.
- Use phy_device_free in the driver remove.
- Cosmetic changes.

 drivers/power/reset/Kconfig                |  11 ++
 drivers/power/reset/Makefile               |   1 +
 drivers/power/reset/linkstation-poweroff.c | 144 +++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/power/reset/linkstation-poweroff.c
drivers/power/reset/Kconfig +11 -0
index f07b982c8d..431cd9f201 100644
@@ -99,6 +99,17 @@ config POWER_RESET_HISI
 	help
 	  Reboot support for Hisilicon boards.
 
+config POWER_RESET_LINKSTATION
+	tristate "Buffalo LinkStation power-off driver"
+	depends on ARCH_MVEBU || COMPILE_TEST
+	depends on OF_MDIO && PHYLIB
+	help
+	  This driver supports turning off some Buffalo LinkStations by
+	  setting an output pin at the ethernet PHY to the correct state.
+	  It also makes the device compatible with the WoL function.
+
+	  Say Y here if you have Buffalo LinkStation LS421D/E.
+
 config POWER_RESET_MSM
 	bool "Qualcomm MSM power-off driver"
 	depends on ARCH_QCOM
drivers/power/reset/Makefile +1 -0
index 5710ca4695..c51eceba9e 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF) += gemini-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
+obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
 obj-$(CONFIG_POWER_RESET_MT6323) += mt6323-poweroff.o
 obj-$(CONFIG_POWER_RESET_OXNAS) += oxnas-restart.o
drivers/power/reset/linkstation-poweroff.c new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw

Hi,

Quoted 145 lines · Daniel González Cabanelas

Some Buffalo LinkStations perform the power off operation, at restart time, depending on the state of an output pin (LED2/INTn) at the ethernet PHY. This pin is also used to wake the machine when a WoL packet is received by the PHY.

The driver is required by the Buffalo LinkStation LS421DE (ARM MVEBU), and other models. Without it, the board remains forever halted if a power off command is executed, unless the PSU is disconnected and connected again.

Add the driver to provide the power off function and also make the WoL feature to be available.

---
Changes in v3:
- Code for PHY autodectection added, "phy-handle,intn" deleted.
Changes in v2:
- The driver is now compatible with the WoL function, the LED2 pin output is now used as INTn. Added the required code to make INTn work properly.
- Code for PHY autodectection deleted, "phy-handle,intn" is now mandatory.
- Replace a dev_info with a dev_dbg.
- Use phy_device_free in the driver remove.
- Cosmetic changes.

 drivers/power/reset/Kconfig                |  11 ++
 drivers/power/reset/Makefile               |   1 +
 drivers/power/reset/linkstation-poweroff.c | 144 +++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/power/reset/linkstation-poweroff.c
drivers/power/reset/Kconfig +11 -0
index f07b982c8d..431cd9f201 100644
@@ -99,6 +99,17 @@ config POWER_RESET_HISI
 	help
 	  Reboot support for Hisilicon boards.
 
+config POWER_RESET_LINKSTATION
+	tristate "Buffalo LinkStation power-off driver"
+	depends on ARCH_MVEBU || COMPILE_TEST
+	depends on OF_MDIO && PHYLIB
+	help
+	  This driver supports turning off some Buffalo LinkStations by
+	  setting an output pin at the ethernet PHY to the correct state.
+	  It also makes the device compatible with the WoL function.
+
+	  Say Y here if you have Buffalo LinkStation LS421D/E.
+
 config POWER_RESET_MSM
 	bool "Qualcomm MSM power-off driver"
 	depends on ARCH_QCOM
drivers/power/reset/Makefile +1 -0
index 5710ca4695..c51eceba9e 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF) += gemini-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
+obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
 obj-$(CONFIG_POWER_RESET_MT6323) += mt6323-poweroff.o
 obj-$(CONFIG_POWER_RESET_OXNAS) += oxnas-restart.o
drivers/power/reset/linkstation-poweroff.c new file +74 -0
new file mode 100644
index 0000000000..688af0a962
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LinkStation power off restart driver
+ * Copyright (C) 2020 Daniel González Cabanelas <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/of_mdio.h>
+#include <linux/of_platform.h>
+#include <linux/reboot.h>
+#include <linux/phy.h>
+
+/* Defines from the eth phy Marvell driver */
+#define MII_MARVELL_COPPER_PAGE		0
+#define MII_MARVELL_LED_PAGE		3
+#define MII_MARVELL_WOL_PAGE		17
+#define MII_MARVELL_PHY_PAGE		22
+
+#define MII_PHY_LED_CTRL		16
+#define MII_88E1318S_PHY_LED_TCR	18
+#define MII_88E1318S_PHY_WOL_CTRL	16
+#define MII_M1011_IEVENT		19
+
+#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE		BIT(7)
+#define MII_88E1318S_PHY_LED_TCR_FORCE_INT		BIT(15)
+#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS	BIT(12)
+#define LED2_FORCE_ON					(0x8 << 8)
+#define LEDMASK						GENMASK(11,8)
+
+static struct phy_device *phydev;
+
+static void mvphy_reg_intn(u16 data)
+{
+	int rc = 0, saved_page;
+
+	saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
+	if (saved_page < 0)
+		goto err;
+
+	/* Force manual LED2 control to let INTn work */
+	__phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
+
+	/* Set the LED[2]/INTn pin to the required state */
+	__phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
+		     MII_88E1318S_PHY_LED_TCR_FORCE_INT,
+		     MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
+
+	if (!data) {
+		/* Clear interrupts to ensure INTn won't be holded in high state */
+		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
+		__phy_read(phydev, MII_M1011_IEVENT);
+
+		/* If WOL was enabled and a magic packet was received before powering
+		 * off, we won't be able to wake up by sending another magic packet. 
+		 * Clear WOL status.
+		 */
+		__phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
+		__phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
+			       MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
+	}
+err:
+	rc = phy_restore_page(phydev, saved_page, rc);
+	if (rc < 0)
+		dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
+}
+
+static int linkstation_reboot_notifier(struct notifier_block *nb,
+				       unsigned long action, void *unused)
+{
+	if (action == SYS_RESTART)
+		mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
+

drivers/power/reset/linkstation-poweroff.c line 84

Quoted 10 lines
drivers/power/reset/linkstation-poweroff.c new file +10 -0
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block linkstation_reboot_nb = {
+	.notifier_call = linkstation_reboot_notifier,
+};
+
+static void linkstation_poweroff(void)
+{
+	unregister_reboot_notifier(&linkstation_reboot_nb);

I think this can be removed here and then you can use devm_register_reboot_notifier in probe and drop the unregister in remove function. Otherwise the driver LGTM.

-- Sebastian

Quoted 66 lines
drivers/power/reset/linkstation-poweroff.c new file +60 -0
+	mvphy_reg_intn(0);
+
+	kernel_restart("Power off");
+}
+
+static int linkstation_poweroff_probe(struct platform_device *pdev)
+{
+	struct mii_bus *bus;
+	struct device_node *dn;
+
+	dn = of_find_node_by_name(NULL, "mdio");
+	if (!dn)
+		return -ENODEV;
+
+	bus = of_mdio_find_bus(dn);
+	of_node_put(dn);
+	if (!bus)
+		return -EPROBE_DEFER;
+
+	phydev = phy_find_first(bus);
+	if (!phydev)
+		return -EPROBE_DEFER;
+
+	register_reboot_notifier(&linkstation_reboot_nb);
+	pm_power_off = linkstation_poweroff;
+
+	dev_dbg(&pdev->dev, "PHY [%s]\n", phydev_name(phydev));
+
+	return 0;
+}
+
+static int linkstation_poweroff_remove(struct platform_device *pdev)
+{
+	pm_power_off = NULL;
+	unregister_reboot_notifier(&linkstation_reboot_nb);
+
+	return 0;
+}
+
+static const struct of_device_id ls_poweroff_of_match[] = {
+	{ .compatible = "linkstation,power-off", },
+	{ },
+};
+
+MODULE_DEVICE_TABLE(of, ls_poweroff_of_match);
+
+static struct platform_driver linkstation_poweroff_driver = {
+	.probe = linkstation_poweroff_probe,
+	.remove = linkstation_poweroff_remove,
+	.driver = {
+		.name = "linkstation_power_off",
+		.of_match_table = ls_poweroff_of_match,
+	},
+};
+
+module_platform_driver(linkstation_poweroff_driver);
+
+MODULE_AUTHOR("Daniel González Cabanelas <[email protected]>");
+MODULE_DESCRIPTION("LinkStation power off driver");
+MODULE_LICENSE("GPL v2");
Signature · 1 line
2.27.0
#2 lore raw

Hi Sebastian,

Quoted 156 lines · Sebastian Reichel

Hi,

Quoted 152 lines · Daniel González Cabanelas

Some Buffalo LinkStations perform the power off operation, at restart time, depending on the state of an output pin (LED2/INTn) at the ethernet PHY. This pin is also used to wake the machine when a WoL packet is received by the PHY.

The driver is required by the Buffalo LinkStation LS421DE (ARM MVEBU), and other models. Without it, the board remains forever halted if a power off command is executed, unless the PSU is disconnected and connected again.

Add the driver to provide the power off function and also make the WoL feature to be available.

---
Changes in v3:
- Code for PHY autodectection added, "phy-handle,intn" deleted.
Changes in v2:
- The driver is now compatible with the WoL function, the LED2 pin output is now used as INTn. Added the required code to make INTn work properly.
- Code for PHY autodectection deleted, "phy-handle,intn" is now mandatory.
- Replace a dev_info with a dev_dbg.
- Use phy_device_free in the driver remove.
- Cosmetic changes.

 drivers/power/reset/Kconfig                |  11 ++
 drivers/power/reset/Makefile               |   1 +
 drivers/power/reset/linkstation-poweroff.c | 144 +++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/power/reset/linkstation-poweroff.c
drivers/power/reset/Kconfig +11 -0
index f07b982c8d..431cd9f201 100644
@@ -99,6 +99,17 @@ config POWER_RESET_HISI
      help
        Reboot support for Hisilicon boards.
 
+config POWER_RESET_LINKSTATION
+     tristate "Buffalo LinkStation power-off driver"
+     depends on ARCH_MVEBU || COMPILE_TEST
+     depends on OF_MDIO && PHYLIB
+     help
+       This driver supports turning off some Buffalo LinkStations by
+       setting an output pin at the ethernet PHY to the correct state.
+       It also makes the device compatible with the WoL function.
+
+       Say Y here if you have Buffalo LinkStation LS421D/E.
+
 config POWER_RESET_MSM
      bool "Qualcomm MSM power-off driver"
      depends on ARCH_QCOM
drivers/power/reset/Makefile +1 -0
index 5710ca4695..c51eceba9e 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF) += gemini-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
+obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
 obj-$(CONFIG_POWER_RESET_MT6323) += mt6323-poweroff.o
 obj-$(CONFIG_POWER_RESET_OXNAS) += oxnas-restart.o
drivers/power/reset/linkstation-poweroff.c new file +81 -0
new file mode 100644
index 0000000000..688af0a962
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LinkStation power off restart driver
+ * Copyright (C) 2020 Daniel González Cabanelas <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/of_mdio.h>
+#include <linux/of_platform.h>
+#include <linux/reboot.h>
+#include <linux/phy.h>
+
+/* Defines from the eth phy Marvell driver */
+#define MII_MARVELL_COPPER_PAGE              0
+#define MII_MARVELL_LED_PAGE         3
+#define MII_MARVELL_WOL_PAGE         17
+#define MII_MARVELL_PHY_PAGE         22
+
+#define MII_PHY_LED_CTRL             16
+#define MII_88E1318S_PHY_LED_TCR     18
+#define MII_88E1318S_PHY_WOL_CTRL    16
+#define MII_M1011_IEVENT             19
+
+#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE         BIT(7)
+#define MII_88E1318S_PHY_LED_TCR_FORCE_INT           BIT(15)
+#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS   BIT(12)
+#define LED2_FORCE_ON                                        (0x8 << 8)
+#define LEDMASK                                              GENMASK(11,8)
+
+static struct phy_device *phydev;
+
+static void mvphy_reg_intn(u16 data)
+{
+     int rc = 0, saved_page;
+
+     saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
+     if (saved_page < 0)
+             goto err;
+
+     /* Force manual LED2 control to let INTn work */
+     __phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
+
+     /* Set the LED[2]/INTn pin to the required state */
+     __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
+                  MII_88E1318S_PHY_LED_TCR_FORCE_INT,
+                  MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
+
+     if (!data) {
+             /* Clear interrupts to ensure INTn won't be holded in high state */
+             __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
+             __phy_read(phydev, MII_M1011_IEVENT);
+
+             /* If WOL was enabled and a magic packet was received before powering
+              * off, we won't be able to wake up by sending another magic packet.
+              * Clear WOL status.
+              */
+             __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
+             __phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
+                            MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
+     }
+err:
+     rc = phy_restore_page(phydev, saved_page, rc);
+     if (rc < 0)
+             dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
+}
+
+static int linkstation_reboot_notifier(struct notifier_block *nb,
+                                    unsigned long action, void *unused)
+{
+     if (action == SYS_RESTART)
+             mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
+
+     return NOTIFY_DONE;
+}
+
+static struct notifier_block linkstation_reboot_nb = {
+     .notifier_call = linkstation_reboot_notifier,
+};
+
Quoted 8 lines

drivers/power/reset/linkstation-poweroff.c line 84

Quoted 3 lines
drivers/power/reset/linkstation-poweroff.c new file +3 -0
+static void linkstation_poweroff(void)
+{
+     unregister_reboot_notifier(&linkstation_reboot_nb);

I think this can be removed here and then you can use devm_register_reboot_notifier in probe and drop the unregister in remove function. Otherwise the driver LGTM.

I've tested this change and it didn't work. Probably because without unregistering the notifier, when kernel_restart("Power off") is called, the notifier is still there, and we end performing a restart instead the power off operation.

Regards
Daniel

Quoted 69 lines

-- Sebastian

Quoted 66 lines
drivers/power/reset/linkstation-poweroff.c new file +60 -1
+     mvphy_reg_intn(0);
+
+     kernel_restart("Power off");
+}
+
+static int linkstation_poweroff_probe(struct platform_device *pdev)
+{
+     struct mii_bus *bus;
+     struct device_node *dn;
+
+     dn = of_find_node_by_name(NULL, "mdio");
+     if (!dn)
+             return -ENODEV;
+
+     bus = of_mdio_find_bus(dn);
+     of_node_put(dn);
+     if (!bus)
+             return -EPROBE_DEFER;
+
+     phydev = phy_find_first(bus);
+     if (!phydev)
+             return -EPROBE_DEFER;
+
+     register_reboot_notifier(&linkstation_reboot_nb);
+     pm_power_off = linkstation_poweroff;
+
+     dev_dbg(&pdev->dev, "PHY [%s]\n", phydev_name(phydev));
+
+     return 0;
+}
+
+static int linkstation_poweroff_remove(struct platform_device *pdev)
+{
+     pm_power_off = NULL;
+     unregister_reboot_notifier(&linkstation_reboot_nb);
+
+     return 0;
+}
+
+static const struct of_device_id ls_poweroff_of_match[] = {
+     { .compatible = "linkstation,power-off", },
+     { },
+};
+
+MODULE_DEVICE_TABLE(of, ls_poweroff_of_match);
+
+static struct platform_driver linkstation_poweroff_driver = {
+     .probe = linkstation_poweroff_probe,
+     .remove = linkstation_poweroff_remove,
+     .driver = {
+             .name = "linkstation_power_off",
+             .of_match_table = ls_poweroff_of_match,
+     },
+};
+
+module_platform_driver(linkstation_poweroff_driver);
+
+MODULE_AUTHOR("Daniel González Cabanelas <[email protected]>");
+MODULE_DESCRIPTION("LinkStation power off driver");
+MODULE_LICENSE("GPL v2");
--

2.27.0

#3 lore raw

Hi,

Quoted 169 lines · Daniel González Cabanelas

Hi Sebastian,

Quoted 156 lines · Sebastian Reichel

Hi,

Quoted 152 lines · Daniel González Cabanelas

Some Buffalo LinkStations perform the power off operation, at restart time, depending on the state of an output pin (LED2/INTn) at the ethernet PHY. This pin is also used to wake the machine when a WoL packet is received by the PHY.

The driver is required by the Buffalo LinkStation LS421DE (ARM MVEBU), and other models. Without it, the board remains forever halted if a power off command is executed, unless the PSU is disconnected and connected again.

Add the driver to provide the power off function and also make the WoL feature to be available.

---
Changes in v3:
- Code for PHY autodectection added, "phy-handle,intn" deleted.
Changes in v2:
- The driver is now compatible with the WoL function, the LED2 pin output is now used as INTn. Added the required code to make INTn work properly.
- Code for PHY autodectection deleted, "phy-handle,intn" is now mandatory.
- Replace a dev_info with a dev_dbg.
- Use phy_device_free in the driver remove.
- Cosmetic changes.

 drivers/power/reset/Kconfig                |  11 ++
 drivers/power/reset/Makefile               |   1 +
 drivers/power/reset/linkstation-poweroff.c | 144 +++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/power/reset/linkstation-poweroff.c
drivers/power/reset/Kconfig +11 -0
index f07b982c8d..431cd9f201 100644
@@ -99,6 +99,17 @@ config POWER_RESET_HISI
      help
        Reboot support for Hisilicon boards.
 
+config POWER_RESET_LINKSTATION
+     tristate "Buffalo LinkStation power-off driver"
+     depends on ARCH_MVEBU || COMPILE_TEST
+     depends on OF_MDIO && PHYLIB
+     help
+       This driver supports turning off some Buffalo LinkStations by
+       setting an output pin at the ethernet PHY to the correct state.
+       It also makes the device compatible with the WoL function.
+
+       Say Y here if you have Buffalo LinkStation LS421D/E.
+
 config POWER_RESET_MSM
      bool "Qualcomm MSM power-off driver"
      depends on ARCH_QCOM
drivers/power/reset/Makefile +1 -0
index 5710ca4695..c51eceba9e 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF) += gemini-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
+obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
 obj-$(CONFIG_POWER_RESET_MT6323) += mt6323-poweroff.o
 obj-$(CONFIG_POWER_RESET_OXNAS) += oxnas-restart.o
drivers/power/reset/linkstation-poweroff.c new file +81 -0
new file mode 100644
index 0000000000..688af0a962
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LinkStation power off restart driver
+ * Copyright (C) 2020 Daniel González Cabanelas <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/of_mdio.h>
+#include <linux/of_platform.h>
+#include <linux/reboot.h>
+#include <linux/phy.h>
+
+/* Defines from the eth phy Marvell driver */
+#define MII_MARVELL_COPPER_PAGE              0
+#define MII_MARVELL_LED_PAGE         3
+#define MII_MARVELL_WOL_PAGE         17
+#define MII_MARVELL_PHY_PAGE         22
+
+#define MII_PHY_LED_CTRL             16
+#define MII_88E1318S_PHY_LED_TCR     18
+#define MII_88E1318S_PHY_WOL_CTRL    16
+#define MII_M1011_IEVENT             19
+
+#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE         BIT(7)
+#define MII_88E1318S_PHY_LED_TCR_FORCE_INT           BIT(15)
+#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS   BIT(12)
+#define LED2_FORCE_ON                                        (0x8 << 8)
+#define LEDMASK                                              GENMASK(11,8)
+
+static struct phy_device *phydev;
+
+static void mvphy_reg_intn(u16 data)
+{
+     int rc = 0, saved_page;
+
+     saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
+     if (saved_page < 0)
+             goto err;
+
+     /* Force manual LED2 control to let INTn work */
+     __phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
+
+     /* Set the LED[2]/INTn pin to the required state */
+     __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
+                  MII_88E1318S_PHY_LED_TCR_FORCE_INT,
+                  MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
+
+     if (!data) {
+             /* Clear interrupts to ensure INTn won't be holded in high state */
+             __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
+             __phy_read(phydev, MII_M1011_IEVENT);
+
+             /* If WOL was enabled and a magic packet was received before powering
+              * off, we won't be able to wake up by sending another magic packet.
+              * Clear WOL status.
+              */
+             __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
+             __phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
+                            MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
+     }
+err:
+     rc = phy_restore_page(phydev, saved_page, rc);
+     if (rc < 0)
+             dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
+}
+
+static int linkstation_reboot_notifier(struct notifier_block *nb,
+                                    unsigned long action, void *unused)
+{
+     if (action == SYS_RESTART)
+             mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
+
+     return NOTIFY_DONE;
+}
+
+static struct notifier_block linkstation_reboot_nb = {
+     .notifier_call = linkstation_reboot_notifier,
+};
+
Quoted 8 lines

drivers/power/reset/linkstation-poweroff.c line 84

Quoted 3 lines
drivers/power/reset/linkstation-poweroff.c new file +3 -0
+static void linkstation_poweroff(void)
+{
+     unregister_reboot_notifier(&linkstation_reboot_nb);

I think this can be removed here and then you can use devm_register_reboot_notifier in probe and drop the unregister in remove function. Otherwise the driver LGTM.

Quoted 4 lines

I've tested this change and it didn't work. Probably because without unregistering the notifier, when kernel_restart("Power off") is called, the notifier is still there, and we end performing a restart instead the power off operation.

Strange. Well it's no big issue, so let's keep it as is. I will wait a bit with merging to give Rob a chance to review the updated DT bindings.

-- Sebastian

Quoted 66 lines
Quoted 66 lines
Quoted 66 lines
drivers/power/reset/linkstation-poweroff.c new file +60 -1
+     mvphy_reg_intn(0);
+
+     kernel_restart("Power off");
+}
+
+static int linkstation_poweroff_probe(struct platform_device *pdev)
+{
+     struct mii_bus *bus;
+     struct device_node *dn;
+
+     dn = of_find_node_by_name(NULL, "mdio");
+     if (!dn)
+             return -ENODEV;
+
+     bus = of_mdio_find_bus(dn);
+     of_node_put(dn);
+     if (!bus)
+             return -EPROBE_DEFER;
+
+     phydev = phy_find_first(bus);
+     if (!phydev)
+             return -EPROBE_DEFER;
+
+     register_reboot_notifier(&linkstation_reboot_nb);
+     pm_power_off = linkstation_poweroff;
+
+     dev_dbg(&pdev->dev, "PHY [%s]\n", phydev_name(phydev));
+
+     return 0;
+}
+
+static int linkstation_poweroff_remove(struct platform_device *pdev)
+{
+     pm_power_off = NULL;
+     unregister_reboot_notifier(&linkstation_reboot_nb);
+
+     return 0;
+}
+
+static const struct of_device_id ls_poweroff_of_match[] = {
+     { .compatible = "linkstation,power-off", },
+     { },
+};
+
+MODULE_DEVICE_TABLE(of, ls_poweroff_of_match);
+
+static struct platform_driver linkstation_poweroff_driver = {
+     .probe = linkstation_poweroff_probe,
+     .remove = linkstation_poweroff_remove,
+     .driver = {
+             .name = "linkstation_power_off",
+             .of_match_table = ls_poweroff_of_match,
+     },
+};
+
+module_platform_driver(linkstation_poweroff_driver);
+
+MODULE_AUTHOR("Daniel González Cabanelas <[email protected]>");
+MODULE_DESCRIPTION("LinkStation power off driver");
+MODULE_LICENSE("GPL v2");
--

2.27.0