summaryrefslogtreecommitdiff
path: root/drivers/soc/tegra/pmc-tegra186.c
blob: 6f5c6f98ba92a171cc66f7b3820fe45ba717b4b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#define pr_fmt(fmt) "tegra-pmc: " fmt

#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/reboot.h>

#include <asm/system_misc.h>

#define PMC_CNTRL 0x000
#define  PMC_CNTRL_MAIN_RST BIT(4)

#define PMC_RST_STATUS 0x070

#define WAKE_AOWAKE_CTRL 0x4f4
#define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)

#define SCRATCH_SCRATCH0 0x2000
#define  SCRATCH_SCRATCH0_MODE_RECOVERY BIT(31)
#define  SCRATCH_SCRATCH0_MODE_BOOTLOADER BIT(30)
#define  SCRATCH_SCRATCH0_MODE_RCM BIT(1)
#define  SCRATCH_SCRATCH0_MODE_MASK (SCRATCH_SCRATCH0_MODE_RECOVERY | \
				     SCRATCH_SCRATCH0_MODE_BOOTLOADER | \
				     SCRATCH_SCRATCH0_MODE_RCM)

struct tegra_pmc {
	struct device *dev;
	void __iomem *regs;
	void __iomem *wake;
	void __iomem *aotag;
	void __iomem *scratch;

	void (*system_restart)(enum reboot_mode mode, const char *cmd);
	struct notifier_block restart;
};

static int tegra186_pmc_restart_notify(struct notifier_block *nb,
				       unsigned long action,
				       void *data)
{
	struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, restart);
	const char *cmd = data;
	u32 value;

	value = readl(pmc->scratch + SCRATCH_SCRATCH0);
	value &= ~SCRATCH_SCRATCH0_MODE_MASK;

	if (cmd) {
		if (strcmp(cmd, "recovery") == 0)
			value |= SCRATCH_SCRATCH0_MODE_RECOVERY;

		if (strcmp(cmd, "bootloader") == 0)
			value |= SCRATCH_SCRATCH0_MODE_BOOTLOADER;

		if (strcmp(cmd, "forced-recovery") == 0)
			value |= SCRATCH_SCRATCH0_MODE_RCM;
	}

	writel(value, pmc->scratch + SCRATCH_SCRATCH0);

	/*
	 * If available, call the system restart implementation that was
	 * registered earlier (typically PSCI).
	 */
	if (pmc->system_restart) {
		pmc->system_restart(reboot_mode, cmd);
		return NOTIFY_DONE;
	}

	/* reset everything but SCRATCH0_SCRATCH0 and PMC_RST_STATUS */
	value = readl(pmc->regs + PMC_CNTRL);
	value |= PMC_CNTRL_MAIN_RST;
	writel(value, pmc->regs + PMC_CNTRL);

	return NOTIFY_DONE;
}

static int tegra186_pmc_setup(struct tegra_pmc *pmc)
{
	struct device_node *np = pmc->dev->of_node;
	bool invert;
	u32 value;

	invert = of_property_read_bool(np, "nvidia,invert-interrupt");

	value = readl(pmc->wake + WAKE_AOWAKE_CTRL);

	if (invert)
		value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
	else
		value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;

	writel(value, pmc->wake + WAKE_AOWAKE_CTRL);

	/*
	 * We need to hook any system restart implementation registered
	 * previously so we can write SCRATCH_SCRATCH0 before reset.
	 */
	pmc->system_restart = arm_pm_restart;
	arm_pm_restart = NULL;

	pmc->restart.notifier_call = tegra186_pmc_restart_notify;
	pmc->restart.priority = 128;

	return register_restart_handler(&pmc->restart);
}

static int tegra186_pmc_probe(struct platform_device *pdev)
{
	struct tegra_pmc *pmc;
	struct resource *res;

	pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
	if (!pmc)
		return -ENOMEM;

	pmc->dev = &pdev->dev;

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pmc");
	pmc->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pmc->regs))
		return PTR_ERR(pmc->regs);

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
	pmc->wake = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pmc->wake))
		return PTR_ERR(pmc->wake);

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
	pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pmc->aotag))
		return PTR_ERR(pmc->aotag);

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
	pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pmc->scratch))
		return PTR_ERR(pmc->scratch);

	return tegra186_pmc_setup(pmc);
}

static const struct of_device_id tegra186_pmc_of_match[] = {
	{ .compatible = "nvidia,tegra186-pmc" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, tegra186_pmc_of_match);

static struct platform_driver tegra186_pmc_driver = {
	.driver = {
		.name = "tegra186-pmc",
		.of_match_table = tegra186_pmc_of_match,
	},
	.probe = tegra186_pmc_probe,
};
builtin_platform_driver(tegra186_pmc_driver);