qnap-poweroff.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * QNAP Turbo NAS Board power off
  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. static void __iomem *base;
  26. static unsigned long tclk;
  27. static void qnap_power_off(void)
  28. {
  29. /* 19200 baud divisor */
  30. const unsigned divisor = ((tclk + (8 * 19200)) / (16 * 19200));
  31. pr_err("%s: triggering power-off...\n", __func__);
  32. /* hijack UART1 and reset into sane state (19200,8n1) */
  33. writel(0x83, UART1_REG(LCR));
  34. writel(divisor & 0xff, UART1_REG(DLL));
  35. writel((divisor >> 8) & 0xff, UART1_REG(DLM));
  36. writel(0x03, UART1_REG(LCR));
  37. writel(0x00, UART1_REG(IER));
  38. writel(0x00, UART1_REG(FCR));
  39. writel(0x00, UART1_REG(MCR));
  40. /* send the power-off command 'A' to PIC */
  41. writel('A', UART1_REG(TX));
  42. }
  43. static int qnap_power_off_probe(struct platform_device *pdev)
  44. {
  45. struct resource *res;
  46. struct clk *clk;
  47. char symname[KSYM_NAME_LEN];
  48. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  49. if (!res) {
  50. dev_err(&pdev->dev, "Missing resource");
  51. return -EINVAL;
  52. }
  53. base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  54. if (!base) {
  55. dev_err(&pdev->dev, "Unable to map resource");
  56. return -EINVAL;
  57. }
  58. /* We need to know tclk in order to calculate the UART divisor */
  59. clk = devm_clk_get(&pdev->dev, NULL);
  60. if (IS_ERR(clk)) {
  61. dev_err(&pdev->dev, "Clk missing");
  62. return PTR_ERR(clk);
  63. }
  64. tclk = clk_get_rate(clk);
  65. /* Check that nothing else has already setup a handler */
  66. if (pm_power_off) {
  67. lookup_symbol_name((ulong)pm_power_off, symname);
  68. dev_err(&pdev->dev,
  69. "pm_power_off already claimed %p %s",
  70. pm_power_off, symname);
  71. return -EBUSY;
  72. }
  73. pm_power_off = qnap_power_off;
  74. return 0;
  75. }
  76. static int qnap_power_off_remove(struct platform_device *pdev)
  77. {
  78. pm_power_off = NULL;
  79. return 0;
  80. }
  81. static const struct of_device_id qnap_power_off_of_match_table[] = {
  82. { .compatible = "qnap,power-off", },
  83. {}
  84. };
  85. MODULE_DEVICE_TABLE(of, qnap_power_off_of_match_table);
  86. static struct platform_driver qnap_power_off_driver = {
  87. .probe = qnap_power_off_probe,
  88. .remove = qnap_power_off_remove,
  89. .driver = {
  90. .owner = THIS_MODULE,
  91. .name = "qnap_power_off",
  92. .of_match_table = of_match_ptr(qnap_power_off_of_match_table),
  93. },
  94. };
  95. module_platform_driver(qnap_power_off_driver);
  96. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  97. MODULE_DESCRIPTION("QNAP Power off driver");
  98. MODULE_LICENSE("GPL v2");