2 * CPU complex suspend & resume functions for Tegra SoCs
4 * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
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.
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
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/>.
19 #include <linux/kernel.h>
20 #include <linux/spinlock.h>
22 #include <linux/cpumask.h>
23 #include <linux/delay.h>
24 #include <linux/cpu_pm.h>
25 #include <linux/suspend.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/clk/tegra.h>
30 #include <asm/smp_plat.h>
31 #include <asm/cacheflush.h>
32 #include <asm/suspend.h>
33 #include <asm/idmap.h>
34 #include <asm/proc-fns.h>
35 #include <asm/tlbflush.h>
45 #ifdef CONFIG_PM_SLEEP
46 static DEFINE_SPINLOCK(tegra_lp2_lock
);
47 static u32 iram_save_size
;
48 static void *iram_save_addr
;
49 struct tegra_lp1_iram tegra_lp1_iram
;
50 void (*tegra_tear_down_cpu
)(void);
51 void (*tegra_sleep_core_finish
)(unsigned long v2p
);
52 static int (*tegra_sleep_func
)(unsigned long v2p
);
54 static void tegra_tear_down_cpu_init(void)
56 switch (tegra_chip_id
) {
58 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
59 tegra_tear_down_cpu
= tegra20_tear_down_cpu
;
64 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
65 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
66 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
67 tegra_tear_down_cpu
= tegra30_tear_down_cpu
;
75 * restores cpu clock setting, clears flow controller
77 * Always called on CPU 0.
79 static void restore_cpu_complex(void)
81 int cpu
= smp_processor_id();
86 cpu
= cpu_logical_map(cpu
);
89 /* Restore the CPU clock settings */
90 tegra_cpu_clock_resume();
92 flowctrl_cpu_suspend_exit(cpu
);
98 * saves pll state for use by restart_plls, prepares flow controller for
99 * transition to suspend state
101 * Must always be called on cpu 0.
103 static void suspend_cpu_complex(void)
105 int cpu
= smp_processor_id();
110 cpu
= cpu_logical_map(cpu
);
113 /* Save the CPU clock settings */
114 tegra_cpu_clock_suspend();
116 flowctrl_cpu_suspend_enter(cpu
);
119 void tegra_clear_cpu_in_lp2(void)
121 int phy_cpu_id
= cpu_logical_map(smp_processor_id());
122 u32
*cpu_in_lp2
= tegra_cpu_lp2_mask
;
124 spin_lock(&tegra_lp2_lock
);
126 BUG_ON(!(*cpu_in_lp2
& BIT(phy_cpu_id
)));
127 *cpu_in_lp2
&= ~BIT(phy_cpu_id
);
129 spin_unlock(&tegra_lp2_lock
);
132 bool tegra_set_cpu_in_lp2(void)
134 int phy_cpu_id
= cpu_logical_map(smp_processor_id());
135 bool last_cpu
= false;
136 cpumask_t
*cpu_lp2_mask
= tegra_cpu_lp2_mask
;
137 u32
*cpu_in_lp2
= tegra_cpu_lp2_mask
;
139 spin_lock(&tegra_lp2_lock
);
141 BUG_ON((*cpu_in_lp2
& BIT(phy_cpu_id
)));
142 *cpu_in_lp2
|= BIT(phy_cpu_id
);
144 if ((phy_cpu_id
== 0) && cpumask_equal(cpu_lp2_mask
, cpu_online_mask
))
146 else if (tegra_chip_id
== TEGRA20
&& phy_cpu_id
== 1)
147 tegra20_cpu_set_resettable_soon();
149 spin_unlock(&tegra_lp2_lock
);
153 int tegra_cpu_do_idle(void)
155 return cpu_do_idle();
158 static int tegra_sleep_cpu(unsigned long v2p
)
160 setup_mm_for_reboot();
161 tegra_sleep_cpu_finish(v2p
);
163 /* should never here */
169 void tegra_idle_lp2_last(void)
171 tegra_pmc_pm_set(TEGRA_SUSPEND_LP2
);
173 cpu_cluster_pm_enter();
174 suspend_cpu_complex();
176 cpu_suspend(PHYS_OFFSET
- PAGE_OFFSET
, &tegra_sleep_cpu
);
178 restore_cpu_complex();
179 cpu_cluster_pm_exit();
182 enum tegra_suspend_mode
tegra_pm_validate_suspend_mode(
183 enum tegra_suspend_mode mode
)
186 * The Tegra devices support suspending to LP1 or lower currently.
188 if (mode
> TEGRA_SUSPEND_LP1
)
189 return TEGRA_SUSPEND_LP1
;
194 static int tegra_sleep_core(unsigned long v2p
)
196 setup_mm_for_reboot();
197 tegra_sleep_core_finish(v2p
);
199 /* should never here */
206 * tegra_lp1_iram_hook
208 * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
209 * SDRAM. These codes not be copied to IRAM in this fuction. We need to
210 * copy these code to IRAM before LP0/LP1 suspend and restore the content
211 * of IRAM after resume.
213 static bool tegra_lp1_iram_hook(void)
215 switch (tegra_chip_id
) {
217 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
218 tegra20_lp1_iram_hook();
223 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
224 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
225 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
226 tegra30_lp1_iram_hook();
232 if (!tegra_lp1_iram
.start_addr
|| !tegra_lp1_iram
.end_addr
)
235 iram_save_size
= tegra_lp1_iram
.end_addr
- tegra_lp1_iram
.start_addr
;
236 iram_save_addr
= kmalloc(iram_save_size
, GFP_KERNEL
);
243 static bool tegra_sleep_core_init(void)
245 switch (tegra_chip_id
) {
247 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
248 tegra20_sleep_core_init();
253 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
254 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
255 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
256 tegra30_sleep_core_init();
262 if (!tegra_sleep_core_finish
)
268 static void tegra_suspend_enter_lp1(void)
272 /* copy the reset vector & SDRAM shutdown code into IRAM */
273 memcpy(iram_save_addr
, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
),
275 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
),
276 tegra_lp1_iram
.start_addr
, iram_save_size
);
278 *((u32
*)tegra_cpu_lp1_mask
) = 1;
281 static void tegra_suspend_exit_lp1(void)
286 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
), iram_save_addr
,
289 *(u32
*)tegra_cpu_lp1_mask
= 0;
292 static const char *lp_state
[TEGRA_MAX_SUSPEND_MODE
] = {
293 [TEGRA_SUSPEND_NONE
] = "none",
294 [TEGRA_SUSPEND_LP2
] = "LP2",
295 [TEGRA_SUSPEND_LP1
] = "LP1",
296 [TEGRA_SUSPEND_LP0
] = "LP0",
299 static int tegra_suspend_enter(suspend_state_t state
)
301 enum tegra_suspend_mode mode
= tegra_pmc_get_suspend_mode();
303 if (WARN_ON(mode
< TEGRA_SUSPEND_NONE
||
304 mode
>= TEGRA_MAX_SUSPEND_MODE
))
307 pr_info("Entering suspend state %s\n", lp_state
[mode
]);
309 tegra_pmc_pm_set(mode
);
313 suspend_cpu_complex();
315 case TEGRA_SUSPEND_LP1
:
316 tegra_suspend_enter_lp1();
318 case TEGRA_SUSPEND_LP2
:
319 tegra_set_cpu_in_lp2();
325 cpu_suspend(PHYS_OFFSET
- PAGE_OFFSET
, tegra_sleep_func
);
328 case TEGRA_SUSPEND_LP1
:
329 tegra_suspend_exit_lp1();
331 case TEGRA_SUSPEND_LP2
:
332 tegra_clear_cpu_in_lp2();
337 restore_cpu_complex();
344 static const struct platform_suspend_ops tegra_suspend_ops
= {
345 .valid
= suspend_valid_only_mem
,
346 .enter
= tegra_suspend_enter
,
349 void __init
tegra_init_suspend(void)
351 enum tegra_suspend_mode mode
= tegra_pmc_get_suspend_mode();
353 if (mode
== TEGRA_SUSPEND_NONE
)
356 tegra_tear_down_cpu_init();
357 tegra_pmc_suspend_init();
359 if (mode
>= TEGRA_SUSPEND_LP1
) {
360 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
361 pr_err("%s: unable to allocate memory for SDRAM"
362 "self-refresh -- LP0/LP1 unavailable\n",
364 tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2
);
365 mode
= TEGRA_SUSPEND_LP2
;
369 /* set up sleep function for cpu_suspend */
371 case TEGRA_SUSPEND_LP1
:
372 tegra_sleep_func
= tegra_sleep_core
;
374 case TEGRA_SUSPEND_LP2
:
375 tegra_sleep_func
= tegra_sleep_cpu
;
381 suspend_set_ops(&tegra_suspend_ops
);