qnap-poweroff.c 3.4 KB

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