linkstation-poweroff.c 3.5 KB

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