blob: 19cf360c7bdde59721c9be574c0f27f6120bfca2 [file] [log] [blame]
Joseph Lod457ef352012-10-31 17:41:17 +08001/*
2 * CPU complex suspend & resume functions for Tegra SoCs
3 *
4 * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/kernel.h>
20#include <linux/spinlock.h>
21#include <linux/io.h>
22#include <linux/cpumask.h>
Joseph Lod5529202012-10-31 17:41:21 +080023#include <linux/delay.h>
24#include <linux/cpu_pm.h>
Joseph Loc8c2e602013-04-03 19:31:47 +080025#include <linux/suspend.h>
Joseph Lod5529202012-10-31 17:41:21 +080026#include <linux/err.h>
Prashant Gaikwad89572c72013-01-11 13:16:21 +053027#include <linux/clk/tegra.h>
Joseph Lod5529202012-10-31 17:41:21 +080028
29#include <asm/smp_plat.h>
30#include <asm/cacheflush.h>
31#include <asm/suspend.h>
32#include <asm/idmap.h>
33#include <asm/proc-fns.h>
34#include <asm/tlbflush.h>
Joseph Lod457ef352012-10-31 17:41:17 +080035
36#include "iomap.h"
37#include "reset.h"
Joseph Lod5529202012-10-31 17:41:21 +080038#include "flowctrl.h"
Joseph Lo5c1350b2013-01-15 22:10:38 +000039#include "fuse.h"
Joseph Lo0337c3e2013-04-03 19:31:28 +080040#include "pmc.h"
Joseph Lod5529202012-10-31 17:41:21 +080041#include "sleep.h"
Joseph Lod5529202012-10-31 17:41:21 +080042
Joseph Lod457ef352012-10-31 17:41:17 +080043#ifdef CONFIG_PM_SLEEP
Joseph Lod457ef352012-10-31 17:41:17 +080044static DEFINE_SPINLOCK(tegra_lp2_lock);
Joseph Lod5529202012-10-31 17:41:21 +080045void (*tegra_tear_down_cpu)(void);
Joseph Lod457ef352012-10-31 17:41:17 +080046
Joseph Lobf91add2013-06-04 18:47:33 +080047static void tegra_tear_down_cpu_init(void)
48{
49 switch (tegra_chip_id) {
50 case TEGRA20:
51 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
52 tegra_tear_down_cpu = tegra20_tear_down_cpu;
53 break;
54 case TEGRA30:
Joseph Lob573ad92013-07-03 17:50:42 +080055 case TEGRA114:
56 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
57 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC))
Joseph Lobf91add2013-06-04 18:47:33 +080058 tegra_tear_down_cpu = tegra30_tear_down_cpu;
59 break;
60 }
61}
62
Joseph Lod5529202012-10-31 17:41:21 +080063/*
64 * restore_cpu_complex
65 *
66 * restores cpu clock setting, clears flow controller
67 *
68 * Always called on CPU 0.
69 */
70static void restore_cpu_complex(void)
71{
72 int cpu = smp_processor_id();
73
74 BUG_ON(cpu != 0);
75
76#ifdef CONFIG_SMP
77 cpu = cpu_logical_map(cpu);
78#endif
79
80 /* Restore the CPU clock settings */
81 tegra_cpu_clock_resume();
82
83 flowctrl_cpu_suspend_exit(cpu);
Joseph Lod5529202012-10-31 17:41:21 +080084}
85
86/*
87 * suspend_cpu_complex
88 *
89 * saves pll state for use by restart_plls, prepares flow controller for
90 * transition to suspend state
91 *
92 * Must always be called on cpu 0.
93 */
94static void suspend_cpu_complex(void)
95{
96 int cpu = smp_processor_id();
97
98 BUG_ON(cpu != 0);
99
100#ifdef CONFIG_SMP
101 cpu = cpu_logical_map(cpu);
102#endif
103
104 /* Save the CPU clock settings */
105 tegra_cpu_clock_suspend();
106
107 flowctrl_cpu_suspend_enter(cpu);
Joseph Lod5529202012-10-31 17:41:21 +0800108}
109
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800110void tegra_clear_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800111{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800112 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800113 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
114
115 spin_lock(&tegra_lp2_lock);
116
117 BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
118 *cpu_in_lp2 &= ~BIT(phy_cpu_id);
119
120 spin_unlock(&tegra_lp2_lock);
121}
122
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800123bool tegra_set_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800124{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800125 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800126 bool last_cpu = false;
127 cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
128 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
129
130 spin_lock(&tegra_lp2_lock);
131
132 BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
133 *cpu_in_lp2 |= BIT(phy_cpu_id);
134
135 if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
136 last_cpu = true;
Joseph Lo5c1350b2013-01-15 22:10:38 +0000137 else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1)
138 tegra20_cpu_set_resettable_soon();
Joseph Lod457ef352012-10-31 17:41:17 +0800139
140 spin_unlock(&tegra_lp2_lock);
141 return last_cpu;
142}
Joseph Lod5529202012-10-31 17:41:21 +0800143
Arnd Bergmann20588422013-04-23 15:36:26 +0200144int tegra_cpu_do_idle(void)
145{
146 return cpu_do_idle();
147}
148
Joseph Lod5529202012-10-31 17:41:21 +0800149static int tegra_sleep_cpu(unsigned long v2p)
150{
Will Deacon6affb482013-03-25 18:19:11 +0000151 setup_mm_for_reboot();
Joseph Lod5529202012-10-31 17:41:21 +0800152 tegra_sleep_cpu_finish(v2p);
153
154 /* should never here */
155 BUG();
156
157 return 0;
158}
159
Joseph Lo4d82d052013-04-02 01:20:50 +0000160void tegra_idle_lp2_last(void)
Joseph Lod5529202012-10-31 17:41:21 +0800161{
Joseph Loc8c2e602013-04-03 19:31:47 +0800162 tegra_pmc_pm_set(TEGRA_SUSPEND_LP2);
Joseph Lod5529202012-10-31 17:41:21 +0800163
164 cpu_cluster_pm_enter();
165 suspend_cpu_complex();
Joseph Lod5529202012-10-31 17:41:21 +0800166
167 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
168
Joseph Lod5529202012-10-31 17:41:21 +0800169 restore_cpu_complex();
170 cpu_cluster_pm_exit();
171}
Joseph Loc8c2e602013-04-03 19:31:47 +0800172
173enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
174 enum tegra_suspend_mode mode)
175{
176 /* Tegra114 didn't support any suspending mode yet. */
177 if (tegra_chip_id == TEGRA114)
178 return TEGRA_SUSPEND_NONE;
179
180 /*
181 * The Tegra devices only support suspending to LP2 currently.
182 */
183 if (mode > TEGRA_SUSPEND_LP2)
184 return TEGRA_SUSPEND_LP2;
185
186 return mode;
187}
188
189static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
190 [TEGRA_SUSPEND_NONE] = "none",
191 [TEGRA_SUSPEND_LP2] = "LP2",
192 [TEGRA_SUSPEND_LP1] = "LP1",
193 [TEGRA_SUSPEND_LP0] = "LP0",
194};
195
196static int __cpuinit tegra_suspend_enter(suspend_state_t state)
197{
198 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
199
200 if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
201 mode >= TEGRA_MAX_SUSPEND_MODE))
202 return -EINVAL;
203
204 pr_info("Entering suspend state %s\n", lp_state[mode]);
205
206 tegra_pmc_pm_set(mode);
207
208 local_fiq_disable();
209
210 suspend_cpu_complex();
211 switch (mode) {
212 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800213 tegra_set_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800214 break;
215 default:
216 break;
217 }
218
219 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
220
221 switch (mode) {
222 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800223 tegra_clear_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800224 break;
225 default:
226 break;
227 }
228 restore_cpu_complex();
229
230 local_fiq_enable();
231
232 return 0;
233}
234
235static const struct platform_suspend_ops tegra_suspend_ops = {
236 .valid = suspend_valid_only_mem,
237 .enter = tegra_suspend_enter,
238};
239
240void __init tegra_init_suspend(void)
241{
242 if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NONE)
243 return;
244
Joseph Lobf91add2013-06-04 18:47:33 +0800245 tegra_tear_down_cpu_init();
Joseph Loc8c2e602013-04-03 19:31:47 +0800246 tegra_pmc_suspend_init();
247
248 suspend_set_ops(&tegra_suspend_ops);
249}
Joseph Lod457ef352012-10-31 17:41:17 +0800250#endif