Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
219 lines (216 sloc) 6.68 KB
diff -r -u -N linux-4.8.12.org/drivers/power/reset/Kconfig linux-4.8.12/drivers/power/reset/Kconfig
--- linux-4.8.12.org/drivers/power/reset/Kconfig 2016-12-02 09:11:45.000000000 +0100
+++ linux-4.8.12/drivers/power/reset/Kconfig 2016-12-16 19:37:39.787988518 +0100
@@ -120,6 +120,15 @@
Say Y if you have a QNAP NAS.
+config POWER_RESET_WDEX2U
+ bool "WDMC EX2 ultra power-off driver"
+ depends on OF_GPIO && PLAT_ORION
+ help
+ This driver supports turning off WD MyCloud EX2 ultra NAS devices by
+ sending commands to the microcontroller which controls the main power.
+
+ Say Y if you have a WD MyCloud EX2 ultra
+
config POWER_RESET_RESTART
bool "Restart power-off driver"
help
diff -r -u -N linux-4.8.12.org/drivers/power/reset/Makefile linux-4.8.12/drivers/power/reset/Makefile
--- linux-4.8.12.org/drivers/power/reset/Makefile 2016-12-02 09:11:45.000000000 +0100
+++ linux-4.8.12/drivers/power/reset/Makefile 2016-12-16 19:38:06.395973742 +0100
@@ -12,6 +12,7 @@
obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
obj-$(CONFIG_POWER_RESET_LTC2952) += ltc2952-poweroff.o
obj-$(CONFIG_POWER_RESET_QNAP) += qnap-poweroff.o
+obj-$(CONFIG_POWER_RESET_WDEX2U) += wt-poweroff.o
obj-$(CONFIG_POWER_RESET_RESTART) += restart-poweroff.o
obj-$(CONFIG_POWER_RESET_ST) += st-poweroff.o
obj-$(CONFIG_POWER_RESET_VERSATILE) += arm-versatile-reboot.o
diff -r -u -N linux-4.8.12.org/drivers/power/reset/wt-poweroff.c linux-4.8.12/drivers/power/reset/wt-poweroff.c
--- linux-4.8.12.org/drivers/power/reset/wt-poweroff.c 1970-01-01 01:00:00.000000000 +0100
+++ linux-4.8.12/drivers/power/reset/wt-poweroff.c 2016-12-17 17:21:42.423378737 +0100
@@ -0,0 +1,184 @@
+/*
+ * Poweroff and restart driver for the Western Digital
+ * MyCloud EX2 Ultra NAS. It could be easily reused for
+ * other devices with a serial connected MCU.
+ *
+ * Copyright (C) 2016 Martin Mueller <mm@sig21.net>
+ *
+ * Based on the code from:
+ *
+ * Copyright (C) 2012 Andrew Lunn <andrew@lunn.ch>
+ * Copyright (C) 2009 Martin Michlmayr <tbm@cyrius.com>
+ * Copyright (C) 2008 Byron Bradley <byron.bbradley@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/serial_reg.h>
+#include <linux/kallsyms.h>
+#include <linux/of.h>
+#include <linux/io.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <asm/system_misc.h>
+
+#define UART_REG(x) (base + ((UART_##x) << 2))
+
+struct power_mcu_cmds {
+ u32 baud;
+ char *poweroff;
+ char *restart;
+};
+
+static const struct power_mcu_cmds wdmcex2u_power_mcu_cmds = {
+ .baud = 19200,
+ .poweroff = "\xfa\x03\x03\x01\x01\x01\xfb",
+ .restart = "\xfa\x03\x03\x02\x01\x0a\xfb",
+};
+
+static const struct of_device_id wt_power_off_of_match_table[] = {
+ { .compatible = "wdmcex2u,power-off",
+ .data = &wdmcex2u_power_mcu_cmds,
+ },
+ {}
+};
+MODULE_DEVICE_TABLE(of, wt_power_off_of_match_table);
+
+static void __iomem *base;
+static unsigned long tclk;
+static const struct power_mcu_cmds *cfg;
+
+static void *pm_power_off_org;
+static void *arm_pm_restart_org;
+
+static void wt_power_off(void)
+{
+ const unsigned divisor = ((tclk + (8 * cfg->baud)) / (16 * cfg->baud));
+ uint8_t i;
+
+ pr_err("%s: triggering power-off...\n", __func__);
+
+ /* hijack UART and reset into sane state */
+ writel(0x83, UART_REG(LCR));
+ writel(divisor & 0xff, UART_REG(DLL));
+ writel((divisor >> 8) & 0xff, UART_REG(DLM));
+ writel(0x03, UART_REG(LCR));
+ writel(0x00, UART_REG(IER));
+ writel(0x00, UART_REG(FCR));
+ writel(0x00, UART_REG(MCR));
+
+ /* send the power-off command to PIC */
+ for (i=0; i<strlen(cfg->poweroff); i++) {
+ writel(cfg->poweroff[i], UART_REG(TX));
+ mdelay(2);
+ }
+ mdelay(1000);
+ pr_err("%s: triggering power-off failed!\n", __func__);
+}
+
+static void wt_restart(enum reboot_mode reboot_mode, const char *cmd)
+{
+ const unsigned divisor = ((tclk + (8 * cfg->baud)) / (16 * cfg->baud));
+ uint8_t i;
+
+ pr_err("%s: triggering restart...\n", __func__);
+
+ /* hijack UART and reset into sane state */
+ writel(0x83, UART_REG(LCR));
+ writel(divisor & 0xff, UART_REG(DLL));
+ writel((divisor >> 8) & 0xff, UART_REG(DLM));
+ writel(0x03, UART_REG(LCR));
+ writel(0x00, UART_REG(IER));
+ writel(0x00, UART_REG(FCR));
+ writel(0x00, UART_REG(MCR));
+
+ /* send the restart command to PIC */
+ for (i=0; i<strlen(cfg->restart); i++) {
+ writel(cfg->restart[i], UART_REG(TX));
+ mdelay(2);
+ }
+ mdelay(1000);
+ pr_err("%s: triggering restart failed!\n", __func__);
+}
+
+static int wt_power_off_probe(struct platform_device *pdev)
+{
+ struct device_node *np = pdev->dev.of_node;
+ struct resource *res;
+ struct clk *clk;
+ char symname[KSYM_NAME_LEN];
+
+ const struct of_device_id *match =
+ of_match_node(wt_power_off_of_match_table, np);
+ cfg = match->data;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res) {
+ dev_err(&pdev->dev, "Missing resource");
+ return -EINVAL;
+ }
+
+ base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
+ if (!base) {
+ dev_err(&pdev->dev, "Unable to map resource");
+ return -EINVAL;
+ }
+
+ /* We need to know tclk in order to calculate the UART divisor */
+ clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(clk)) {
+ dev_err(&pdev->dev, "Clk missing");
+ return PTR_ERR(clk);
+ }
+
+ tclk = clk_get_rate(clk);
+
+ pm_power_off_org = pm_power_off;
+ if ( strlen(cfg->poweroff) ) {
+ if (pm_power_off) {
+ lookup_symbol_name((ulong)pm_power_off, symname);
+ dev_warn(&pdev->dev, "replacing pm_power_off (%s)", symname);
+ } else
+ dev_notice(&pdev->dev, "installing pm_power_off handler");
+ pm_power_off = wt_power_off;
+ }
+
+ arm_pm_restart_org = arm_pm_restart;
+ if ( strlen(cfg->restart) ) {
+ if (arm_pm_restart) {
+ lookup_symbol_name((ulong)arm_pm_restart, symname);
+ dev_warn(&pdev->dev, "replacing arm_pm_restart (%s)", symname);
+ } else
+ dev_notice(&pdev->dev, "installing arm_pm_restart handler");
+ arm_pm_restart = wt_restart;
+ }
+
+ return 0;
+}
+
+static int wt_power_off_remove(struct platform_device *pdev)
+{
+ pm_power_off = pm_power_off_org;
+ arm_pm_restart = arm_pm_restart_org;
+ return 0;
+}
+
+static struct platform_driver wt_power_off_driver = {
+ .probe = wt_power_off_probe,
+ .remove = wt_power_off_remove,
+ .driver = {
+ .name = "wt_power_off",
+ .of_match_table = of_match_ptr(wt_power_off_of_match_table),
+ },
+};
+module_platform_driver(wt_power_off_driver);
+
+MODULE_AUTHOR("Martin Mueller <mm@sig21.net>");
+MODULE_DESCRIPTION("Welltrend MCU power off driver");
+MODULE_LICENSE("GPL v2");
You can’t perform that action at this time.