qnap-poweroff.c 3.2 KB

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