summaryrefslogtreecommitdiffstats
path: root/drivers/platform/tegra/pm.c
blob: 7c2b6f6af6b0a3738b101d837351a9a65efe7ceb (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * drivers/platform/tegra/pm.c
 *
 * CPU complex suspend & resume functions for Tegra SoCs
 *
 * Copyright (c) 2009-2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/io.h>
#include <linux/syscore_ops.h>
#include <linux/suspend.h>
#include <linux/notifier.h>
#include <linux/serial_reg.h>
#include <linux/tegra-pm.h>
#include <linux/of_address.h>
#include <linux/ioport.h>

struct tegra_pm_context {
	u8	uart[5];
};

static RAW_NOTIFIER_HEAD(tegra_pm_chain_head);

static u64 resume_time;
static u64 resume_entry_time;
static u64 suspend_time;
static u64 suspend_entry_time;

static int debug_uart_disabled;

void tegra_log_suspend_entry_time(void)
{
        suspend_entry_time = arch_timer_read_counter();
}
EXPORT_SYMBOL(tegra_log_suspend_entry_time);

void tegra_log_resume_time(void)
{
	u32 timer_rate = arch_timer_get_rate()/1000;

        resume_time = arch_timer_read_counter() - resume_entry_time;
	resume_time = resume_time / timer_rate;
}
EXPORT_SYMBOL(tegra_log_resume_time);

static void tegra_log_suspend_time(void)
{
	u32 timer_rate = arch_timer_get_rate()/1000;

        suspend_time = arch_timer_read_counter() - suspend_entry_time;
	suspend_time = suspend_time / timer_rate;
}

int tegra_register_pm_notifier(struct notifier_block *nb)
{
	return raw_notifier_chain_register(&tegra_pm_chain_head, nb);
}
EXPORT_SYMBOL(tegra_register_pm_notifier);

int tegra_unregister_pm_notifier(struct notifier_block *nb)
{
	return raw_notifier_chain_unregister(&tegra_pm_chain_head, nb);
}
EXPORT_SYMBOL(tegra_unregister_pm_notifier);

int tegra_pm_notifier_call_chain(unsigned int val)
{
	int ret = raw_notifier_call_chain(&tegra_pm_chain_head, val, NULL);

	return notifier_to_errno(ret);
}

static struct tegra_pm_context suspend_ctx;

static void __iomem *debug_uart_port_base;

static inline unsigned long tegra_uart_read(unsigned long reg)
{
	return readl(debug_uart_port_base + (reg << 2));
}

static inline void tegra_uart_write(unsigned val,
	      unsigned long reg)
{
	writel(val, debug_uart_port_base + (reg << 2));
}

static int tegra_debug_uart_suspend(void)
{
	u32 lcr;
	tegra_log_suspend_time();

	pr_info("Entered SC7\n");

	if (!debug_uart_disabled) {
		lcr = tegra_uart_read(UART_LCR);

		suspend_ctx.uart[0] = lcr;
		suspend_ctx.uart[1] = tegra_uart_read(UART_MCR);

		/* DLAB = 0 */
		tegra_uart_write(lcr & ~UART_LCR_DLAB, UART_LCR);

		suspend_ctx.uart[2] = tegra_uart_read(UART_IER);

		/* DLAB = 1 */
		tegra_uart_write(lcr | UART_LCR_DLAB, UART_LCR);

		suspend_ctx.uart[3] = tegra_uart_read(UART_DLL);
		suspend_ctx.uart[4] = tegra_uart_read(UART_DLM);

		tegra_uart_write(lcr, UART_LCR);
	}

	return 0;
}

static void tegra_debug_uart_resume(void)
{
	u32 lcr;

	resume_entry_time = arch_timer_read_counter();
	lcr = suspend_ctx.uart[0];

	if (!debug_uart_disabled) {
		tegra_uart_write(suspend_ctx.uart[1], UART_MCR);

		/* DLAB = 0 */
		tegra_uart_write(lcr & ~UART_LCR_DLAB, UART_LCR);

		tegra_uart_write(UART_FCR_ENABLE_FIFO | UART_FCR_T_TRIG_01 |
				UART_FCR_R_TRIG_01, UART_FCR);

		tegra_uart_write(suspend_ctx.uart[2], UART_IER);

		/* DLAB = 1 */
		tegra_uart_write(lcr | UART_LCR_DLAB, UART_LCR);

		tegra_uart_write(suspend_ctx.uart[3], UART_DLL);
		tegra_uart_write(suspend_ctx.uart[4], UART_DLM);

		tegra_uart_write(lcr, UART_LCR);
	}

	pr_info("Exited SC7\n");
}

static struct syscore_ops tegra_debug_uart_syscore_ops = {
	.suspend = tegra_debug_uart_suspend,
	.resume = tegra_debug_uart_resume,
	.save = tegra_debug_uart_suspend,
	.restore = tegra_debug_uart_resume,
};

static int tegra_debug_uart_syscore_init(void)
{
	const char *property;
	struct device_node *node;
	struct resource r;

	node = of_find_node_by_name(NULL, "combined-uart");
	if (node && of_device_is_available(node)) {
		debug_uart_disabled = 1;
		goto register_ops;
	}

	property = of_get_property(of_chosen, "stdout-path", NULL);
	if (!property) {
		pr_info("%s: stdout-path property missing\n", __func__);
		goto out;
	}

	node = of_find_node_by_path(property);
	if (!node) {
		pr_err("%s: failed to get node of stdout-path\n", __func__);
		goto out;
	}

	if (of_address_to_resource(node, 0, &r)) {
		pr_err("%s: failed to get resource of stdout-path\n", __func__);
		goto out;
	}

	debug_uart_port_base = ioremap(r.start, resource_size(&r));
	if (!debug_uart_port_base) {
		pr_err("%s: failed to remap debug_uart_port_base\n", __func__);
		goto out;
	}

register_ops:
	register_syscore_ops(&tegra_debug_uart_syscore_ops);

out:
	return 0;
}
arch_initcall(tegra_debug_uart_syscore_init);

static ssize_t resume_time_show(struct kobject *kobj,
				struct kobj_attribute *attr,
				char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%llums\n", resume_time);
}

static struct kobj_attribute resume_time_attribute =
	__ATTR(resume_time, 0444, resume_time_show, NULL);

static ssize_t suspend_time_show(struct kobject *kobj,
				struct kobj_attribute *attr,
				char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%llums\n", suspend_time);
}

static struct kobj_attribute suspend_time_attribute =
	__ATTR(suspend_time, 0444, suspend_time_show, NULL);

static struct kobject *suspend_kobj;

static int __init suspend_resume_time_init(void)
{
	suspend_kobj = kobject_create_and_add("suspend", power_kobj);
	if (suspend_kobj) {
		if (sysfs_create_file(suspend_kobj,
					&resume_time_attribute.attr))
			pr_err("%s: sysfs_create_file resume_time failed!\n",
								__func__);
		if (sysfs_create_file(suspend_kobj,
					&suspend_time_attribute.attr))
			pr_err("%s: sysfs_create_file suspend_time failed!\n",
								__func__);
	}

	return 0;
}
late_initcall(suspend_resume_time_init);