Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / soc / tegra / pmc-tegra186.c
blob6f5c6f98ba92a171cc66f7b3820fe45ba717b4b0
1 /*
2 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
14 #define pr_fmt(fmt) "tegra-pmc: " fmt
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
22 #include <asm/system_misc.h>
24 #define PMC_CNTRL 0x000
25 #define PMC_CNTRL_MAIN_RST BIT(4)
27 #define PMC_RST_STATUS 0x070
29 #define WAKE_AOWAKE_CTRL 0x4f4
30 #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
32 #define SCRATCH_SCRATCH0 0x2000
33 #define SCRATCH_SCRATCH0_MODE_RECOVERY BIT(31)
34 #define SCRATCH_SCRATCH0_MODE_BOOTLOADER BIT(30)
35 #define SCRATCH_SCRATCH0_MODE_RCM BIT(1)
36 #define SCRATCH_SCRATCH0_MODE_MASK (SCRATCH_SCRATCH0_MODE_RECOVERY | \
37 SCRATCH_SCRATCH0_MODE_BOOTLOADER | \
38 SCRATCH_SCRATCH0_MODE_RCM)
40 struct tegra_pmc {
41 struct device *dev;
42 void __iomem *regs;
43 void __iomem *wake;
44 void __iomem *aotag;
45 void __iomem *scratch;
47 void (*system_restart)(enum reboot_mode mode, const char *cmd);
48 struct notifier_block restart;
51 static int tegra186_pmc_restart_notify(struct notifier_block *nb,
52 unsigned long action,
53 void *data)
55 struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, restart);
56 const char *cmd = data;
57 u32 value;
59 value = readl(pmc->scratch + SCRATCH_SCRATCH0);
60 value &= ~SCRATCH_SCRATCH0_MODE_MASK;
62 if (cmd) {
63 if (strcmp(cmd, "recovery") == 0)
64 value |= SCRATCH_SCRATCH0_MODE_RECOVERY;
66 if (strcmp(cmd, "bootloader") == 0)
67 value |= SCRATCH_SCRATCH0_MODE_BOOTLOADER;
69 if (strcmp(cmd, "forced-recovery") == 0)
70 value |= SCRATCH_SCRATCH0_MODE_RCM;
73 writel(value, pmc->scratch + SCRATCH_SCRATCH0);
76 * If available, call the system restart implementation that was
77 * registered earlier (typically PSCI).
79 if (pmc->system_restart) {
80 pmc->system_restart(reboot_mode, cmd);
81 return NOTIFY_DONE;
84 /* reset everything but SCRATCH0_SCRATCH0 and PMC_RST_STATUS */
85 value = readl(pmc->regs + PMC_CNTRL);
86 value |= PMC_CNTRL_MAIN_RST;
87 writel(value, pmc->regs + PMC_CNTRL);
89 return NOTIFY_DONE;
92 static int tegra186_pmc_setup(struct tegra_pmc *pmc)
94 struct device_node *np = pmc->dev->of_node;
95 bool invert;
96 u32 value;
98 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
100 value = readl(pmc->wake + WAKE_AOWAKE_CTRL);
102 if (invert)
103 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
104 else
105 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
107 writel(value, pmc->wake + WAKE_AOWAKE_CTRL);
110 * We need to hook any system restart implementation registered
111 * previously so we can write SCRATCH_SCRATCH0 before reset.
113 pmc->system_restart = arm_pm_restart;
114 arm_pm_restart = NULL;
116 pmc->restart.notifier_call = tegra186_pmc_restart_notify;
117 pmc->restart.priority = 128;
119 return register_restart_handler(&pmc->restart);
122 static int tegra186_pmc_probe(struct platform_device *pdev)
124 struct tegra_pmc *pmc;
125 struct resource *res;
127 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
128 if (!pmc)
129 return -ENOMEM;
131 pmc->dev = &pdev->dev;
133 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pmc");
134 pmc->regs = devm_ioremap_resource(&pdev->dev, res);
135 if (IS_ERR(pmc->regs))
136 return PTR_ERR(pmc->regs);
138 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
139 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
140 if (IS_ERR(pmc->wake))
141 return PTR_ERR(pmc->wake);
143 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
144 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
145 if (IS_ERR(pmc->aotag))
146 return PTR_ERR(pmc->aotag);
148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
149 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
150 if (IS_ERR(pmc->scratch))
151 return PTR_ERR(pmc->scratch);
153 return tegra186_pmc_setup(pmc);
156 static const struct of_device_id tegra186_pmc_of_match[] = {
157 { .compatible = "nvidia,tegra186-pmc" },
158 { /* sentinel */ }
160 MODULE_DEVICE_TABLE(of, tegra186_pmc_of_match);
162 static struct platform_driver tegra186_pmc_driver = {
163 .driver = {
164 .name = "tegra186-pmc",
165 .of_match_table = tegra186_pmc_of_match,
167 .probe = tegra186_pmc_probe,
169 builtin_platform_driver(tegra186_pmc_driver);