qnap-poweroff.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * QNAP Turbo NAS Board power off. Can also be used on Synology devices.
  4. *
  5. * Copyright (C) 2012 Andrew Lunn <[email protected]>
  6. *
  7. * Based on the code from:
  8. *
  9. * Copyright (C) 2009 Martin Michlmayr <[email protected]>
  10. * Copyright (C) 2008 Byron Bradley <[email protected]>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/serial_reg.h>
  16. #include <linux/of.h>
  17. #include <linux/io.h>
  18. #include <linux/clk.h>
  19. #define UART1_REG(x) (base + ((UART_##x) << 2))
  20. struct power_off_cfg {
  21. u32 baud;
  22. char cmd;
  23. };
  24. static const struct power_off_cfg qnap_power_off_cfg = {
  25. .baud = 19200,
  26. .cmd = 'A',
  27. };
  28. static const struct power_off_cfg synology_power_off_cfg = {
  29. .baud = 9600,
  30. .cmd = '1',
  31. };
  32. static const struct of_device_id qnap_power_off_of_match_table[] = {
  33. { .compatible = "qnap,power-off",
  34. .data = &qnap_power_off_cfg,
  35. },
  36. { .compatible = "synology,power-off",
  37. .data = &synology_power_off_cfg,
  38. },
  39. {}
  40. };
  41. MODULE_DEVICE_TABLE(of, qnap_power_off_of_match_table);
  42. static void __iomem *base;
  43. static unsigned long tclk;
  44. static const struct power_off_cfg *cfg;
  45. static void qnap_power_off(void)
  46. {
  47. const unsigned divisor = ((tclk + (8 * cfg->baud)) / (16 * cfg->baud));
  48. pr_err("%s: triggering power-off...\n", __func__);
  49. /* hijack UART1 and reset into sane state */
  50. writel(0x83, UART1_REG(LCR));
  51. writel(divisor & 0xff, UART1_REG(DLL));
  52. writel((divisor >> 8) & 0xff, UART1_REG(DLM));
  53. writel(0x03, UART1_REG(LCR));
  54. writel(0x00, UART1_REG(IER));
  55. writel(0x00, UART1_REG(FCR));
  56. writel(0x00, UART1_REG(MCR));
  57. /* send the power-off command to PIC */
  58. writel(cfg->cmd, UART1_REG(TX));
  59. }
  60. static int qnap_power_off_probe(struct platform_device *pdev)
  61. {
  62. struct device_node *np = pdev->dev.of_node;
  63. struct resource *res;
  64. struct clk *clk;
  65. const struct of_device_id *match =
  66. of_match_node(qnap_power_off_of_match_table, np);
  67. cfg = match->data;
  68. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. if (!res) {
  70. dev_err(&pdev->dev, "Missing resource");
  71. return -EINVAL;
  72. }
  73. base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  74. if (!base) {
  75. dev_err(&pdev->dev, "Unable to map resource");
  76. return -EINVAL;
  77. }
  78. /* We need to know tclk in order to calculate the UART divisor */
  79. clk = devm_clk_get(&pdev->dev, NULL);
  80. if (IS_ERR(clk)) {
  81. dev_err(&pdev->dev, "Clk missing");
  82. return PTR_ERR(clk);
  83. }
  84. tclk = clk_get_rate(clk);
  85. /* Check that nothing else has already setup a handler */
  86. if (pm_power_off) {
  87. dev_err(&pdev->dev, "pm_power_off already claimed for %ps",
  88. pm_power_off);
  89. return -EBUSY;
  90. }
  91. pm_power_off = qnap_power_off;
  92. return 0;
  93. }
  94. static int qnap_power_off_remove(struct platform_device *pdev)
  95. {
  96. pm_power_off = NULL;
  97. return 0;
  98. }
  99. static struct platform_driver qnap_power_off_driver = {
  100. .probe = qnap_power_off_probe,
  101. .remove = qnap_power_off_remove,
  102. .driver = {
  103. .name = "qnap_power_off",
  104. .of_match_table = of_match_ptr(qnap_power_off_of_match_table),
  105. },
  106. };
  107. module_platform_driver(qnap_power_off_driver);
  108. MODULE_AUTHOR("Andrew Lunn <[email protected]>");
  109. MODULE_DESCRIPTION("QNAP Power off driver");
  110. MODULE_LICENSE("GPL v2");