blob: 52b7dc61d870a7eb3605ebdcaed1183c1098dcbb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Lunne8fc7212012-12-28 13:25:09 +01002/*
Andrew Lunn200c0a32014-02-20 06:02:35 +10003 * QNAP Turbo NAS Board power off. Can also be used on Synology devices.
Andrew Lunne8fc7212012-12-28 13:25:09 +01004 *
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>
Andrew Lunne8fc7212012-12-28 13:25:09 +010011 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/serial_reg.h>
17#include <linux/kallsyms.h>
18#include <linux/of.h>
19#include <linux/io.h>
20#include <linux/clk.h>
21
22#define UART1_REG(x) (base + ((UART_##x) << 2))
23
Andrew Lunn200c0a32014-02-20 06:02:35 +100024struct power_off_cfg {
25 u32 baud;
26 char cmd;
27};
28
29static const struct power_off_cfg qnap_power_off_cfg = {
30 .baud = 19200,
31 .cmd = 'A',
32};
33
34static const struct power_off_cfg synology_power_off_cfg = {
35 .baud = 9600,
36 .cmd = '1',
37};
38
39static const struct of_device_id qnap_power_off_of_match_table[] = {
40 { .compatible = "qnap,power-off",
41 .data = &qnap_power_off_cfg,
42 },
43 { .compatible = "synology,power-off",
44 .data = &synology_power_off_cfg,
45 },
46 {}
47};
48MODULE_DEVICE_TABLE(of, qnap_power_off_of_match_table);
49
Andrew Lunne8fc7212012-12-28 13:25:09 +010050static void __iomem *base;
51static unsigned long tclk;
Andrew Lunn200c0a32014-02-20 06:02:35 +100052static const struct power_off_cfg *cfg;
Andrew Lunne8fc7212012-12-28 13:25:09 +010053
54static void qnap_power_off(void)
55{
Andrew Lunn200c0a32014-02-20 06:02:35 +100056 const unsigned divisor = ((tclk + (8 * cfg->baud)) / (16 * cfg->baud));
Andrew Lunne8fc7212012-12-28 13:25:09 +010057
58 pr_err("%s: triggering power-off...\n", __func__);
59
Andrew Lunn200c0a32014-02-20 06:02:35 +100060 /* hijack UART1 and reset into sane state */
Andrew Lunne8fc7212012-12-28 13:25:09 +010061 writel(0x83, UART1_REG(LCR));
62 writel(divisor & 0xff, UART1_REG(DLL));
63 writel((divisor >> 8) & 0xff, UART1_REG(DLM));
64 writel(0x03, UART1_REG(LCR));
65 writel(0x00, UART1_REG(IER));
66 writel(0x00, UART1_REG(FCR));
67 writel(0x00, UART1_REG(MCR));
68
Andrew Lunn200c0a32014-02-20 06:02:35 +100069 /* send the power-off command to PIC */
70 writel(cfg->cmd, UART1_REG(TX));
Andrew Lunne8fc7212012-12-28 13:25:09 +010071}
72
73static int qnap_power_off_probe(struct platform_device *pdev)
74{
Andrew Lunn200c0a32014-02-20 06:02:35 +100075 struct device_node *np = pdev->dev.of_node;
Andrew Lunne8fc7212012-12-28 13:25:09 +010076 struct resource *res;
77 struct clk *clk;
78 char symname[KSYM_NAME_LEN];
79
Andrew Lunn200c0a32014-02-20 06:02:35 +100080 const struct of_device_id *match =
81 of_match_node(qnap_power_off_of_match_table, np);
82 cfg = match->data;
83
Andrew Lunne8fc7212012-12-28 13:25:09 +010084 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 if (!res) {
86 dev_err(&pdev->dev, "Missing resource");
87 return -EINVAL;
88 }
89
90 base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
91 if (!base) {
92 dev_err(&pdev->dev, "Unable to map resource");
93 return -EINVAL;
94 }
95
96 /* We need to know tclk in order to calculate the UART divisor */
97 clk = devm_clk_get(&pdev->dev, NULL);
98 if (IS_ERR(clk)) {
99 dev_err(&pdev->dev, "Clk missing");
100 return PTR_ERR(clk);
101 }
102
103 tclk = clk_get_rate(clk);
104
105 /* Check that nothing else has already setup a handler */
106 if (pm_power_off) {
107 lookup_symbol_name((ulong)pm_power_off, symname);
108 dev_err(&pdev->dev,
109 "pm_power_off already claimed %p %s",
110 pm_power_off, symname);
111 return -EBUSY;
112 }
113 pm_power_off = qnap_power_off;
114
115 return 0;
116}
117
118static int qnap_power_off_remove(struct platform_device *pdev)
119{
120 pm_power_off = NULL;
121 return 0;
122}
123
Andrew Lunne8fc7212012-12-28 13:25:09 +0100124static struct platform_driver qnap_power_off_driver = {
125 .probe = qnap_power_off_probe,
126 .remove = qnap_power_off_remove,
127 .driver = {
Andrew Lunne8fc7212012-12-28 13:25:09 +0100128 .name = "qnap_power_off",
129 .of_match_table = of_match_ptr(qnap_power_off_of_match_table),
130 },
131};
132module_platform_driver(qnap_power_off_driver);
133
134MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
135MODULE_DESCRIPTION("QNAP Power off driver");
Andrew Lunn8fd526f2013-01-08 19:15:26 +0100136MODULE_LICENSE("GPL v2");