025-power-reset-add-driver-for-LinkStation-power-off.patch 6.4 KB

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