linkstation-poweroff.c 5.2 KB

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