blob: 94db3b6664dfa51f3f7bf2770d286bd4c0f7e685 [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
Prashant Gaikwad89572c72013-01-11 13:16:21 +053019#include <linux/clk/tegra.h>
Thierry Redinga0524ac2014-07-11 09:44:49 +020020#include <linux/cpumask.h>
21#include <linux/cpu_pm.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/suspend.h>
Joseph Lod5529202012-10-31 17:41:21 +080029
Thierry Reding304664e2014-07-11 09:52:41 +020030#include <soc/tegra/fuse.h>
31
Joseph Lod5529202012-10-31 17:41:21 +080032#include <asm/cacheflush.h>
Joseph Lod5529202012-10-31 17:41:21 +080033#include <asm/idmap.h>
34#include <asm/proc-fns.h>
Thierry Redinga0524ac2014-07-11 09:44:49 +020035#include <asm/smp_plat.h>
36#include <asm/suspend.h>
Joseph Lod5529202012-10-31 17:41:21 +080037#include <asm/tlbflush.h>
Joseph Lod457ef352012-10-31 17:41:17 +080038
Joseph Lod5529202012-10-31 17:41:21 +080039#include "flowctrl.h"
Thierry Redinga0524ac2014-07-11 09:44:49 +020040#include "iomap.h"
Joseph Lo0337c3e2013-04-03 19:31:28 +080041#include "pmc.h"
Thierry Redinga0524ac2014-07-11 09:44:49 +020042#include "pm.h"
43#include "reset.h"
Joseph Lod5529202012-10-31 17:41:21 +080044#include "sleep.h"
Joseph Lod5529202012-10-31 17:41:21 +080045
Joseph Lod457ef352012-10-31 17:41:17 +080046#ifdef CONFIG_PM_SLEEP
Joseph Lod457ef352012-10-31 17:41:17 +080047static DEFINE_SPINLOCK(tegra_lp2_lock);
Joseph Lo95872f42013-08-12 17:40:03 +080048static u32 iram_save_size;
49static void *iram_save_addr;
50struct tegra_lp1_iram tegra_lp1_iram;
Joseph Lod5529202012-10-31 17:41:21 +080051void (*tegra_tear_down_cpu)(void);
Joseph Lo95872f42013-08-12 17:40:03 +080052void (*tegra_sleep_core_finish)(unsigned long v2p);
53static int (*tegra_sleep_func)(unsigned long v2p);
Joseph Lod457ef352012-10-31 17:41:17 +080054
Joseph Lobf91add2013-06-04 18:47:33 +080055static void tegra_tear_down_cpu_init(void)
56{
Thierry Reding304664e2014-07-11 09:52:41 +020057 switch (tegra_get_chip_id()) {
Joseph Lobf91add2013-06-04 18:47:33 +080058 case TEGRA20:
59 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
60 tegra_tear_down_cpu = tegra20_tear_down_cpu;
61 break;
62 case TEGRA30:
Joseph Lob573ad92013-07-03 17:50:42 +080063 case TEGRA114:
Joseph Lof0c4ac12013-10-11 17:58:38 +080064 case TEGRA124:
Joseph Lob573ad92013-07-03 17:50:42 +080065 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
Joseph Lof0c4ac12013-10-11 17:58:38 +080066 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
67 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
Joseph Lobf91add2013-06-04 18:47:33 +080068 tegra_tear_down_cpu = tegra30_tear_down_cpu;
69 break;
70 }
71}
72
Joseph Lod5529202012-10-31 17:41:21 +080073/*
74 * restore_cpu_complex
75 *
76 * restores cpu clock setting, clears flow controller
77 *
78 * Always called on CPU 0.
79 */
80static void restore_cpu_complex(void)
81{
82 int cpu = smp_processor_id();
83
84 BUG_ON(cpu != 0);
85
86#ifdef CONFIG_SMP
87 cpu = cpu_logical_map(cpu);
88#endif
89
90 /* Restore the CPU clock settings */
91 tegra_cpu_clock_resume();
92
93 flowctrl_cpu_suspend_exit(cpu);
Joseph Lod5529202012-10-31 17:41:21 +080094}
95
96/*
97 * suspend_cpu_complex
98 *
99 * saves pll state for use by restart_plls, prepares flow controller for
100 * transition to suspend state
101 *
102 * Must always be called on cpu 0.
103 */
104static void suspend_cpu_complex(void)
105{
106 int cpu = smp_processor_id();
107
108 BUG_ON(cpu != 0);
109
110#ifdef CONFIG_SMP
111 cpu = cpu_logical_map(cpu);
112#endif
113
114 /* Save the CPU clock settings */
115 tegra_cpu_clock_suspend();
116
117 flowctrl_cpu_suspend_enter(cpu);
Joseph Lod5529202012-10-31 17:41:21 +0800118}
119
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800120void tegra_clear_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800121{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800122 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800123 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
124
125 spin_lock(&tegra_lp2_lock);
126
127 BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
128 *cpu_in_lp2 &= ~BIT(phy_cpu_id);
129
130 spin_unlock(&tegra_lp2_lock);
131}
132
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800133bool tegra_set_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800134{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800135 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800136 bool last_cpu = false;
137 cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
138 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
139
140 spin_lock(&tegra_lp2_lock);
141
142 BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
143 *cpu_in_lp2 |= BIT(phy_cpu_id);
144
145 if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
146 last_cpu = true;
Thierry Reding304664e2014-07-11 09:52:41 +0200147 else if (tegra_get_chip_id() == TEGRA20 && phy_cpu_id == 1)
Joseph Lo5c1350b2013-01-15 22:10:38 +0000148 tegra20_cpu_set_resettable_soon();
Joseph Lod457ef352012-10-31 17:41:17 +0800149
150 spin_unlock(&tegra_lp2_lock);
151 return last_cpu;
152}
Joseph Lod5529202012-10-31 17:41:21 +0800153
Arnd Bergmann20588422013-04-23 15:36:26 +0200154int tegra_cpu_do_idle(void)
155{
156 return cpu_do_idle();
157}
158
Joseph Lod5529202012-10-31 17:41:21 +0800159static int tegra_sleep_cpu(unsigned long v2p)
160{
Will Deacon6affb482013-03-25 18:19:11 +0000161 setup_mm_for_reboot();
Joseph Lod5529202012-10-31 17:41:21 +0800162 tegra_sleep_cpu_finish(v2p);
163
164 /* should never here */
165 BUG();
166
167 return 0;
168}
169
Joseph Lo4d82d052013-04-02 01:20:50 +0000170void tegra_idle_lp2_last(void)
Joseph Lod5529202012-10-31 17:41:21 +0800171{
Joseph Loc8c2e602013-04-03 19:31:47 +0800172 tegra_pmc_pm_set(TEGRA_SUSPEND_LP2);
Joseph Lod5529202012-10-31 17:41:21 +0800173
174 cpu_cluster_pm_enter();
175 suspend_cpu_complex();
Joseph Lod5529202012-10-31 17:41:21 +0800176
177 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
178
Joseph Lod5529202012-10-31 17:41:21 +0800179 restore_cpu_complex();
180 cpu_cluster_pm_exit();
181}
Joseph Loc8c2e602013-04-03 19:31:47 +0800182
183enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
184 enum tegra_suspend_mode mode)
185{
Joseph Loc8c2e602013-04-03 19:31:47 +0800186 /*
Joseph Lo95872f42013-08-12 17:40:03 +0800187 * The Tegra devices support suspending to LP1 or lower currently.
Joseph Loc8c2e602013-04-03 19:31:47 +0800188 */
Joseph Lo95872f42013-08-12 17:40:03 +0800189 if (mode > TEGRA_SUSPEND_LP1)
190 return TEGRA_SUSPEND_LP1;
Joseph Loc8c2e602013-04-03 19:31:47 +0800191
192 return mode;
193}
194
Joseph Lo95872f42013-08-12 17:40:03 +0800195static int tegra_sleep_core(unsigned long v2p)
196{
197 setup_mm_for_reboot();
198 tegra_sleep_core_finish(v2p);
199
200 /* should never here */
201 BUG();
202
203 return 0;
204}
205
206/*
207 * tegra_lp1_iram_hook
208 *
209 * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
210 * SDRAM. These codes not be copied to IRAM in this fuction. We need to
211 * copy these code to IRAM before LP0/LP1 suspend and restore the content
212 * of IRAM after resume.
213 */
214static bool tegra_lp1_iram_hook(void)
215{
Thierry Reding304664e2014-07-11 09:52:41 +0200216 switch (tegra_get_chip_id()) {
Joseph Lo731a9272013-08-12 17:40:05 +0800217 case TEGRA20:
218 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
219 tegra20_lp1_iram_hook();
220 break;
Joseph Loe7a932b2013-08-12 17:40:04 +0800221 case TEGRA30:
Joseph Loe9f62442013-08-12 17:40:06 +0800222 case TEGRA114:
Joseph Lof0c4ac12013-10-11 17:58:38 +0800223 case TEGRA124:
Joseph Loe9f62442013-08-12 17:40:06 +0800224 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
Joseph Lof0c4ac12013-10-11 17:58:38 +0800225 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
226 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
Joseph Loe7a932b2013-08-12 17:40:04 +0800227 tegra30_lp1_iram_hook();
228 break;
229 default:
230 break;
231 }
232
Joseph Lo95872f42013-08-12 17:40:03 +0800233 if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
234 return false;
235
236 iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
237 iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
238 if (!iram_save_addr)
239 return false;
240
241 return true;
242}
243
244static bool tegra_sleep_core_init(void)
245{
Thierry Reding304664e2014-07-11 09:52:41 +0200246 switch (tegra_get_chip_id()) {
Joseph Lo731a9272013-08-12 17:40:05 +0800247 case TEGRA20:
248 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
249 tegra20_sleep_core_init();
250 break;
Joseph Loe7a932b2013-08-12 17:40:04 +0800251 case TEGRA30:
Joseph Loe9f62442013-08-12 17:40:06 +0800252 case TEGRA114:
Joseph Lof0c4ac12013-10-11 17:58:38 +0800253 case TEGRA124:
Joseph Loe9f62442013-08-12 17:40:06 +0800254 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
Joseph Lof0c4ac12013-10-11 17:58:38 +0800255 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
256 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
Joseph Loe7a932b2013-08-12 17:40:04 +0800257 tegra30_sleep_core_init();
258 break;
259 default:
260 break;
261 }
262
Joseph Lo95872f42013-08-12 17:40:03 +0800263 if (!tegra_sleep_core_finish)
264 return false;
265
266 return true;
267}
268
269static void tegra_suspend_enter_lp1(void)
270{
271 tegra_pmc_suspend();
272
273 /* copy the reset vector & SDRAM shutdown code into IRAM */
Stephen Warrenfddb7702013-08-20 16:19:15 -0600274 memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
Joseph Lo95872f42013-08-12 17:40:03 +0800275 iram_save_size);
Stephen Warrenfddb7702013-08-20 16:19:15 -0600276 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
277 tegra_lp1_iram.start_addr, iram_save_size);
Joseph Lo95872f42013-08-12 17:40:03 +0800278
279 *((u32 *)tegra_cpu_lp1_mask) = 1;
280}
281
282static void tegra_suspend_exit_lp1(void)
283{
284 tegra_pmc_resume();
285
286 /* restore IRAM */
Stephen Warrenfddb7702013-08-20 16:19:15 -0600287 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA), iram_save_addr,
Joseph Lo95872f42013-08-12 17:40:03 +0800288 iram_save_size);
289
290 *(u32 *)tegra_cpu_lp1_mask = 0;
291}
292
Joseph Loc8c2e602013-04-03 19:31:47 +0800293static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
294 [TEGRA_SUSPEND_NONE] = "none",
295 [TEGRA_SUSPEND_LP2] = "LP2",
296 [TEGRA_SUSPEND_LP1] = "LP1",
297 [TEGRA_SUSPEND_LP0] = "LP0",
298};
299
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400300static int tegra_suspend_enter(suspend_state_t state)
Joseph Loc8c2e602013-04-03 19:31:47 +0800301{
302 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
303
304 if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
305 mode >= TEGRA_MAX_SUSPEND_MODE))
306 return -EINVAL;
307
308 pr_info("Entering suspend state %s\n", lp_state[mode]);
309
310 tegra_pmc_pm_set(mode);
311
312 local_fiq_disable();
313
314 suspend_cpu_complex();
315 switch (mode) {
Joseph Lo95872f42013-08-12 17:40:03 +0800316 case TEGRA_SUSPEND_LP1:
317 tegra_suspend_enter_lp1();
318 break;
Joseph Loc8c2e602013-04-03 19:31:47 +0800319 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800320 tegra_set_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800321 break;
322 default:
323 break;
324 }
325
Joseph Lo95872f42013-08-12 17:40:03 +0800326 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
Joseph Loc8c2e602013-04-03 19:31:47 +0800327
328 switch (mode) {
Joseph Lo95872f42013-08-12 17:40:03 +0800329 case TEGRA_SUSPEND_LP1:
330 tegra_suspend_exit_lp1();
331 break;
Joseph Loc8c2e602013-04-03 19:31:47 +0800332 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800333 tegra_clear_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800334 break;
335 default:
336 break;
337 }
338 restore_cpu_complex();
339
340 local_fiq_enable();
341
342 return 0;
343}
344
345static const struct platform_suspend_ops tegra_suspend_ops = {
346 .valid = suspend_valid_only_mem,
347 .enter = tegra_suspend_enter,
348};
349
350void __init tegra_init_suspend(void)
351{
Joseph Lo95872f42013-08-12 17:40:03 +0800352 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
353
354 if (mode == TEGRA_SUSPEND_NONE)
Joseph Loc8c2e602013-04-03 19:31:47 +0800355 return;
356
Joseph Lobf91add2013-06-04 18:47:33 +0800357 tegra_tear_down_cpu_init();
Joseph Loc8c2e602013-04-03 19:31:47 +0800358 tegra_pmc_suspend_init();
359
Joseph Lo95872f42013-08-12 17:40:03 +0800360 if (mode >= TEGRA_SUSPEND_LP1) {
361 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
362 pr_err("%s: unable to allocate memory for SDRAM"
363 "self-refresh -- LP0/LP1 unavailable\n",
364 __func__);
365 tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
366 mode = TEGRA_SUSPEND_LP2;
367 }
368 }
369
370 /* set up sleep function for cpu_suspend */
371 switch (mode) {
372 case TEGRA_SUSPEND_LP1:
373 tegra_sleep_func = tegra_sleep_core;
374 break;
375 case TEGRA_SUSPEND_LP2:
376 tegra_sleep_func = tegra_sleep_cpu;
377 break;
378 default:
379 break;
380 }
381
Joseph Loc8c2e602013-04-03 19:31:47 +0800382 suspend_set_ops(&tegra_suspend_ops);
383}
Joseph Lod457ef352012-10-31 17:41:17 +0800384#endif